Commit 4ea2fc71 authored by alexpott's avatar alexpott

Issue #2578559 by yched: Have ViewsSelection no longer extend SelectionBase

parent c8f46903
...@@ -19,8 +19,6 @@ ...@@ -19,8 +19,6 @@
* *
* @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager
* @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
* @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase
* @see \Drupal\Core\Entity\Plugin\Derivative\SelectionBase
* @see plugin_api * @see plugin_api
* *
* @Annotation * @Annotation
......
...@@ -155,7 +155,7 @@ public static function validateEntityAutocomplete(array &$element, FormStateInte ...@@ -155,7 +155,7 @@ public static function validateEntityAutocomplete(array &$element, FormStateInte
if ($match === NULL) { if ($match === NULL) {
// Try to get a match from the input string when the user didn't use // Try to get a match from the input string when the user didn't use
// the autocomplete but filled in a value manually. // the autocomplete but filled in a value manually.
$match = $handler->validateAutocompleteInput($input, $element, $form_state, $complete_form, !$autocreate); $match = static::matchEntityByTitle($handler, $input, $element, $form_state, !$autocreate);
} }
if ($match !== NULL) { if ($match !== NULL) {
...@@ -202,6 +202,60 @@ public static function validateEntityAutocomplete(array &$element, FormStateInte ...@@ -202,6 +202,60 @@ public static function validateEntityAutocomplete(array &$element, FormStateInte
$form_state->setValueForElement($element, $value); $form_state->setValueForElement($element, $value);
} }
/**
* Finds an entity from an autocomplete input without an explicit ID.
*
* The method will return an entity ID if one single entity unambuguously
* matches the incoming input, and sill assign form errors otherwise.
*
* @param string $input
* Single string from autocomplete element.
* @param array $element
* The form element to set a form error.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
* @param bool $strict
* Whether to trigger a form error if an element from $input (eg. an entity)
* is not found.
*
* @return integer|null
* Value of a matching entity ID, or NULL if none.
*/
protected static function matchEntityByTitle($handler, $input, &$element, FormStateInterface $form_state, $strict) {
$entities_by_bundle = $handler->getReferenceableEntities($input, '=', 6);
$entities = array_reduce($entities_by_bundle, function ($flattened, $bundle_entities) {
return $flattened + $bundle_entities;
}, []);
$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, 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, 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, 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);
}
}
/** /**
* Converts an array of entity objects into a string of entity labels. * Converts an array of entity objects into a string of entity labels.
* *
......
...@@ -8,16 +8,13 @@ ...@@ -8,16 +8,13 @@
namespace Drupal\Core\Entity\EntityReferenceSelection; namespace Drupal\Core\Entity\EntityReferenceSelection;
use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface; use Drupal\Core\Plugin\PluginFormInterface;
/** /**
* Interface definition for Entity Reference Selection plugins. * Interface definition for Entity Reference Selection plugins.
* *
* @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase
* @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager
* @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection * @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection
* @see \Drupal\Core\Entity\Plugin\Derivative\SelectionBase
* @see plugin_api * @see plugin_api
*/ */
interface SelectionInterface extends PluginFormInterface { interface SelectionInterface extends PluginFormInterface {
...@@ -48,28 +45,6 @@ public function countReferenceableEntities($match = NULL, $match_operator = 'CON ...@@ -48,28 +45,6 @@ public function countReferenceableEntities($match = NULL, $match_operator = 'CON
*/ */
public function validateReferenceableEntities(array $ids); public function validateReferenceableEntities(array $ids);
/**
* Validates input from an autocomplete widget that has no ID.
*
* @param string $input
* Single string from autocomplete widget.
* @param array $element
* The form element to set a form error.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
* @param array $form
* The form.
* @param bool $strict
* Whether to trigger a form error if an element from $input (eg. an entity)
* is not found. Defaults to TRUE.
*
* @return integer|null
* Value of a matching entity ID, or NULL if none.
*
* @see \Drupal\entity_reference\Plugin\Field\FieldWidget::elementValidate()
*/
public function validateAutocompleteInput($input, &$element, FormStateInterface $form_state, $form, $strict = TRUE);
/** /**
* Allows the selection to alter the SelectQuery generated by EntityFieldQuery. * Allows the selection to alter the SelectQuery generated by EntityFieldQuery.
* *
......
...@@ -19,8 +19,6 @@ ...@@ -19,8 +19,6 @@
* *
* @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection * @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection
* @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
* @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase
* @see \Drupal\Core\Entity\Plugin\Derivative\SelectionBase
* @see plugin_api * @see plugin_api
*/ */
class SelectionPluginManager extends DefaultPluginManager implements SelectionPluginManagerInterface, FallbackPluginManagerInterface { class SelectionPluginManager extends DefaultPluginManager implements SelectionPluginManagerInterface, FallbackPluginManagerInterface {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
/** /**
* @file * @file
* Contains \Drupal\Core\Entity\Plugin\Derivative\SelectionBase. * Contains \Drupal\Core\Entity\Plugin\Derivative\DefaultSelectionDeriver.
*/ */
namespace Drupal\Core\Entity\Plugin\Derivative; namespace Drupal\Core\Entity\Plugin\Derivative;
...@@ -13,15 +13,15 @@ ...@@ -13,15 +13,15 @@
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Provides derivative plugins for Entity Reference Selection plugins. * Provides derivative plugins for the DefaultSelection plugin.
* *
* @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection
* @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager
* @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection * @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection
* @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
* @see plugin_api * @see plugin_api
*/ */
class SelectionBase extends DeriverBase implements ContainerDeriverInterface { class DefaultSelectionDeriver extends DeriverBase implements ContainerDeriverInterface {
/** /**
* The entity manager * The entity manager
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface; use Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
/** /**
...@@ -63,11 +62,6 @@ public function validateReferenceableEntities(array $ids) { ...@@ -63,11 +62,6 @@ public function validateReferenceableEntities(array $ids) {
return array(); return array();
} }
/**
* {@inheritdoc}
*/
public function validateAutocompleteInput($input, &$element, FormStateInterface $form_state, $form, $strict = TRUE) { }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -17,10 +17,9 @@ ...@@ -17,10 +17,9 @@
* cannot filter properly, for example when the target entity type has no * cannot filter properly, for example when the target entity type has no
* 'label' key provided in the entity type plugin definition. * 'label' key provided in the entity type plugin definition.
* *
* @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase * @see \Drupal\Core\Entity\Plugin\Derivative\DefaultSelectionDeriver
* @see \Drupal\Core\Entity\Plugin\Derivative\SelectionBase
*/ */
class PhpSelection extends SelectionBase { class PhpSelection extends DefaultSelection {
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
namespace Drupal\comment\Plugin\EntityReferenceSelection; namespace Drupal\comment\Plugin\EntityReferenceSelection;
use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
use Drupal\comment\CommentInterface; use Drupal\comment\CommentInterface;
/** /**
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
* weight = 1 * weight = 1
* ) * )
*/ */
class CommentSelection extends SelectionBase { class CommentSelection extends DefaultSelection {
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\file\Plugin\EntityReferenceSelection; namespace Drupal\file\Plugin\EntityReferenceSelection;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
/** /**
* Provides specific access control for the file entity type. * Provides specific access control for the file entity type.
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
* weight = 1 * weight = 1
* ) * )
*/ */
class FileSelection extends SelectionBase { class FileSelection extends DefaultSelection {
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\node\Plugin\EntityReferenceSelection; namespace Drupal\node\Plugin\EntityReferenceSelection;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
/** /**
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
* weight = 1 * weight = 1
* ) * )
*/ */
class NodeSelection extends SelectionBase { class NodeSelection extends DefaultSelection {
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Html;
use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\Entity\Vocabulary; use Drupal\taxonomy\Entity\Vocabulary;
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
* weight = 1 * weight = 1
* ) * )
*/ */
class TermSelection extends SelectionBase { class TermSelection extends DefaultSelection {
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
use Drupal\Core\Database\Connection; use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
* weight = 1 * weight = 1
* ) * )
*/ */
class UserSelection extends SelectionBase { class UserSelection extends DefaultSelection {
/** /**
* The database connection. * The database connection.
......
...@@ -7,10 +7,17 @@ ...@@ -7,10 +7,17 @@
namespace Drupal\views\Plugin\EntityReferenceSelection; namespace Drupal\views\Plugin\EntityReferenceSelection;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase; use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\views\Views; use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Plugin implementation of the 'selection' entity_reference. * Plugin implementation of the 'selection' entity_reference.
...@@ -22,7 +29,66 @@ ...@@ -22,7 +29,66 @@
* weight = 0 * weight = 0
* ) * )
*/ */
class ViewsSelection extends SelectionBase { class ViewsSelection extends PluginBase implements SelectionInterface, ContainerFactoryPluginInterface {
/**
* 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;
/**
* Constructs a new SelectionBase 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')
);
}
/** /**
* The loaded View object. * The loaded View object.
...@@ -98,10 +164,12 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta ...@@ -98,10 +164,12 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { }
// Don't call the parent validation handler because we don't have any
// 'target_bundles' setting. /**
} * {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { }
/** /**
* Initializes a view. * Initializes a view.
...@@ -223,8 +291,6 @@ public static function settingsFormValidate($element, FormStateInterface $form_s ...@@ -223,8 +291,6 @@ public static function settingsFormValidate($element, FormStateInterface $form_s
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { public function entityQueryAlter(SelectInterface $query) { }
throw new \BadMethodCallException('The Views selection plugin does not use the Entity Query system for entity selection.');
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment