Commit 5d20c57b authored by alexpott's avatar alexpott

Issue #2107243 by amateescu, jibran, larowlan, Xano: Decouple entity reference...

Issue #2107243 by amateescu, jibran, larowlan, Xano: Decouple entity reference selection plugins from field definitions
parent c0b346b7
......@@ -348,6 +348,9 @@ services:
arguments: ['@config.manager', '@entity.manager']
tags:
- { name: event_subscriber }
plugin.manager.entity_reference_selection:
class: Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager
parent: default_plugin_manager
plugin.manager.block:
class: Drupal\Core\Block\BlockManager
parent: default_plugin_manager
......
......@@ -2,25 +2,25 @@
/**
* @file
* Contains \Drupal\entity_reference\Annotation\EntityReferenceSelection.
* Contains \Drupal\Core\Entity\Annotation\EntityReferenceSelection.
*/
namespace Drupal\entity_reference\Annotation;
namespace Drupal\Core\Entity\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines an EntityReferenceSelection plugin annotation object.
*
* Plugin Namespace: Plugin\entity_reference\selection
* Plugin Namespace: Plugin\EntityReferenceSelection
*
* For a working example, see
* \Drupal\comment\Plugin\entity_reference\selection\CommentSelection
* \Drupal\comment\Plugin\EntityReferenceSelection\CommentSelection
*
* @see \Drupal\entity_reference\Plugin\Type\SelectionPluginManager
* @see \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface
* @see \Drupal\entity_reference\Plugin\entity_reference\selection\SelectionBase
* @see \Drupal\entity_reference\Plugin\Derivative\SelectionBase
* @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager
* @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
* @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase
* @see \Drupal\Core\Entity\Plugin\Derivative\SelectionBase
* @see plugin_api
*
* @Annotation
......
......@@ -2,25 +2,25 @@
/**
* @file
* Contains \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface.
* Contains \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface.
*/
namespace Drupal\entity_reference\Plugin\Type\Selection;
namespace Drupal\Core\Entity\EntityReferenceSelection;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
/**
* Interface definition for Entity Reference Selection plugins.
*
* @see \Drupal\entity_reference\Plugin\entity_reference\selection\SelectionBase
* @see \Drupal\entity_reference\Plugin\Type\SelectionPluginManager
* @see \Drupal\entity_reference\Annotation\EntityReferenceSelection
* @see \Drupal\entity_reference\Plugin\Derivative\SelectionBase
* @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase
* @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager
* @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection
* @see \Drupal\Core\Entity\Plugin\Derivative\SelectionBase
* @see plugin_api
*/
interface SelectionInterface {
interface SelectionInterface extends PluginFormInterface {
/**
* Returns a list of referenceable entities.
......@@ -78,15 +78,4 @@ public function validateAutocompleteInput($input, &$element, FormStateInterface
*/
public function entityQueryAlter(SelectInterface $query);
/**
* Generates the settings form for this selection.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the selection is associated.
*
* @return array
* A Form API array.
*/
public static function settingsForm(FieldDefinitionInterface $field_definition);
}
......@@ -2,75 +2,68 @@
/**
* @file
* Contains \Drupal\entity_reference\Plugin\Type\SelectionPluginManager.
* Contains \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager.
*/
namespace Drupal\entity_reference\Plugin\Type;
namespace Drupal\Core\Entity\EntityReferenceSelection;
use Drupal\Component\Plugin\Factory\ReflectionFactory;
use Drupal\Component\Plugin\FallbackPluginManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
/**
* Plugin type manager for Entity Reference Selection plugins.
*
* @see \Drupal\entity_reference\Annotation\EntityReferenceSelection
* @see \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface
* @see \Drupal\entity_reference\Plugin\entity_reference\selection\SelectionBase
* @see \Drupal\entity_reference\Plugin\Derivative\SelectionBase
* @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection
* @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
* @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase
* @see \Drupal\Core\Entity\Plugin\Derivative\SelectionBase
* @see plugin_api
*/
class SelectionPluginManager extends DefaultPluginManager implements FallbackPluginManagerInterface {
class SelectionPluginManager extends DefaultPluginManager implements SelectionPluginManagerInterface, FallbackPluginManagerInterface {
/**
* {@inheritdoc}
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
$this->discovery = new AnnotatedClassDiscovery('Plugin/entity_reference/selection', $namespaces, 'Drupal\entity_reference\Annotation\EntityReferenceSelection');
// We're not using the parent constructor because we use a different factory
// method and don't need the derivative discovery decorator.
$this->factory = new ReflectionFactory($this, '\Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface');
$this->moduleHandler = $module_handler;
$this->alterInfo('entity_reference_selection');
$this->setCacheBackend($cache_backend, 'entity_reference_selection_plugins');
parent::__construct('Plugin/EntityReferenceSelection', $namespaces, $module_handler, 'Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface', 'Drupal\Core\Entity\Annotation\EntityReferenceSelection');
}
/**
* Overrides \Drupal\Component\Plugin\PluginManagerBase::getInstance().
* {@inheritdoc}
*/
public function getInstance(array $options) {
$selection_handler = $options['field_definition']->getSetting('handler');
$target_entity_type = $options['field_definition']->getSetting('target_type');
if (!isset($options['target_type'])) {
throw new \InvalidArgumentException("Missing required 'target_type' property for a EntityReferenceSelection plugin.");
}
// Initialize default options.
$options += array(
'handler' => 'default',
'handler_settings' => array(),
);
// Get all available selection plugins for this entity type.
$selection_handler_groups = $this->getSelectionGroups($target_entity_type);
$selection_handler_groups = $this->getSelectionGroups($options['target_type']);
// Sort the selection plugins by weight and select the best match.
uasort($selection_handler_groups[$selection_handler], array('Drupal\Component\Utility\SortArray', 'sortByWeightElement'));
end($selection_handler_groups[$selection_handler]);
$plugin_id = key($selection_handler_groups[$selection_handler]);
uasort($selection_handler_groups[$options['handler']], array('Drupal\Component\Utility\SortArray', 'sortByWeightElement'));
end($selection_handler_groups[$options['handler']]);
$plugin_id = key($selection_handler_groups[$options['handler']]);
return $this->createInstance($plugin_id, $options);
}
/**
* Returns a list of selection plugins that can reference a specific entity
* type.
*
* @param string $entity_type
* A Drupal entity type.
*
* @return array
* An array of selection plugins grouped by selection group.
* {@inheritdoc}
*/
public function getSelectionGroups($entity_type) {
public function getSelectionGroups($entity_type_id) {
$plugins = array();
$definitions = $this->getDefinitions();
......@@ -78,7 +71,7 @@ public function getSelectionGroups($entity_type) {
unset($definitions['broken']);
foreach ($definitions as $plugin_id => $plugin) {
if (empty($plugin['entity_types']) || in_array($entity_type, $plugin['entity_types'])) {
if (empty($plugin['entity_types']) || in_array($entity_type_id, $plugin['entity_types'])) {
$plugins[$plugin['group']][$plugin_id] = $plugin;
}
}
......@@ -87,19 +80,13 @@ public function getSelectionGroups($entity_type) {
}
/**
* Gets the selection handler for a given entity_reference field.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition for the operation.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity for the operation.
*
* @return \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface
* The selection plugin.
* {@inheritdoc}
*/
public function getSelectionHandler(FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL) {
$options = array(
'field_definition' => $field_definition,
'target_type' => $field_definition->getFieldStorageDefinition()->getSetting('target_type'),
'handler' => $field_definition->getSetting('handler'),
'handler_settings' => $field_definition->getSetting('handler_settings'),
'entity' => $entity,
);
return $this->getInstance($options);
......
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface.
*/
namespace Drupal\Core\Entity\EntityReferenceSelection;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
/**
* Defines an interface for the entity reference selection plugin manager.
*/
interface SelectionPluginManagerInterface extends PluginManagerInterface {
/**
* Returns selection plugins that can reference a specific entity type.
*
* @param string $entity_type_id
* A Drupal entity type ID.
*
* @return array
* An array of selection plugins grouped by selection group.
*/
public function getSelectionGroups($entity_type_id);
/**
* Gets the selection handler for a given entity_reference field.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition for the operation.
* @param \Drupal\Core\Entity\EntityInterface $entity
* (optional) The entity for the operation. Defaults to NULL.
*
* @return \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
* The selection plugin.
*/
public function getSelectionHandler(FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL);
}
<?php
/**
* @file
* Contains \Drupal\Core\Entity\Plugin\Derivative\SelectionBase.
*/
namespace Drupal\Core\Entity\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides derivative plugins for Entity Reference Selection plugins.
*
* @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase
* @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager
* @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection
* @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
* @see plugin_api
*/
class SelectionBase extends DeriverBase implements ContainerDeriverInterface {
/**
* The entity manager
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Creates an SelectionBase object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
*/
public function __construct(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity.manager')
);
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
$this->derivatives[$entity_type_id] = $base_plugin_definition;
$this->derivatives[$entity_type_id]['entity_types'] = array($entity_type_id);
$this->derivatives[$entity_type_id]['label'] = t('@entity_type selection', array('@entity_type' => $entity_type->getLabel()));
$this->derivatives[$entity_type_id]['base_plugin_label'] = (string) $base_plugin_definition['label'];
}
return parent::getDerivativeDefinitions($base_plugin_definition);
}
}
......@@ -2,12 +2,13 @@
/**
* @file
* Contains \Drupal\entity_reference\Plugin\Type\Selection\SelectionBroken.
* Contains \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBroken.
*/
namespace Drupal\entity_reference\Plugin\Type\Selection;
namespace Drupal\Core\Entity\Plugin\EntityReferenceSelection;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
......@@ -19,18 +20,28 @@
* label = @Translation("Broken/Missing")
* )
*/
class SelectionBroken implements SelectionInterface {
class Broken implements SelectionInterface {
/**
* {@inheritdoc}
*/
public static function settingsForm(FieldDefinitionInterface $field_definition) {
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['selection_handler'] = array(
'#markup' => t('The selected selection handler is broken.'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { }
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { }
/**
* {@inheritdoc}
*/
......
......@@ -243,7 +243,7 @@ public function preSave() {
* {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$manager = \Drupal::service('plugin.manager.entity_reference.selection');
$manager = \Drupal::service('plugin.manager.entity_reference_selection');
if ($referenceable = $manager->getSelectionHandler($field_definition)->getReferenceableEntities()) {
$group = array_rand($referenceable);
$values['target_id'] = array_rand($referenceable[$group]);
......
......@@ -2,20 +2,20 @@
/**
* @file
* Contains \Drupal\comment\Plugin\entity_reference\selection\CommentSelection.
* Contains \Drupal\comment\Plugin\EntityReferenceSelection\CommentSelection.
*/
namespace Drupal\comment\Plugin\entity_reference\selection;
namespace Drupal\comment\Plugin\EntityReferenceSelection;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase;
use Drupal\comment\CommentInterface;
use Drupal\entity_reference\Plugin\entity_reference\selection\SelectionBase;
/**
* Provides specific access control for the comment entity type.
*
* @EntityReferenceSelection(
* id = "comment_default",
* id = "default:comment",
* label = @Translation("Comment selection"),
* entity_types = {"comment"},
* group = "default",
......@@ -33,7 +33,7 @@ public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
// Adding the 'comment_access' tag is sadly insufficient for comments:
// core requires us to also know about the concept of 'published' and
// 'unpublished'.
if (!\Drupal::currentUser()->hasPermission('administer comments')) {
if (!$this->currentUser->hasPermission('administer comments')) {
$query->condition('status', CommentInterface::PUBLISHED);
}
return $query;
......@@ -61,8 +61,9 @@ public function entityQueryAlter(SelectInterface $query) {
// Passing the query to node_query_node_access_alter() is sadly
// insufficient for nodes.
// @see SelectionEntityTypeNode::entityQueryAlter()
if (!\Drupal::currentUser()->hasPermission('bypass node access') && !count(\Drupal::moduleHandler()->getImplementations('node_grants'))) {
if (!$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'))) {
$query->condition($node_alias . '.status', 1);
}
}
}
......@@ -201,7 +201,7 @@ function entity_reference_query_entity_reference_alter(AlterableInterface $query
* @param int $cardinality
* The cardinality of the field.
*
* @see \Drupal\entity_reference\Plugin\entity_reference\selection\SelectionBase::settingsForm()
* @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
*/
function entity_reference_create_field($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = array(), $cardinality = 1) {
// Look for or add the specified field to the requested entity bundle.
......
services:
plugin.manager.entity_reference.selection:
class: Drupal\entity_reference\Plugin\Type\SelectionPluginManager
parent: default_plugin_manager
entity_reference.autocomplete:
class: Drupal\entity_reference\EntityReferenceAutocomplete
arguments: ['@entity.manager', '@plugin.manager.entity_reference.selection']
arguments: ['@entity.manager', '@plugin.manager.entity_reference_selection']
......@@ -80,7 +80,7 @@ public function getSettableValues(AccountInterface $account = NULL) {
*/
public function getSettableOptions(AccountInterface $account = NULL) {
$field_definition = $this->getFieldDefinition();
if (!$options = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($field_definition, $this->getEntity())->getReferenceableEntities()) {
if (!$options = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($field_definition, $this->getEntity())->getReferenceableEntities()) {
return array();
}
......@@ -138,17 +138,18 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$field = $form_state->get('field');
// Get all selection plugins for this entity type.
$selection_plugins = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionGroups($this->getSetting('target_type'));
$handler_groups = array_keys($selection_plugins);
$handlers = \Drupal::service('plugin.manager.entity_reference.selection')->getDefinitions();
$selection_plugins = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionGroups($this->getSetting('target_type'));
$handlers_options = array();
foreach ($handlers as $plugin_id => $plugin) {
foreach (array_keys($selection_plugins) as $selection_group_id) {
// We only display base plugins (e.g. 'default', 'views', ...) and not
// entity type specific plugins (e.g. 'default_node', 'default_user',
// entity type specific plugins (e.g. 'default:node', 'default:user',
// ...).
if (in_array($plugin_id, $handler_groups)) {
$handlers_options[$plugin_id] = String::checkPlain($plugin['label']);
if (array_key_exists($selection_group_id, $selection_plugins[$selection_group_id])) {
$handlers_options[$selection_group_id] = String::checkPlain($selection_plugins[$selection_group_id][$selection_group_id]['label']);
}
elseif (array_key_exists($selection_group_id . ':' . $this->getSetting('target_type'), $selection_plugins[$selection_group_id])) {
$selection_group_plugin = $selection_group_id . ':' . $this->getSetting('target_type');
$handlers_options[$selection_group_id] = String::checkPlain($selection_plugins[$selection_group_id][$selection_group_plugin]['base_plugin_label']);
}
}
......@@ -191,8 +192,8 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
'#attributes' => array('class' => array('entity_reference-settings')),
);
$handler = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($field);
$form['handler']['handler_settings'] += $handler->settingsForm($field);
$handler = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($field);
$form['handler']['handler_settings'] += $handler->buildConfigurationForm(array(), $form_state);
return $form;
}
......@@ -209,6 +210,9 @@ public static function fieldSettingsFormValidate(array $form, FormStateInterface
if ($form_state->hasValue('field')) {
$form_state->unsetValue(array('field', 'settings', 'handler_submit'));
$form_state->get('field')->settings = $form_state->getValue(['field', 'settings']);
$handler = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($form_state->get('field'));
$handler->validateConfigurationForm($form, $form_state);
}
}
......
......@@ -10,9 +10,8 @@
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Tags;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\entity_reference\Plugin\Type\SelectionPluginManager;
use Drupal\field\FieldConfigInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
/**
......@@ -30,7 +29,7 @@ class EntityReferenceAutocomplete {
/**
* The Entity reference selection handler plugin manager.
*
* @var \Drupal\entity_reference\Plugin\Type\SelectionPluginManager
* @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface
*/
protected $selectionHandlerManager;
......@@ -39,10 +38,10 @@ class EntityReferenceAutocomplete {
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\entity_reference\Plugin\Type\SelectionPluginManager $selection_manager
* @param \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selection_manager
* The Entity reference selection handler plugin manager.
*/
public function __construct(EntityManagerInterface $entity_manager, SelectionPluginManager $selection_manager) {
public function __construct(EntityManagerInterface $entity_manager, SelectionPluginManagerInterface $selection_manager) {
$this->entityManager = $entity_manager;
$this->selectionHandlerManager = $selection_manager;
}
......
<?php
/**
* @file
* Contains \Drupal\entity_reference\Plugin\Derivative\SelectionBase.
*/
namespace Drupal\entity_reference\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
/**
* Provides derivative plugins for Entity Reference Selection plugins.
*
* @see \Drupal\entity_reference\Plugin\entity_reference\selection\SelectionBase
* @see \Drupal\entity_reference\Plugin\Type\SelectionPluginManager
* @see \Drupal\entity_reference\Annotation\EntityReferenceSelection
* @see \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface
* @see plugin_api
*/
class SelectionBase extends DeriverBase {
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$supported_entities = array(
'comment',
'file',
'node',
'taxonomy_term',
'user'
);
foreach (\Drupal::entityManager()->getDefinitions() as $entity_type_id => $entity_type) {
if (!in_array($entity_type_id, $supported_entities)) {
$this->derivatives[$entity_type_id] = $base_plugin_definition;
$this->derivatives[$entity_type_id]['label'] = t('@entity_type selection', array('@entity_type' => $entity_type->getLabel()));
}
}
return parent::getDerivativeDefinitions($base_plugin_definition);
}
}
......@@ -44,7 +44,7 @@ public static function defaultSettings() {
public function elementValidate($element, FormStateInterface $form_state, $form) {
$value = array();
// If a value was entered into the autocomplete.
$handler = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($this->fieldDefinition);
$handler = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($this->fieldDefinition);
$bundles = entity_get_bundles($this->getFieldSetting('target_type'));
$auto_create = $this->getSelectionHandlerSetting('auto_create');
......
......@@ -64,7 +64,7 @@ public function elementValidate($element, FormStateInterface $form_state, $form)
if ($value === NULL) {
// Try to get a match from the input string when the user didn't use the
// autocomplete but filled in a value manually.
$handler = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($this->fieldDefinition);
$handler = \Drupal::service('plugin.manager.entity_reference_selection')->getSelectionHandler($this->fieldDefinition);
$value = $handler->validateAutocompleteInput($element['#value'], $element, $form_state, $form, !$auto_create);
}
......
......@@ -118,6 +118,26 @@ public function testFieldAdminHandler() {
// The first 'Edit' link is for the Body field.
$this->clickLink(t('Edit'), 1);
$this->drupalPostForm(NULL, array(), t('Save settings'));
// Switch the target type to 'taxonomy_term' and check that the settings
// specific to its selection handler are displayed.
$field_name = 'node.' . $this->type . '.field_test';
$edit = array(
'field_storage[settings][target_type]' => 'taxonomy_term',
);
$this->drupalPostForm($bundle_path . '/fields/' . $field_name . '/storage', $edit, t('Save field settings'));
$this->drupalGet($bundle_path . '/fields/' . $field_name);
$this->assertFieldByName('field[settings][handler_settings][auto_create]');
// Switch the target type to 'user' and check that the settings specific to
// its selection handler are displayed.
$field_name = 'node.' . $this->type . '.field_test';
$edit = array(
'field_storage[settings][target_type]' => 'user',
);
$this->drupalPostForm($bundle_path . '/fields/' . $field_name . '/storage', $edit, t('Save field settings'));
$this->drupalGet($bundle_path . '/fields/' . $field_name);
$this->assertFieldByName('field[settings][handler_settings][filter][type]', '_none');
}
......
......@@ -103,7 +103,7 @@ protected function assertResults(array $result) {
*/
public function testSelectionHandler() {
// Get values from selection handler.
$handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($this->field);
$handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($this->field);
$result = $handler->getReferenceableEntities();
$this->assertResults($result);
}
......@@ -140,7 +140,7 @@ public function testSelectionHandlerRelationship() {
$view->save();
// Get values from the selection handler.
$handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($this->field);
$handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($this->field);
$result = $handler->getReferenceableEntities();
$this->assertResults($result);
}
......
......@@ -2,18 +2,18 @@
/**
* @file
* Contains \Drupal\file\Plugin\Type\selection\FileSelection.
* Contains \Drupal\file\Plugin\EntityReferenceSelection\FileSelection.
*/
namespace Drupal\file\Plugin\entity_reference\selection;
namespace Drupal\file\Plugin\EntityReferenceSelection;
use Drupal\entity_reference\Plugin\entity_reference\selection\SelectionBase;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase;
/**
* Provides specific access control for the file entity type.
*
* @EntityReferenceSelection(
* id = "file_default",
* id = "default:file",
* label = @Translation("File selection"),
* entity_types = {"file"},
* group = "default",
......@@ -30,4 +30,5 @@ public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$query->condition('status', FILE_STATUS_PERMANENT);
return $query;