Issue #2482705 by mbovan, amateescu, Berdir: ViewsSelection::validateAutocompleteInput is not implemented

8.0.x
Alex Pott 2015-08-11 09:32:16 +01:00
parent cd7e7cef91
commit a1b24c20d8
2 changed files with 55 additions and 98 deletions

View File

@ -9,6 +9,7 @@ namespace Drupal\entity_reference\Tests;
use Drupal\Core\Entity\Entity; use Drupal\Core\Entity\Entity;
use Drupal\field_ui\Tests\FieldUiTestTrait; use Drupal\field_ui\Tests\FieldUiTestTrait;
use Drupal\node\Entity\Node;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
use Drupal\taxonomy\Entity\Vocabulary; use Drupal\taxonomy\Entity\Vocabulary;
@ -247,22 +248,59 @@ class EntityReferenceAdminTest extends WebTestBase {
); );
$this->drupalPostForm(NULL, $edit, t('Save settings')); $this->drupalPostForm(NULL, $edit, t('Save settings'));
// Create a node. // Create nodes.
$edit = array( $node1 = Node::create([
'title[0][value]' => 'Foo Node', 'type' => $this->type,
); 'title' => 'Foo Node',
$this->drupalPostForm('node/add/' . $this->type, $edit, t('Save')); ]);
$node1->save();
$node2 = Node::create([
'type' => $this->type,
'title' => 'Foo Node',
]);
$node2->save();
// Try to add a new node and fill the entity reference field. // Try to add a new node and fill the entity reference field.
$this->drupalGet('node/add/' . $this->type); $this->drupalGet('node/add/' . $this->type);
$result = $this->xpath('//input[@name="field_test_entity_ref_field[0][target_id]" and contains(@data-autocomplete-path, "/entity_reference_autocomplete/node/views/")]'); $result = $this->xpath('//input[@name="field_test_entity_ref_field[0][target_id]" and contains(@data-autocomplete-path, "/entity_reference_autocomplete/node/views/")]');
$target_url = $this->getAbsoluteUrl($result[0]['data-autocomplete-path']); $target_url = $this->getAbsoluteUrl($result[0]['data-autocomplete-path']);
$this->drupalGet($target_url, array('query' => array('q' => 'Foo'))); $this->drupalGet($target_url, array('query' => array('q' => 'Foo')));
$this->assertRaw('Foo'); $this->assertRaw($node1->getTitle() . ' (' . $node1->id() . ')');
$this->assertRaw($node2->getTitle() . ' (' . $node2->id() . ')');
$edit = array(
'title[0][value]' => 'Example',
'field_test_entity_ref_field[0][target_id]' => 'Test'
);
$this->drupalPostForm('node/add/' . $this->type, $edit, t('Save'));
// Assert that entity reference autocomplete field is validated.
$this->assertText(t('1 error has been found: Test Entity Reference Field'), 'Node save failed when required entity reference field was not correctly filled.');
$this->assertText(t('There are no entities matching "@entity"', ['@entity' => 'Test']));
$edit = array(
'title[0][value]' => 'Test',
'field_test_entity_ref_field[0][target_id]' => $node1->getTitle()
);
$this->drupalPostForm('node/add/' . $this->type, $edit, t('Save'));
// Assert the results multiple times to avoid sorting problem of nodes with
// the same title.
$this->assertText(t('1 error has been found: Test Entity Reference Field'));
$this->assertText(t('Multiple entities match this reference;'));
$this->assertText(t("@node1", ['@node1' => $node1->getTitle() . ' (' . $node1->id() . ')']));
$this->assertText(t("@node2", ['@node2' => $node2->getTitle() . ' (' . $node2->id() . ')']));
$edit = array(
'title[0][value]' => 'Test',
'field_test_entity_ref_field[0][target_id]' => $node1->getTitle() . '(' . $node1->id() . ')'
);
$this->drupalPostForm('node/add/' . $this->type, $edit, t('Save'));
$this->assertLink($node1->getTitle());
} }
/** /**
* Tests the formatters for the Entity References * Tests the formatters for the Entity References.
*/ */
public function testAvailableFormatters() { public function testAvailableFormatters() {
// Create a new vocabulary. // Create a new vocabulary.

View File

@ -7,17 +7,10 @@
namespace Drupal\views\Plugin\EntityReferenceSelection; namespace Drupal\views\Plugin\EntityReferenceSelection;
use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface; 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\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.
@ -29,28 +22,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* weight = 0 * weight = 0
* ) * )
*/ */
class ViewsSelection extends PluginBase implements SelectionInterface, ContainerFactoryPluginInterface { class ViewsSelection extends SelectionBase {
/**
* 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;
/** /**
* The loaded View object. * The loaded View object.
@ -59,44 +31,6 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
*/ */
protected $view; 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} * {@inheritdoc}
*/ */
@ -161,16 +95,6 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
return $form; return $form;
} }
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { }
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { }
/** /**
* Initializes a view. * Initializes a view.
* *
@ -227,7 +151,7 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
$return = array(); $return = array();
if ($result) { if ($result) {
foreach($this->view->result as $row) { foreach ($this->view->result as $row) {
$entity = $row->_entity; $entity = $row->_entity;
$return[$entity->bundle()][$entity->id()] = $entity->label(); $return[$entity->bundle()][$entity->id()] = $entity->label();
} }
@ -259,18 +183,6 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
return $result; return $result;
} }
/**
* {@inheritdoc}
*/
public function validateAutocompleteInput($input, &$element, FormStateInterface $form_state, $form, $strict = TRUE) {
return NULL;
}
/**
* {@inheritdoc}
*/
public function entityQueryAlter(SelectInterface $query) {}
/** /**
* Element validate; Check View is valid. * Element validate; Check View is valid.
*/ */
@ -300,4 +212,11 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
$form_state->setValueForElement($element, $value); $form_state->setValueForElement($element, $value);
} }
/**
* {@inheritdoc}
*/
protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
throw new \BadMethodCallException('The Views selection plugin does not use the Entity Query system for entity selection.');
}
} }