Issue #2017851 by pcambra, amateescu, Pancho: Move entity_reference_get_selection_handler() to a method on SelectionPluginManager.

8.0.x
Alex Pott 2013-07-29 13:57:27 +02:00
parent e1ce0ace11
commit 42ebadcac2
9 changed files with 46 additions and 26 deletions

View File

@ -76,19 +76,6 @@ function entity_reference_field_widget_info_alter(&$info) {
} }
} }
/**
* Gets the selection handler for a given entity_reference field.
*
* @return \Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface
*/
function entity_reference_get_selection_handler(FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL) {
$options = array(
'field_definition' => $field_definition,
'entity' => $entity,
);
return Drupal::service('plugin.manager.entity_reference.selection')->getInstance($options);
}
/** /**
* Implements hook_field_presave(). * Implements hook_field_presave().
* *
@ -115,7 +102,7 @@ function entity_reference_field_validate(EntityInterface $entity = NULL, $field,
} }
if ($ids) { if ($ids) {
$valid_ids = entity_reference_get_selection_handler($instance, $entity)->validateReferenceableEntities(array_keys($ids)); $valid_ids = Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($instance, $entity)->validateReferenceableEntities(array_keys($ids));
$invalid_entities = array_diff_key($ids, array_flip($valid_ids)); $invalid_entities = array_diff_key($ids, array_flip($valid_ids));
if ($invalid_entities) { if ($invalid_entities) {
@ -256,7 +243,7 @@ function entity_reference_field_instance_settings_form($field, $instance, $form_
'#attributes' => array('class' => array('entity_reference-settings')), '#attributes' => array('class' => array('entity_reference-settings')),
); );
$handler = entity_reference_get_selection_handler($instance); $handler = Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($instance);
$form['handler']['handler_settings'] += $handler->settingsForm($field, $instance); $form['handler']['handler_settings'] += $handler->settingsForm($field, $instance);
return $form; return $form;
@ -353,7 +340,7 @@ function entity_reference_settings_ajax_submit($form, &$form_state) {
* Implements hook_options_list(). * Implements hook_options_list().
*/ */
function entity_reference_options_list(FieldDefinitionInterface $field_definition, EntityInterface $entity) { function entity_reference_options_list(FieldDefinitionInterface $field_definition, EntityInterface $entity) {
if (!$options = entity_reference_get_selection_handler($field_definition, $entity)->getReferenceableEntities()) { if (!$options = Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($field_definition, $entity)->getReferenceableEntities()) {
return array(); return array();
} }

View File

@ -4,4 +4,4 @@ services:
arguments: ['@container.namespaces', '@cache.cache', '@language_manager', '@module_handler'] arguments: ['@container.namespaces', '@cache.cache', '@language_manager', '@module_handler']
entity_reference.autocomplete: entity_reference.autocomplete:
class: Drupal\entity_reference\EntityReferenceAutocomplete class: Drupal\entity_reference\EntityReferenceAutocomplete
arguments: ['@plugin.manager.entity'] arguments: ['@plugin.manager.entity', '@plugin.manager.entity_reference.selection']

View File

@ -4,9 +4,11 @@
* @file * @file
* Contains \Drupal\entity_reference/EntityReferenceAutocomplete. * Contains \Drupal\entity_reference/EntityReferenceAutocomplete.
*/ */
namespace Drupal\entity_reference; namespace Drupal\entity_reference;
use Drupal\Core\Entity\EntityManager; use Drupal\Core\Entity\EntityManager;
use Drupal\entity_reference\Plugin\Type\SelectionPluginManager;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
/** /**
@ -21,14 +23,24 @@ class EntityReferenceAutocomplete {
*/ */
protected $entityManager; protected $entityManager;
/**
* The Entity reference selection handler plugin manager.
*
* @var \Drupal\entity_reference\Plugin\Type\SelectionPluginManager
*/
protected $selectionHandlerManager;
/** /**
* Constructs a EntityReferenceAutocomplete object. * Constructs a EntityReferenceAutocomplete object.
* *
* @param \Drupal\Core\Entity\EntityManager $entity_manager * @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager. * The entity manager.
* @param \Drupal\entity_reference\Plugin\Type\SelectionPluginManager $selection_manager
* The Entity reference selection handler plugin manager.
*/ */
public function __construct(EntityManager $entity_manager) { public function __construct(EntityManager $entity_manager, SelectionPluginManager $selection_manager) {
$this->entityManager = $entity_manager; $this->entityManager = $entity_manager;
$this->selectionHandlerManager = $selection_manager;
} }
/** /**
@ -60,7 +72,6 @@ class EntityReferenceAutocomplete {
* @see \Drupal\entity_reference\EntityReferenceController * @see \Drupal\entity_reference\EntityReferenceController
*/ */
public function getMatches($field, $instance, $entity_type, $entity_id = '', $prefix = '', $string = '') { public function getMatches($field, $instance, $entity_type, $entity_id = '', $prefix = '', $string = '') {
$target_type = $field['settings']['target_type'];
$matches = array(); $matches = array();
$entity = NULL; $entity = NULL;
@ -70,7 +81,7 @@ class EntityReferenceAutocomplete {
throw new AccessDeniedHttpException(); throw new AccessDeniedHttpException();
} }
} }
$handler = entity_reference_get_selection_handler($instance, $entity); $handler = $this->selectionHandlerManager->getSelectionHandler($instance, $entity);
if (isset($string)) { if (isset($string)) {
// Get an array of matching entities. // Get an array of matching entities.

View File

@ -10,6 +10,8 @@ namespace Drupal\entity_reference\Plugin\Type;
use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\Factory\ReflectionFactory; use Drupal\Component\Plugin\Factory\ReflectionFactory;
use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Field\FieldDefinitionInterface;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManager; use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Core\Plugin\DefaultPluginManager;
@ -88,4 +90,24 @@ class SelectionPluginManager extends DefaultPluginManager {
return $plugins; return $plugins;
} }
/**
* Gets the selection handler for a given entity_reference field.
*
* @param \Drupal\Core\Entity\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.
*/
public function getSelectionHandler(FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL) {
$options = array(
'field_definition' => $field_definition,
'entity' => $entity,
);
return $this->getInstance($options);
}
} }

