Currently, all plugins are available, even if the module and/or field type is not available. You get a fatal error on the add field screen, e.g.

Fatal error: Call to undefined function Drupal\dream_fields\Plugin\DreamField\image_style_options() in /home/drupal/drupal-core/modules/dream_fields/src/Plugin/DreamField/DreamFieldImage.php on line 33

This happens when the image module is not enabled.

The plugins should declare the dependency on the field type, I guess in the annotation (a bit like formatters and widgets do)

/**
 * Plugin implementation of 'image'.
 *
 * @DreamField(
 *   id = "image",
 *   label = @Translation("Image"),
 *   description = @Translation("This will add an input field for a single image and will be outputted without a the label."),
 *   field_types = {
 *     "image"
 *   }
 * )
 */
CommentFileSizeAuthor
#9 2726317-plugins-use-provider.patch3.99 KBDave Reid
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

swentel created an issue. See original summary.

attiks’s picture

Working on a patch

  • attiks committed 4d7f7de on 8.x-1.x
    Issue #2726317 by attiks: Allow plugins to declare field type...
attiks’s picture

Status: Active » Fixed

Logic added to the form, although might be better inside the plugin manager?

attiks’s picture

FYI: Created an issue to add tests #2726955: Add testing

swentel’s picture

I'd be safer to let the plugin manager figure this out yeah.
One other thing, shouldn't it be documented on the annotation too ?

attiks’s picture

Status: Fixed » Needs work

Yes and Yes, thanks

Dave Reid’s picture

Why not use the 'provider' annotation key to match it with the module providing the field type? That would remove the need for any additional or custom logic.

Dave Reid’s picture

Status: Needs work » Needs review
FileSize
3.99 KB

This moves the information to the 'provider' key which just works natively with D8 plugins. And also fixes that the datetime field was missing the dependency information as well.

attiks’s picture

Status: Needs review » Fixed

Cool, thanks, it works. I was using provider to get the preview image, but will fix it using preview_provider key

  • attiks committed d7df1ce on 8.x-1.x authored by Dave Reid
    Issue #2726317 by Dave Reid, attiks: Allow plugins to declare field type...

  • attiks committed f1ef777 on 8.x-1.x
    Issue #2726317 by attiks: Add preview provider
    
attiks’s picture

Status: Fixed » Needs review

Just discussed with @swentel and it feels like we're abusing the provider key, provider should be the module providing the plugin, and there might be a case where we have multiple dependencies, which can only be specified using an array

  • attiks committed 7b41d7b on 8.x-1.x
    Issue #2726317 by Dave Reid, attiks: Allow plugins to declare field type...
  • attiks committed 8e11262 on 8.x-1.x
    Revert "Issue #2726317 by Dave Reid, attiks: Allow plugins to declare...
  • attiks committed 9035b13 on 8.x-1.x
    Revert "Issue #2726317 by attiks: Add preview provider"
    
    This reverts...
attiks’s picture

Status: Needs review » Fixed

Back to the old situation, but moved the code to the plugin manager, findDefinitions

Dave Reid’s picture

I figured for basic dependencies, use provider since the module provides the field type. The provider key was built in to core specifically to provide plugins conditionally based if a module is enabled or not, so it's using it exactly for the purpose it was built for, but oh well.

Dave Reid’s picture

Note that with the reverts, DreamFieldLink.php is missing the dependency on the link field type again.

attiks’s picture

#16 They explained it to me that the provider is the module providing the plugin

#17 Will add it again, thanks

Dave Reid’s picture

That is incorrect. See #2053153: Allow contrib modules to provide plugins on behalf of optional modules. Provider is only filled in with the module that provides it by default, but was meant to be used in this case.

Dave Reid’s picture

And https://www.drupal.org/node/2218535 as the change record

attiks’s picture

Status: Fixed » Needs review

Thanks for the links, so back to NR, but my problem remains that I need to specify multiple dependencies, ex. the list of checkboxes can reference both terms and nodes, and depends on the option widget.

I'll check if your patch still applies and will add extra check on the modules.

  • attiks committed 1d5b49a on 8.x-1.x authored by Dave Reid
    Issue #2726317 by Dave Reid, attiks: Allow plugins to declare field type...
attiks’s picture

Status: Needs review » Fixed

Switch back to using the provider, but left the field_types as well to have more control, probably not needed for core, but might be needed for contrib.

Thanks for explaining, learned something new!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.