diff --git a/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php b/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php index c3cf1ac..9a186ef 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php +++ b/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php @@ -85,7 +85,7 @@ public static function settingsForm(&$field, &$instance) { '#title' => t('View arguments'), '#default_value' => $default, '#required' => FALSE, - '#description' => t('Provide a comma separated list of arguments to pass to the view.'), + '#description' => t('Provide a comma separated list of arguments to pass to the view.') . '
' . t('This field supports tokens.'), ); } else { @@ -144,11 +144,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']; - $arguments = $this->instance['settings']['handler_settings']['view']['arguments']; + $arguments = $this->handleArgs($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, $arguments); + $result = $this->view->executeDisplay($display_name, (!array_filter($arguments) ? array() : $arguments)); } $return = array(); @@ -174,11 +174,11 @@ public function countReferencableEntities($match = NULL, $match_operator = 'CONT */ public function validateReferencableEntities(array $ids) { $display_name = $this->instance['settings']['handler_settings']['view']['display_name']; - $arguments = $this->instance['settings']['handler_settings']['view']['arguments']; + $arguments = $this->handleArgs($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, $arguments); + $entities = $this->view->executeDisplay($display_name, (!array_filter($arguments) ? array() : $arguments)); $result = array_keys($entities); } return $result; @@ -224,4 +224,35 @@ public function settingsFormValidate($element, &$form_state, $form) { $value = array('view_name' => $view, 'display_name' => $display, 'arguments' => $arguments); form_set_value($element, $value, $form_state); } + + /** + * Handles replacing tokens in arguments for views. + * + * Replaces tokens using Token::replace. + * + * @param array $args + * An array of arguments that may contain tokens. + * + * @return array + * The arguments to be sent to the View. + */ + protected function handleArgs($args) { + $token_service = \Drupal::token(); + $language_interface = language(LANGUAGE_TYPE_INTERFACE); + $options = array( + 'clear' => TRUE, + 'language' => $language_interface, + ); + + $data = array(); + if ($this->entity) { + $data = array($this->entity->entityType() => $this->entity); + } + + // Replace tokens for each argument. + foreach ($args as $key => $arg) { + $args[$key] = $token_service->replace($arg, $data, $options); + } + return $args; + } }