diff --git a/plugins/selection/EntityReference_SelectionHandler_Views.class.php b/plugins/selection/EntityReference_SelectionHandler_Views.class.php index 1b036a7..7f759ea 100644 --- a/plugins/selection/EntityReference_SelectionHandler_Views.class.php +++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php @@ -9,12 +9,16 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio * Implements EntityReferenceHandler::getInstance(). */ public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) { - return new EntityReference_SelectionHandler_Views($field, $instance); + return new EntityReference_SelectionHandler_Views($field, $instance, $entity_type, $entity); } - protected function __construct($field, $instance) { + protected function __construct($field, $instance, $entity_type, $entity) { $this->field = $field; $this->instance = $instance; + $this->entity = $entity; + // Get the entity token type of the entity type. + $entity_info = entity_get_info($entity_type); + $this->entity_type_token = isset($entity_info['token type']) ? $entity_info['token type'] : $entity_type; } /** @@ -52,13 +56,36 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio ); $default = !empty($view_settings['args']) ? implode(', ', $view_settings['args']) : ''; + $description = t('Provide a comma separated list of arguments to pass to the view.') . '
' . t('This field supports tokens.'); + + if (!module_exists('token')) { + $description .= '
' . t('Install the token module to get more tokens and display available once.', array('@url' => 'http://drupal.org/project/token')); + } + $form['view']['args'] = array( '#type' => 'textfield', '#title' => t('View arguments'), '#default_value' => $default, '#required' => FALSE, - '#description' => t('Provide a comma separated list of arguments to pass to the view.'), + '#description' => $description, ); + if (module_exists('token')) { + // Get the token type for the entity type our field is in (a type 'taxonomy_term' has a 'term' type token). + $instance_entity_info = entity_get_info($instance['entity_type']); + $token_type = isset($instance_entity_info['token type']) ? $instance_entity_info['token type'] : $instance['entity_type']; + + $form['view']['tokens'] = array( + '#theme' => 'token_tree', + // The token types that have specific context. Can be multiple token types like 'term' and/or 'user'. + '#token_types' => array($token_type), + // A boolean TRUE or FALSE whether to include 'global' context tokens like [current-user:*] + // or [site:*]. Defaults to TRUE. + '#global_types' => TRUE, + // A boolean whether to include the 'Click this token to insert in into the the focused textfield' + // JavaScript functionality. Defaults to TRUE. + '#click_insert' => TRUE, + ); + } } else { $form['view']['no_view_help'] = array( @@ -104,7 +131,7 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio */ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) { $display_name = $this->field['settings']['handler_settings']['view']['display_name']; - $args = $this->field['settings']['handler_settings']['view']['args']; + $args = $this->handleArgs($this->field['settings']['handler_settings']['view']['args']); $result = array(); if ($this->initializeView($match, $match_operator, $limit)) { // Get the results. @@ -133,7 +160,7 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio function validateReferencableEntities(array $ids) { $display_name = $this->field['settings']['handler_settings']['view']['display_name']; - $args = $this->field['settings']['handler_settings']['view']['args']; + $args = $this->handleArgs($this->field['settings']['handler_settings']['view']['args']); $result = array(); if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) { // Get the results. @@ -163,6 +190,23 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio public function entityFieldQueryAlter(SelectQueryInterface $query) { } + + /** + * Handles arguments for views. + * + * If the token module exists, replace the token. + * + * @param array $args Usually $this->field['settings']['handler_settings']['view']['args'] + * + * @return + * Array with arguments + */ + protected handleArgs($args) { + if (module_exists('token')) { + return array_map('token_replace', $args, array(array($this->entity_type_token => $this->entity))); + } + return $args; + } }