Commit 42ebadca authored by alexpott's avatar alexpott

Issue #2017851 by pcambra, amateescu, Pancho: Move...

Issue #2017851 by pcambra, amateescu, Pancho: Move entity_reference_get_selection_handler() to a method on SelectionPluginManager.
parent e1ce0ace
......@@ -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().
*
......@@ -115,7 +102,7 @@ function entity_reference_field_validate(EntityInterface $entity = NULL, $field,
}
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));
if ($invalid_entities) {
......@@ -256,7 +243,7 @@ function entity_reference_field_instance_settings_form($field, $instance, $form_
'#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);
return $form;
......@@ -353,7 +340,7 @@ function entity_reference_settings_ajax_submit($form, &$form_state) {
* Implements hook_options_list().
*/
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();
}
......
......@@ -4,4 +4,4 @@ services:
arguments: ['@container.namespaces', '@cache.cache', '@language_manager', '@module_handler']
entity_reference.autocomplete:
class: Drupal\entity_reference\EntityReferenceAutocomplete
arguments: ['@plugin.manager.entity']
arguments: ['@plugin.manager.entity', '@plugin.manager.entity_reference.selection']
......@@ -4,9 +4,11 @@
* @file
* Contains \Drupal\entity_reference/EntityReferenceAutocomplete.
*/
namespace Drupal\entity_reference;
use Drupal\Core\Entity\EntityManager;
use Drupal\entity_reference\Plugin\Type\SelectionPluginManager;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
/**
......@@ -21,14 +23,24 @@ class EntityReferenceAutocomplete {
*/
protected $entityManager;
/**
* The Entity reference selection handler plugin manager.
*
* @var \Drupal\entity_reference\Plugin\Type\SelectionPluginManager
*/
protected $selectionHandlerManager;
/**
* Constructs a EntityReferenceAutocomplete object.
*
* @param \Drupal\Core\Entity\EntityManager $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->selectionHandlerManager = $selection_manager;
}
/**
......@@ -60,7 +72,6 @@ public function __construct(EntityManager $entity_manager) {
* @see \Drupal\entity_reference\EntityReferenceController
*/
public function getMatches($field, $instance, $entity_type, $entity_id = '', $prefix = '', $string = '') {
$target_type = $field['settings']['target_type'];
$matches = array();
$entity = NULL;
......@@ -70,7 +81,7 @@ public function getMatches($field, $instance, $entity_type, $entity_id = '', $pr
throw new AccessDeniedHttpException();
}
}
$handler = entity_reference_get_selection_handler($instance, $entity);
$handler = $this->selectionHandlerManager->getSelectionHandler($instance, $entity);
if (isset($string)) {
// Get an array of matching entities.
......
......@@ -10,6 +10,8 @@
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\Factory\ReflectionFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Field\FieldDefinitionInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Plugin\DefaultPluginManager;
......@@ -88,4 +90,24 @@ public function getSelectionGroups($entity_type) {
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);
}
}
......@@ -38,7 +38,7 @@ class AutocompleteTagsWidget extends AutocompleteWidgetBase {
public function elementValidate($element, &$form_state, $form) {
$value = array();
// 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'));
$auto_create = $this->getSelectionHandlerSetting('auto_create');
......
......@@ -69,7 +69,7 @@ public function elementValidate($element, &$form_state, $form) {
else {
// Try to get a match from the input string when the user didn't use the
// 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);
}
......
......@@ -34,7 +34,7 @@ function setUp() {
}
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 ($test['arguments'] as $arguments) {
......
......@@ -121,7 +121,7 @@ public function testSort() {
$normal_user = $this->drupalCreateUser(array('access content'));
$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
// expected.
......@@ -137,7 +137,7 @@ public function testSort() {
'field' => 'nid',
'direction' => 'ASC',
);
$handler = entity_reference_get_selection_handler($instance);
$handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($instance);
$result = $handler->getReferenceableEntities();
$expected_result = array(
$nodes['published1']->id() => $node_labels['published1'],
......
......@@ -70,7 +70,7 @@ public function testSelectionHandler() {
$instance->save();
// 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();
$success = FALSE;
......
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