Problem/Motivation
The behavior of instantiating the default entity reference selection plugin for a given entity type does not work as documented.
See \Drupal\Core\Entity\Annotation\EntityReferenceSelection::$group for the applicable documentation:
/**
* The selection plugin group.
*
* This property is used to allow selection plugins to target a specific
* entity type while also inheriting the code of an existing selection plugin.
* For example, if we want to override the NodeSelection from the 'default'
* selection type, we can define the annotation of a new plugin as follows:
* @code
* id = "node_advanced",
* entity_types = {"node"},
* group = "default",
* weight = 5
* @endcode
*
* @var string
*/
This functionality is not supported as documented, only the "default" plugin is utilized as Drupal\Core\Entity\Plugin\Derivative\DefaultSelectionDeriver assumes the plugin ID of default:[entity_type_id]. This is particularly onerous because in order to modify the behavior of all fields referencing a specific entity type, you have to provide a new plugin and update each of the field configurations. Being able to drop in a plugin with the appropriate annotation and it taking affect on all fields referencing a certain entity type (and configured to use the "default" plugin) would be much easier.
Proposed resolution
This could be addressed in a couple of ways:
- In
\Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager::getSelectionGroups, it could determine the appropriate selection handler for the entity type and provide it as plugin iddefault:[entity_type_id], or - An adjustment to
\Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::fieldSettingsForm
Remaining tasks
(reviews needed, tests to be written or run, documentation to be written, etc.)
User interface changes
None.
However, the behavior of the field configuration screen would change as choosing the "default" reference method would utilize the plugin determined to be the default for that entity type, as opposed to the existing behavior where the canonical "default" is used.
API changes
The API won't necessarily change, but the format of the data provided by \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager::getSelectionGroups may need to change. In core, only \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::fieldSettingsForm utilizes this code. However, in the project I'm currently working on I see the code used by the entity_queue and webform contrib modules.
If changes are made to \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::fieldSettingsForm, similar changes may be required in other projects, as noted above. Otherwise they may provide an experience inconsistent with core's behavior.
Data model changes
None.
Comments
Comment #4
jonathanshawDuplicate of #2649712: Entity reference selection plugins break when not following a weird ID pattern