summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-08-11 08:32:16 (GMT)
committerAlex Pott2015-08-11 08:32:16 (GMT)
commita1b24c20d887a532a079688b2da67eb125ed746f (patch)
tree4a8171556670e1c7d03a42e4d275002a549326a1
parentcd7e7cef910448adf9d146da9f365329be4dfee1 (diff)
Issue #2482705 by mbovan, amateescu, Berdir: ViewsSelection::validateAutocompleteInput is not implemented
-rw-r--r--core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php52
-rw-r--r--core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php101
2 files changed, 55 insertions, 98 deletions
diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php
index eb8b85f..0956896 100644
--- a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php
+++ b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php
@@ -9,6 +9,7 @@ namespace Drupal\entity_reference\Tests;
use Drupal\Core\Entity\Entity;
use Drupal\field_ui\Tests\FieldUiTestTrait;
+use Drupal\node\Entity\Node;
use Drupal\simpletest\WebTestBase;
use Drupal\taxonomy\Entity\Vocabulary;
@@ -247,22 +248,59 @@ class EntityReferenceAdminTest extends WebTestBase {
);
$this->drupalPostForm(NULL, $edit, t('Save settings'));
- // Create a node.
- $edit = array(
- 'title[0][value]' => 'Foo Node',
- );
- $this->drupalPostForm('node/add/' . $this->type, $edit, t('Save'));
+ // Create nodes.
+ $node1 = Node::create([
+ 'type' => $this->type,
+ 'title' => 'Foo Node',
+ ]);
+ $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.
$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/")]');
$target_url = $this->getAbsoluteUrl($result[0]['data-autocomplete-path']);
$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() {
// Create a new vocabulary.
diff --git a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php
index 7b4d424..c59a55c 100644
--- a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php
+++ b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php
@@ -7,17 +7,10 @@
namespace Drupal\views\Plugin\EntityReferenceSelection;
-use Drupal\Core\Database\Query\SelectInterface;
-use Drupal\Core\Entity\EntityManagerInterface;
-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.
@@ -29,28 +22,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* weight = 0
* )
*/
-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;
+class ViewsSelection extends SelectionBase {
/**
* The loaded View object.
@@ -60,44 +32,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) {
@@ -162,16 +96,6 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
}
/**
- * {@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
@@ -227,7 +151,7 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
$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();
}
@@ -260,18 +184,6 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
}
/**
- * {@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.
*/
public static function settingsFormValidate($element, FormStateInterface $form_state, $form) {
@@ -300,4 +212,11 @@ class ViewsSelection extends PluginBase implements SelectionInterface, Container
$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.');
+ }
+
}