diff --git a/plugins/selection/EntityReference_SelectionHandler_Views.class.php b/plugins/selection/EntityReference_SelectionHandler_Views.class.php index 92fa7a1..95e6651 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,31 @@ 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', + '#token_types' => array( $token_type ) , // The token types that have specific context. Can be multiple token types like 'term' and/or 'user' + '#global_types' => TRUE, // A boolean TRUE or FALSE whether to include 'global' context tokens like [current-user:*] or [site:*]. Defaults to TRUE. + '#click_insert' => TRUE, // A boolean whether to include the 'Click this token to insert in into the the focused textfield' JavaScript functionality. Defaults to TRUE. + ); + } } else { $form['view']['no_view_help'] = array( @@ -104,7 +126,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 = array_map( 'token_replace', $this->field['settings']['handler_settings']['view']['args'], array( array($this->entity_type_token => $this->entity))); $result = array(); if ($this->initializeView($match, $match_operator, $limit)) { // Get the results. @@ -123,7 +145,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 = array_map( 'token_replace', $this->field['settings']['handler_settings']['view']['args'], array( array($this->entity_type_token => $this->entity))); $result = array(); if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) { // Get the results.