View File

@ -38,7 +38,7 @@ class AutocompleteTagsWidget extends AutocompleteWidgetBase {
public function elementValidate($element, &$form_state, $form) { public function elementValidate($element, &$form_state, $form) {
$value = array(); $value = array();
// If a value was entered into the autocomplete. // If a value was entered into the autocomplete.
$handler = entity_reference_get_selection_handler($this->fieldDefinition); $handler = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($this->fieldDefinition);
$bundles = entity_get_bundles($this->getFieldSetting('target_type')); $bundles = entity_get_bundles($this->getFieldSetting('target_type'));
$auto_create = $this->getSelectionHandlerSetting('auto_create'); $auto_create = $this->getSelectionHandlerSetting('auto_create');

View File

@ -69,7 +69,7 @@ class AutocompleteWidget extends AutocompleteWidgetBase {
else { else {
// Try to get a match from the input string when the user didn't use the // Try to get a match from the input string when the user didn't use the
// autocomplete but filled in a value manually. // autocomplete but filled in a value manually.
$handler = entity_reference_get_selection_handler($this->fieldDefinition); $handler = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($this->fieldDefinition);
$value = $handler->validateAutocompleteInput($element['#value'], $element, $form_state, $form, !$auto_create); $value = $handler->validateAutocompleteInput($element['#value'], $element, $form_state, $form, !$auto_create);
} }

View File

@ -34,7 +34,7 @@ class EntityReferenceSelectionAccessTest extends WebTestBase {
} }
protected function assertReferenceable(FieldDefinitionInterface $field_definition, $tests, $handler_name) { protected function assertReferenceable(FieldDefinitionInterface $field_definition, $tests, $handler_name) {
$handler = entity_reference_get_selection_handler($field_definition); $handler = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($field_definition);
foreach ($tests as $test) { foreach ($tests as $test) {
foreach ($test['arguments'] as $arguments) { foreach ($test['arguments'] as $arguments) {

View File

@ -121,7 +121,7 @@ class EntityReferenceSelectionSortTest extends WebTestBase {
$normal_user = $this->drupalCreateUser(array('access content')); $normal_user = $this->drupalCreateUser(array('access content'));
$GLOBALS['user'] = $normal_user; $GLOBALS['user'] = $normal_user;
$handler = entity_reference_get_selection_handler($instance); $handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($instance);
// Not only assert the result, but make sure the keys are sorted as // Not only assert the result, but make sure the keys are sorted as
// expected. // expected.
@ -137,7 +137,7 @@ class EntityReferenceSelectionSortTest extends WebTestBase {
'field' => 'nid', 'field' => 'nid',
'direction' => 'ASC', 'direction' => 'ASC',
); );
$handler = entity_reference_get_selection_handler($instance); $handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($instance);
$result = $handler->getReferenceableEntities(); $result = $handler->getReferenceableEntities();
$expected_result = array( $expected_result = array(
$nodes['published1']->id() => $node_labels['published1'], $nodes['published1']->id() => $node_labels['published1'],

View File

@ -70,7 +70,7 @@ class SelectionTest extends WebTestBase {
$instance->save(); $instance->save();
// Get values from selection handler. // Get values from selection handler.
$handler = entity_reference_get_selection_handler($instance); $handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($instance);
$result = $handler->getReferenceableEntities(); $result = $handler->getReferenceableEntities();
$success = FALSE; $success = FALSE;