Cannot overwrite the entity reference node selection handler

Created on 12 May 2016, about 8 years ago
Updated 17 March 2023, about 1 year ago

I want to implement my own default node selection handler. But it is never picked up by the plugin system whatever naming conventions I follow.

What does the documentation say

See: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21...

/**
   * 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
   */
  public $group;

What I did

I want exactly do what is described in this comment I want to use my own selection handler instead of default:node provided with core.
I tried several annotation formats.
e.g.:

/**
 * Special version of the node reference selector that displays also the node
 * type in the autocomplete suggestions together with the default node titla.
 *
 * @EntityReferenceSelection(
 *   id = "node_with_type",
 *   label = @Translation("Node selection with additional node type in suggestions."),
 *   entity_types = {"node"},
 *   group = "default",
 *   weight = 2
 * )
 *
 */
/**
 * Special version of the node reference selector that displays also the node
 * type in the autocomplete suggestions together with the default node titla.
 *
 * @EntityReferenceSelection(
 *   id = "default:node_with_type",
 *   label = @Translation("Node selection with additional node type in suggestions."),
 *   entity_types = {"node"},
 *   group = "default",
 *   weight = 2
 * )
 *
 */

However. This is never used. In \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager::getInstance there is always used the default.

 /**
   * {@inheritdoc}
   */
  public function getInstance(array $options) {
    if (!isset($options['target_type'])) {
      throw new \InvalidArgumentException("Missing required 'target_type' property for a EntityReferenceSelection plugin.");
    }

    // Initialize default options.
    $options += array(
      'handler' => $this->getPluginId($options['target_type'], 'default'),
      'handler_settings' => array(),
    );

    // A specific selection plugin ID was already specified.
    if (strpos($options['handler'], ':') !== FALSE) {
      $plugin_id = $options['handler'];
    }
    // Only a selection group name was specified.
    else {
      $plugin_id = $this->getPluginId($options['target_type'], $options['handler']);
    }

    return $this->createInstance($plugin_id, $options);
  }

It says "Initialize default options" and then actually would get my implementations in getPluginId. But this is thrown away right away in the same step.

I question if

    $options += array(
      'handler' => $this->getPluginId($options['target_type'], 'default'),
      'handler_settings' => array(),
    );

is right?

Should it be the other way round?

    // Initialize default options.
    $more_options = array(
      'handler' => $this->getPluginId($options['target_type'], 'default'),
      'handler_settings' => array(),
    );

    $options = $more_options + $options;

Then it would work if the id is something like default:my_node. But it fails further down in

      $plugin_id = $this->getPluginId($options['target_type'], $options['handler']);

if I use an id like node_advanced as there is not such a base plugin.

I am kind of lost. Can someone who was involved in this and review if this is a bug or the docs are wrong or if it is just an error in my thinking how this is supposed to work? Thanks a lot.

πŸ’¬ Support request
Status

Fixed

Version

8.2 ⚰️

Component
PluginΒ  β†’

Last updated about 17 hours ago

Created by

πŸ‡©πŸ‡ͺGermany marcusx

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024