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:

  1. In \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager::getSelectionGroups, it could determine the appropriate selection handler for the entity type and provide it as plugin id default:[entity_type_id], or
  2. 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

kerasai created an issue. See original summary.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jonathanshaw’s picture