diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php index c9debba..cb8c7d6 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php @@ -41,8 +41,6 @@ */ class SelectionBase extends PluginBase implements SelectionInterface, ContainerFactoryPluginInterface { - use SelectionValidateTrait; - /** * The entity manager. * @@ -271,6 +269,45 @@ public function validateReferenceableEntities(array $ids) { } /** + * {@inheritdoc} + */ + public function validateAutocompleteInput($input, &$element, FormStateInterface $form_state, $form, $strict = TRUE) { + $bundled_entities = $this->getReferenceableEntities($input, '=', 6); + $entities = array(); + foreach ($bundled_entities as $entities_list) { + $entities += $entities_list; + } + $params = array( + '%value' => $input, + '@value' => $input, + ); + if (empty($entities)) { + if ($strict) { + // Error if there are no entities available for a required field. + $form_state->setError($element, $this->t('There are no entities matching "%value".', $params)); + } + } + elseif (count($entities) > 5) { + $params['@id'] = key($entities); + // Error if there are more than 5 matching entities. + $form_state->setError($element, $this->t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)".', $params)); + } + elseif (count($entities) > 1) { + // More helpful error if there are only a few matching entities. + $multiples = array(); + foreach ($entities as $id => $name) { + $multiples[] = $name . ' (' . $id . ')'; + } + $params['@id'] = $id; + $form_state->setError($element, $this->t('Multiple entities match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".', array('%multiple' => implode('", "', $multiples)))); + } + else { + // Take the one and only matching entity. + return key($entities); + } + } + + /** * Builds an EntityQuery to get referenceable entities. * * @param string|null $match diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionValidateTrait.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionValidateTrait.php deleted file mode 100644 index b77392a..0000000 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionValidateTrait.php +++ /dev/null @@ -1,56 +0,0 @@ -getReferenceableEntities($input, '=', 6); - $entities = array(); - foreach ($bundled_entities as $entities_list) { - $entities += $entities_list; - } - $params = array( - '%value' => $input, - '@value' => $input, - ); - if (empty($entities)) { - if ($strict) { - // Error if there are no entities available for a required field. - $form_state->setError($element, $this->t('There are no entities matching "%value".', $params)); - } - } - elseif (count($entities) > 5) { - $params['@id'] = key($entities); - // Error if there are more than 5 matching entities. - $form_state->setError($element, $this->t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)".', $params)); - } - elseif (count($entities) > 1) { - // More helpful error if there are only a few matching entities. - $multiples = array(); - foreach ($entities as $id => $name) { - $multiples[] = $name . ' (' . $id . ')'; - } - $params['@id'] = $id; - $form_state->setError($element, $this->t('Multiple entities match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".', array('%multiple' => implode('", "', $multiples)))); - } - else { - // Take the one and only matching entity. - return key($entities); - } - } - -} diff --git a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php index 47a946c..9bc6690 100644 --- a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php +++ b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php @@ -8,17 +8,12 @@ namespace Drupal\views\Plugin\EntityReferenceSelection; use Drupal\Core\Database\Query\SelectInterface; -use Drupal\Core\Entity\EntityManagerInterface; -use Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionValidateTrait; -use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Drupal\Core\Plugin\PluginBase; -use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Drupal\views\Views; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Plugin implementation of the 'selection' entity_reference. @@ -30,30 +25,7 @@ * weight = 0 * ) */ -class ViewsSelection extends PluginBase implements SelectionInterface, ContainerFactoryPluginInterface { - - use SelectionValidateTrait; - - /** - * The entity manager. - * - * @var \Drupal\Core\Entity\EntityManagerInterface - */ - protected $entityManager; - - /** - * The module handler service. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - - /** - * The current user. - * - * @var \Drupal\Core\Session\AccountInterface - */ - protected $currentUser; +class ViewsSelection extends SelectionBase implements SelectionInterface, ContainerFactoryPluginInterface { /** * The loaded View object. @@ -63,44 +35,6 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container protected $view; /** - * Constructs a new ViewsSelection object. - * - * @param array $configuration - * A configuration array containing information about the plugin instance. - * @param string $plugin_id - * The plugin_id for the plugin instance. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager service. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler service. - * @param \Drupal\Core\Session\AccountInterface $current_user - * The current user. - */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - - $this->entityManager = $entity_manager; - $this->moduleHandler = $module_handler; - $this->currentUser = $current_user; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('entity.manager'), - $container->get('module_handler'), - $container->get('current_user') - ); - } - - /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { @@ -165,16 +99,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta } /** - * {@inheritdoc} - */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { } - - /** - * {@inheritdoc} - */ - public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { } - - /** * Initializes a view. * * @param string|null $match @@ -230,7 +154,7 @@ public function getReferenceableEntities($match = NULL, $match_operator = 'CONTA $return = array(); if ($result) { - foreach($this->view->result as $row) { + foreach ($this->view->result as $row) { $entity = $row->_entity; $return[$entity->bundle()][$entity->id()] = $entity->label(); } @@ -263,11 +187,6 @@ public function validateReferenceableEntities(array $ids) { } /** - * {@inheritdoc} - */ - public function entityQueryAlter(SelectInterface $query) {} - - /** * Element validate; Check View is valid. */ public static function settingsFormValidate($element, FormStateInterface $form_state, $form) {