diff --git a/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/Views.php b/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/Views.php index a4e5676..04c46cb 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/Views.php +++ b/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/Views.php @@ -45,7 +45,7 @@ public function __construct($field, $instance = NULL, EntityInterface $entity = * Implements Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface::settingsForm(). */ public static function settingsForm($field, $instance) { - $view_settings = empty($instance['settings']['handler_settings']['view']) ? '' : $instance['settings']['handler_settings']['view']; + $view_settings = empty($instance['settings']['handler_settings']['view']) ? array() : $instance['settings']['handler_settings']['view']; $displays = views_get_applicable_views('entity_reference_display'); // Filter views that list the entity type we want, and group the separate // displays by view. @@ -78,8 +78,8 @@ public static function settingsForm($field, $instance) { '#description' => '

' . t('Choose the view and display that select the entities that can be referenced.
Only views with a display of type "Entity Reference" are eligible.') . '

', ); - $default = !empty($view_settings['args']) ? implode(', ', $view_settings['args']) : ''; - $form['view']['args'] = array( + $default = !empty($view_settings['arguments']) ? implode(', ', $view_settings['arguments']) : ''; + $form['view']['arguments'] = array( '#type' => 'textfield', '#title' => t('View arguments'), '#default_value' => $default, @@ -112,7 +112,7 @@ public static function settingsForm($field, $instance) { protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $limit = 0, $ids = NULL) { $view_name = $this->instance['settings']['handler_settings']['view']['view_name']; $display_name = $this->instance['settings']['handler_settings']['view']['display_name']; - $args = $this->instance['settings']['handler_settings']['view']['args']; + $arguments = $this->instance['settings']['handler_settings']['view']['arguments']; $entity_type = $this->field['settings']['target_type']; // Check that the view is valid and the display still exists. @@ -138,11 +138,11 @@ protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $ */ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { $display_name = $this->instance['settings']['handler_settings']['view']['display_name']; - $args = $this->instance['settings']['handler_settings']['view']['args']; + $arguments = $this->instance['settings']['handler_settings']['view']['arguments']; $result = array(); if ($this->initializeView($match, $match_operator, $limit)) { // Get the results. - $result = $this->view->executeDisplay($display_name, $args); + $result = $this->view->executeDisplay($display_name, $arguments); } $return = array(); @@ -168,11 +168,11 @@ public function countReferencableEntities($match = NULL, $match_operator = 'CONT */ public function validateReferencableEntities(array $ids) { $display_name = $this->instance['settings']['handler_settings']['view']['display_name']; - $args = $this->instance['settings']['handler_settings']['view']['args']; + $arguments = $this->instance['settings']['handler_settings']['view']['arguments']; $result = array(); if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) { // Get the results. - $entities = $this->view->executeDisplay($display_name, $args); + $entities = $this->view->executeDisplay($display_name, $arguments); $result = array_keys($entities); } return $result; @@ -186,7 +186,7 @@ public function validateAutocompleteInput($input, &$element, &$form_state, $form } /** - * Implements Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface::entityFieldQueryAlter(). + * Implements Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface::entityQueryAlter(). */ public function entityQueryAlter(SelectInterface $query) {} @@ -203,19 +203,19 @@ public function settingsFormValidate($element, &$form_state, $form) { return; } - // Explode the 'args' string into an actual array. Beware, explode() turns an + // Explode the 'arguments' string into an actual array. Beware, explode() turns an // empty string into an array with one empty string. We'll need an empty array // instead. - $args_string = trim($element['args']['#value']); - if ($args_string === '') { - $args = array(); + $arguments_string = trim($element['arguments']['#value']); + if ($arguments_string === '') { + $arguments = array(); } else { // array_map is called to trim whitespaces from the arguments. - $args = array_map('trim', explode(',', $args_string)); + $arguments = array_map('trim', explode(',', $arguments_string)); } - $value = array('view_name' => $view, 'display_name' => $display, 'args' => $args); + $value = array('view_name' => $view, 'display_name' => $display, 'arguments' => $arguments); form_set_value($element, $value, $form_state); } } diff --git a/core/modules/views/lib/Drupal/views/Tests/EntityReference/SelectionTest.php b/core/modules/views/lib/Drupal/views/Tests/EntityReference/SelectionTest.php new file mode 100644 index 0000000..9b194a2 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/EntityReference/SelectionTest.php @@ -0,0 +1,89 @@ + 'Entity reference selection handler', + 'description' => 'Tests entity-reference selection handler provided by Views.', + 'group' => 'Views', + ); + } + + public static $modules = array('entity_reference'); + + protected function setUp() { + parent::setUp(); + + $this->enableViewsTestModule(); + } + + /** + * Tests the selection handler. + */ + public function testSelectionHandler() { + // Create nodes. + $type = $this->drupalCreateContentType()->type; + $node1 = $this->drupalCreateNode(array('type' => $type)); + $node2 = $this->drupalCreateNode(array('type' => $type)); + $node3 = $this->drupalCreateNode(); + + $nodes = array(); + foreach (array($node1, $node2, $node3) as $node) { + $nodes[$node->type][$node->nid] = $node->label(); + } + + // Build a fake field instance. + $field = array( + 'translatable' => FALSE, + 'entity_types' => array(), + 'settings' => array( + 'target_type' => 'node', + ), + 'field_name' => 'test_field', + 'type' => 'entity_reference', + 'cardinality' => '1', + ); + $instance = array( + 'settings' => array( + 'handler' => 'views', + 'handler_settings' => array( + 'target_bundles' => array(), + 'view' => array( + 'view_name' => 'entity_reference', + 'display_name' => 'entity_reference_1', + 'arguments' => array(), + ), + ), + ), + ); + + // Get values from selection handler. + $handler = entity_reference_get_selection_handler($field, $instance); + $result = $handler->getReferencableEntities(); + + $success = FALSE; + foreach ($result as $node_type => $values) { + foreach ($values as $nid => $label) { + if (!$success = $nodes[$node_type][$nid] == trim(strip_tags($label))) { + // There was some error, so break. + break; + } + } + } + + $this->assertTrue($success, 'Views selection handler returned expected values.'); + } +} diff --git a/core/modules/views/tests/views_test_config/config/views.view.entity_reference.yml b/core/modules/views/tests/views_test_config/config/views.view.entity_reference.yml new file mode 100644 index 0000000..0cddf22 --- /dev/null +++ b/core/modules/views/tests/views_test_config/config/views.view.entity_reference.yml @@ -0,0 +1,81 @@ +api_version: '3.0' +base_field: nid +base_table: node +core: 8.x +description: '' +disabled: '0' +display: + default: + display_plugin: default + id: default + display_title: Master + position: '' + display_options: + access: + type: perm + cache: + type: none + query: + type: views_query + exposed_form: + type: basic + pager: + type: full + style: + type: default + row: + type: fields + fields: + title: + id: title + table: node + field: title + label: '' + alter: + alter_text: '0' + make_link: '0' + absolute: '0' + trim: '0' + word_boundary: '0' + ellipsis: '0' + strip_tags: '0' + html: '0' + hide_empty: '0' + empty_zero: '0' + link_to_node: '1' + filters: + status: + value: '1' + table: node + field: status + id: status + expose: + operator: '0' + group: '1' + sorts: + created: + id: created + table: node + field: created + order: DESC + entity_reference_1: + display_plugin: entity_reference + id: entity_reference_1 + display_title: EntityReference + position: '' + display_options: + style: + type: entity_reference + options: + grouping: { } + search_fields: + title: title + pager: + type: none + options: + offset: '0' +human_name: 'Entity reference' +module: views +name: entity_reference +tag: '' +uuid: e9b5e564-c7c7-491f-aa49-73e23ba2ae33