diff --git a/core/modules/field/modules/entity_reference/entity_reference.module b/core/modules/field/modules/entity_reference/entity_reference.module index df3e3f3..c94e685 100644 --- a/core/modules/field/modules/entity_reference/entity_reference.module +++ b/core/modules/field/modules/entity_reference/entity_reference.module @@ -97,7 +97,7 @@ function entity_reference_field_validate($entity_type, $entity, $field, $instanc foreach ($invalid_entities as $id => $delta) { $errors[$field['field_name']][$langcode][$delta][] = array( 'error' => 'entity_reference_invalid_entity', - 'message' => t('The referenced entity (@type: @id) is invalid.', array('@type' => $field['settings']['target_type'], '@id' => $id)), + 'message' => t('The referenced entity (@type: @id) does not exist.', array('@type' => $field['settings']['target_type'], '@id' => $id)), ); } } @@ -235,7 +235,7 @@ function _entity_reference_field_instance_settings_ajax_process($form, $form_sta * @see _entity_reference_field_instance_settings_ajax_process() */ function _entity_reference_field_instance_settings_ajax_process_element(&$element, $main_form) { - if (isset($element['#ajax']) && $element['#ajax'] === TRUE) { + if (!empty($element['#ajax'])) { $element['#ajax'] = array( 'callback' => 'entity_reference_settings_ajax', 'wrapper' => $main_form['#id'], @@ -335,7 +335,7 @@ function entity_reference_options_list($field, $instance, $entity_type = NULL, $ */ function entity_reference_query_entity_reference_alter(AlterableInterface $query) { $handler = $query->getMetadata('entity_reference_selection_handler'); - $handler->entityFieldQueryAlter($query); + $handler->entityQueryAlter($query); } /** @@ -425,7 +425,7 @@ function entity_reference_autocomplete_callback_get_matches($type, $field, $inst if ($entity_id !== 'NULL') { $entity = entity_load($entity_type, $entity_id); - // TODO: Improve when we have entity_access(). + // @todo: Improve when we have entity_access(). $entity_access = $target_type == 'node' ? node_access('view', $entity) : TRUE; if (!$entity || !$entity_access) { return MENU_ACCESS_DENIED; diff --git a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionBroken.php b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionBroken.php index 2f09cb5..5d8147c 100644 --- a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionBroken.php +++ b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionBroken.php @@ -59,7 +59,7 @@ public function validateReferencableEntities(array $ids) { public function validateAutocompleteInput($input, &$element, &$form_state, $form) { } /** - * Implements SelectionInterface::entityFieldQueryAlter(). + * Implements SelectionInterface::entityQueryAlter(). */ - public function entityFieldQueryAlter(SelectInterface $query) { } + public function entityQueryAlter(SelectInterface $query) { } } diff --git a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionInterface.php b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionInterface.php index 05a11c5..9ad938d 100644 --- a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionInterface.php +++ b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionInterface.php @@ -62,7 +62,7 @@ public function validateAutocompleteInput($input, &$element, &$form_state, $form * @param \Drupal\Core\Database\Query\SelectInterface $query * A Select Query object. */ - public function entityFieldQueryAlter(SelectInterface $query); + public function entityQueryAlter(SelectInterface $query); /** * Generates the settings form for this selection. diff --git a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/Comment.php b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/Comment.php index ec93709..307ecbd 100644 --- a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/Comment.php +++ b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/Comment.php @@ -27,17 +27,26 @@ class Comment extends SelectionGeneric { /** - * Overrides SelectionBase::entityFieldQueryAlter(). + * Overrides SelectionBase::buildEntityQuery(). */ - public function entityFieldQueryAlter(SelectInterface $query) { - // Adding the 'comment_access' tag is sadly insufficient for comments: core - // requires us to also know about the concept of 'published' and + public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { + $query = parent::buildEntityQuery($match, $match_operator); + + // Adding the 'comment_access' tag is sadly insufficient for comments: + // core requires us to also know about the concept of 'published' and // 'unpublished'. - $tables = $query->getTables(); - $base_table = $tables['base_table']['alias']; if (!user_access('administer comments')) { - $query->condition("$base_table.status", COMMENT_PUBLISHED); + $query->condition('status', COMMENT_PUBLISHED); } + return $query; + } + + /** + * Overrides SelectionBase::entityQueryAlter(). + */ + public function entityQueryAlter(SelectInterface $query) { + $tables = $query->getTables(); + $base_table = $tables['base_table']['alias']; // The Comment module doesn't implement any proper comment access, // and as a consequence doesn't make sure that comments cannot be viewed @@ -64,7 +73,7 @@ public function entityFieldQueryAlter(SelectInterface $query) { // Passing the query to node_query_node_access_alter() is sadly // insufficient for nodes. - // @see SelectionEntityTypeNode::entityFieldQueryAlter() + // @see SelectionEntityTypeNode::entityQueryAlter() if (!user_access('bypass node access') && !count(module_implements('node_grants'))) { $query->condition($node_alias . '.status', 1); } diff --git a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/File.php b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/File.php index 858645c..004a28f 100644 --- a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/File.php +++ b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/File.php @@ -27,17 +27,10 @@ class File extends SelectionGeneric { /** - * Overrides SelectionBase::entityFieldQueryAlter(). + * Overrides SelectionBase::buildEntityQuery(). */ - public function entityFieldQueryAlter(SelectInterface $query) { - // Core forces us to know about 'permanent' vs. 'temporary' files. - $tables = $query->getTables(); - $base_table = $tables['base_table']['alias']; + public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { + $query = parent::buildEntityQuery($match, $match_operator); $query->condition('status', FILE_STATUS_PERMANENT); - - // Access control to files is a very difficult business. For now, we are not - // going to give it a shot. - // @todo: fix this when core access control is less insane. - return $query; } } diff --git a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/Node.php b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/Node.php index b5dd021..8707e35 100644 --- a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/Node.php +++ b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/Node.php @@ -27,18 +27,18 @@ class Node extends SelectionGeneric { /** - * Overrides SelectionBase::entityFieldQueryAlter(). + * Overrides SelectionBase::buildEntityQuery(). */ - public function entityFieldQueryAlter(SelectInterface $query) { + public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { + $query = parent::buildEntityQuery($match, $match_operator); // Adding the 'node_access' tag is sadly insufficient for nodes: core // requires us to also know about the concept of 'published' and // 'unpublished'. We need to do that as long as there are no access control // modules in use on the site. As long as one access control module is there, // it is supposed to handle this check. if (!user_access('bypass node access') && !count(module_implements('node_grants'))) { - $tables = $query->getTables(); - $base_table = $tables['base_table']['alias']; - $query->condition("$base_table.status", NODE_PUBLISHED); + $query->condition('status', NODE_PUBLISHED); } + return $query; } } diff --git a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionGeneric.php b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionGeneric.php index 0e9184e..888e097 100644 --- a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionGeneric.php +++ b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionGeneric.php @@ -29,6 +29,27 @@ class SelectionGeneric implements SelectionInterface { /** + * The field array. + * + * @var array + */ + protected $field; + + /** + * The instance array. + * + * @var array + */ + protected $instance; + + /** + * The entity object, or NULL + * + * @var NULL|EntityInterface + */ + protected $entity; + + /** * Constructs a SelectionGeneric object. */ public function __construct($field, $instance, EntityInterface $entity = NULL) { @@ -163,7 +184,7 @@ public static function settingsForm($field, $instance) { public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { $target_type = $this->field['settings']['target_type']; - $query = $this->buildEntityFieldQuery($match, $match_operator); + $query = $this->buildEntityQuery($match, $match_operator); if ($limit > 0) { $query->range(0, $limit); } @@ -188,7 +209,7 @@ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAI * Implements SelectionInterface::countReferencableEntities(). */ public function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') { - $query = $this->buildEntityFieldQuery($match, $match_operator); + $query = $this->buildEntityQuery($match, $match_operator); return $query ->count() ->execute(); @@ -202,7 +223,7 @@ public function validateReferencableEntities(array $ids) { if ($ids) { $target_type = $this->field['settings']['target_type']; $entity_info = entity_get_info($target_type); - $query = $this->buildEntityFieldQuery(); + $query = $this->buildEntityQuery(); $result = $query ->condition($entity_info['entity_keys']['id'], $ids, 'IN') ->execute(); @@ -215,37 +236,39 @@ public function validateReferencableEntities(array $ids) { * Implements SelectionInterface::validateAutocompleteInput(). */ public function validateAutocompleteInput($input, &$element, &$form_state, $form) { - $entities = $this->getReferencableEntities($input, '=', 6); - if (empty($entities)) { - // Error if there are no entities available for a required field. - form_error($element, t('There are no entities matching "%value"', array('%value' => $input))); - } - elseif (count($entities) > 5) { - // Error if there are more than 5 matching entities. - form_error($element, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)"', array( - '%value' => $input, - '@value' => $input, - '@id' => key($entities), - ))); - } - elseif (count($entities) > 1) { - // More helpful error if there are only a few matching entities. - $multiples = array(); - foreach ($entities as $id => $name) { - $multiples[] = $name . ' (' . $id . ')'; - } - form_error($element, t('Multiple entities match this reference; "%multiple"', array('%multiple' => implode('", "', $multiples)))); - } - else { - // Take the one and only matching entity. - return key($entities); + $entities = $this->getReferencableEntities($input, '=', 6); + $params = array( + '%value' => $input, + '@value' => $input, + ); + if (empty($entities)) { + // Error if there are no entities available for a required field. + form_error($element, t('There are no entities matching "%value".', $params)); + } + elseif (count($entities) > 5) { + $params['@id'] = key($entities); + // Error if there are more than 5 matching entities. + form_error($element, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)".', $params)); + } + elseif (count($entities) > 1) { + // More helpful error if there are only a few matching entities. + $multiples = array(); + foreach ($entities as $id => $name) { + $multiples[] = $name . ' (' . $id . ')'; } + $params['@id'] = $id; + form_error($element, t('Multiple entities match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".', array('%multiple' => implode('", "', $multiples)))); + } + else { + // Take the one and only matching entity. + return key($entities); + } } /** * Builds an EntityFieldQuery to get referencable entities. */ - protected function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') { + public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { $target_type = $this->field['settings']['target_type']; $entity_info = entity_get_info($target_type); @@ -284,9 +307,9 @@ protected function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTA } /** - * Implements SelectionInterface::entityFieldQueryAlter(). + * Implements SelectionInterface::entityQueryAlter(). */ - public function entityFieldQueryAlter(SelectInterface $query) { } + public function entityQueryAlter(SelectInterface $query) { } /** * Helper method: Passes a query to the alteration system again. diff --git a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/Term.php b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/Term.php index 7b8cbc8..00e8006 100644 --- a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/Term.php +++ b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/Term.php @@ -27,9 +27,9 @@ class Term extends SelectionGeneric { /** - * Overrides SelectionBase::entityFieldQueryAlter(). + * Overrides SelectionBase::entityQueryAlter(). */ - public function entityFieldQueryAlter(SelectInterface $query) { + public function entityQueryAlter(SelectInterface $query) { // The Taxonomy module doesn't implement any proper taxonomy term access, // and as a consequence doesn't make sure that taxonomy terms cannot be // viewed when the user doesn't have access to the vocabulary. diff --git a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/User.php b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/User.php index 8effa95..9c64364 100644 --- a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/User.php +++ b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/User.php @@ -28,10 +28,10 @@ class User extends SelectionGeneric { /** - * Overrides SelectionBase::buildEntityFieldQuery(). + * Overrides SelectionBase::buildEntityQuery(). */ - public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS') { - $query = parent::buildEntityFieldQuery($match, $match_operator); + public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { + $query = parent::buildEntityQuery($match, $match_operator); // The user entity doesn't have a label column. if (isset($match)) { @@ -47,9 +47,9 @@ public function buildEntityFieldQuery($match = NULL, $match_operator = 'CONTAINS } /** - * Overrides SelectionBase::entityFieldQueryAlter(). + * Overrides SelectionBase::entityQueryAlter(). */ - public function entityFieldQueryAlter(SelectInterface $query) { + public function entityQueryAlter(SelectInterface $query) { if (user_access('administer users')) { // In addition, if the user is administrator, we need to make sure to // match the anonymous user, that doesn't actually have a name in the diff --git a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php index f53a162..61f3606 100644 --- a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php +++ b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php @@ -76,7 +76,7 @@ public function prepareView(array $entities, $langcode, array &$items) { $entity = $target_entities[$identifier]; - // TODO: Improve when we have entity_access(). + // @todo: Improve when we have entity_access(). $entity_access = $target_type == 'node' ? node_access('view', $entity) : TRUE; if (!$entity_access) { continue; diff --git a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/views/display/EntityReference.php b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/views/display/EntityReference.php index 40334a3..3b43c6f 100644 --- a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/views/display/EntityReference.php +++ b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/views/display/EntityReference.php @@ -27,15 +27,27 @@ * help = @Translation("Selects referenceable entities for an entity reference field."), * theme = "views_view", * uses_hook_menu = FALSE, - * use_ajax = FALSE, - * use_pager = FALSE, - * accept_attachments = FALSE, * entity_reference_display = TRUE * ) */ class EntityReference extends DisplayPluginBase { /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::$useAJAX. + */ + protected $usesAJAX = FALSE; + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::$usesPager. + */ + protected $usesPager = FALSE; + + /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAttachments. + */ + protected $usesAttachments = FALSE; + + /** * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::defineOptions(). */ protected function defineOptions() { @@ -59,6 +71,17 @@ protected function defineOptions() { } /** + * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::optionsSummary(). + * + * Disable 'cache' and 'title' so it won't be changed. + */ + public function optionsSummary(&$categories, &$options) { + parent::optionsSummary($categories, $options); + unset($options['query']); + unset($options['title']); + } + + /** * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::getStyleType(). */ protected function getStyleType() { @@ -76,7 +99,7 @@ public function execute() { * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::render(). */ public function render() { - if (!empty($this->view->result) || !empty($this->view->style_plugin->definition['even empty'])) { + if (!empty($this->view->result) && $this->view->style_plugin->even_empty()) { return $this->view->style_plugin->render($this->view->result); } return ''; @@ -93,21 +116,15 @@ public function usesExposed() { * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::query(). */ public function query() { - $options = $this->getOption('entity_reference_options'); - - // Play nice with Views UI 'preview' : if the view is not executed through - // EntityReference_SelectionHandler_Views::getReferencableEntities(), - // don't alter the query. - if (empty($options)) { + if (!empty($this->view->live_preview)) { return; } - // Make sure the id field is included in the results, and save its alias - // so that references_plugin_style can retrieve it. - $this->id_field_alias = $id_field = $this->view->query->add_field($this->view->storage->base_table, $this->view->storage->base_field); - if (strpos($id_field, '.') === FALSE) { - $id_field = $this->view->storage->base_table . '.' . $this->id_field_alias; - } + // Make sure the id field is included in the results. + $id_field = $this->view->storage->get('base_field'); + $this->id_field_alias = $this->view->query->add_field($this->view->storage->get('base_table'), $id_field); + + $options = $this->getOption('entity_reference_options'); // Restrict the autocomplete options based on what's been typed already. if (isset($options['match'])) { diff --git a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/views/style/EntityReference.php b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/views/style/EntityReference.php index e8f6857..1e0f602 100644 --- a/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/views/style/EntityReference.php +++ b/core/modules/field/modules/entity_reference/lib/Drupal/entity_reference/Plugin/views/style/EntityReference.php @@ -26,11 +26,22 @@ */ class EntityReference extends StylePluginBase { + /** + * Overrides Drupal\views\Plugin\views\style\StylePluginBase::usesRowPlugin. + */ protected $usesRowPlugin = TRUE; + /** + * Overrides Drupal\views\Plugin\views\style\StylePluginBase::usesFields. + */ protected $usesFields = TRUE; /** + * Overrides Drupal\views\Plugin\views\style\StylePluginBase::usesGrouping. + */ + protected $usesGrouping = FALSE; + + /** * Overrides Drupal\views\Plugin\views\style\StylePluginBase\StylePluginBase::defineOptions(). */ protected function defineOptions() { @@ -45,33 +56,24 @@ protected function defineOptions() { */ public function buildOptionsForm(&$form, &$form_state) { parent::buildOptionsForm($form, $form_state); - $options = array(); - - if (isset($form['grouping'])) { - $options = $form['grouping'][0]['field']['#options']; - unset($options['']); - $form['search_fields'] = array( - '#type' => 'checkboxes', - '#title' => t('Search fields'), - '#options' => $options, - '#required' => TRUE, - '#default_value' => $this->options['search_fields'], - '#description' => t('Select the field(s) that will be searched when using the autocomplete widget.'), - '#weight' => -3, - ); - } + + $options = $this->displayHandler->getFieldLabels(TRUE); + $form['search_fields'] = array( + '#type' => 'checkboxes', + '#title' => t('Search fields'), + '#options' => $options, + '#required' => TRUE, + '#default_value' => $this->options['search_fields'], + '#description' => t('Select the field(s) that will be searched when using the autocomplete widget.'), + '#weight' => -3, + ); } /** * Overrides Drupal\views\Plugin\views\style\StylePluginBase\StylePluginBase::render(). */ public function render() { - $options = $this->displayHandler->getOption('entity_reference_options'); - - // Play nice with Views UI 'preview': if the view is not executed through - // Drupal\views\Plugin\entity_reference\selection\Views::getReferencableEntities(), - // just display the HTML. - if (empty($options)) { + if (!empty($this->view->live_preview)) { return parent::render(); } @@ -79,7 +81,7 @@ public function render() { $sets = $this->render_grouping($this->view->result, $this->options['grouping']); // Grab the alias of the 'id' field added by entity_reference_plugin_display. - $id_field_alias = $this->displayHandler->id_field_alias; + $id_field_alias = $this->view->storage->get('base_field'); // @todo We don't display grouping info for now. Could be useful for select // widget, though. @@ -97,6 +99,17 @@ public function render() { } /** + * Overrides Drupal\views\Plugin\views\display\PathPluginBase::preview(). + */ + public function preview() { + if (!empty($this->view->live_preview)) { + return '
' . check_plain($this->view->render()) . ''; + } + + return $this->view->render(); + } + + /** * Overrides Drupal\views\Plugin\views\style\StylePluginBase\StylePluginBase::even_empty(). */ function even_empty() { 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 2f0329d..aa2bc0a 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 @@ -12,6 +12,7 @@ use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface; +use Drupal\views\ViewsException; /** * Plugin implementation of the 'selection' entity_reference. @@ -52,8 +53,10 @@ public static function settingsForm($field, $instance) { $options = array(); foreach ($displays as $data) { list($view, $display_id) = $data; - if ($view->storage->base_table == $entity_info['base table']) { - $options[$view->storage->name . ':' . $display_id] = $view->storage->name . ' - ' . $view->storage->display[$display_id]['display_title']; + if ($view->storage->get('base_table') == $entity_info['base_table']) { + $name = $view->storage->get('name'); + $display = $view->storage->get('display'); + $options[$name . ':' . $display_id] = $name . ' - ' . $display[$display_id]['display_title']; } } @@ -114,9 +117,8 @@ protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $ // Check that the view is valid and the display still exists. $this->view = views_get_view($view_name); - if (!$this->view || !$this->view->storage->getDisplay($display_name) || !$this->view->access($display_name)) { - watchdog('entity_reference', 'The view %view_name is no longer eligible for the %field_name field.', array('%view_name' => $view_name, '%field_name' => $this->instance['label']), WATCHDOG_WARNING); - return FALSE; + if (!$this->view->access($display_name)) { + throw new ViewsException('The view %view_name is no longer eligible for the %field_name field.', array('%view_name' => $view_name, '%field_name' => $this->instance['label'])); } $this->view->setDisplay($display_name); @@ -145,9 +147,8 @@ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAI $return = array(); if ($result) { - $target_type = $this->field['settings']['target_type']; - $entities = entity_load_multiple($target_type, array_keys($result)); - foreach($entities as $entity) { + foreach($this->view->result as $row) { + $entity = $row->_entity; $return[$entity->bundle()][$entity->id()] = $result[$entity->id()]; } } @@ -159,7 +160,7 @@ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAI */ public function countReferencableEntities($match = NULL, $match_operator = 'CONTAINS') { $this->getReferencableEntities($match, $match_operator); - return $this->view->total_items; + return $this->view->pager->get_total_items(); } /** @@ -187,7 +188,7 @@ public function validateAutocompleteInput($input, &$element, &$form_state, $form /** * Implements Drupal\entity_reference\Plugin\Type\Selection\SelectionInterface::entityFieldQueryAlter(). */ - public function entityFieldQueryAlter(SelectInterface $query) { } + public function entityQueryAlter(SelectInterface $query) { } /** * Element validate; Check View is valid. diff --git a/core/modules/views/lib/Drupal/views/ViewsException.php b/core/modules/views/lib/Drupal/views/ViewsException.php new file mode 100644 index 0000000..8085db2 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/ViewsException.php @@ -0,0 +1,15 @@ +