diff --git a/plugins/selection/EntityReference_SelectionHandler_Views.class.php b/plugins/selection/EntityReference_SelectionHandler_Views.class.php index 1b036a7..d5009a4 100644 --- a/plugins/selection/EntityReference_SelectionHandler_Views.class.php +++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php @@ -9,12 +9,13 @@ 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; } /** @@ -52,13 +53,41 @@ 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, + '#maxlength' => '512', ); + + if (module_exists('token')) { + $default = !empty($view_settings['cleanup']) ? $view_settings['cleanup'] : 0; + $form['view']['cleanup'] = array( + '#type' => 'checkbox', + '#title' => t('Remove empty arguments'), + '#default' => $default, + '#description' => '

' . t('Remove empty arguments after token replacement. This is useful if your view accepts optional arguments.') . '

', + ); + + // Get the token type for the entity type our field is in (a type 'taxonomy_term' has a 'term' type token). + $info = entity_get_info($instance['entity_type']); + + $form['view']['tokens'] = array( + '#theme' => 'token_tree', + '#token_types' => array($info['token type']), + '#global_types' => TRUE, + '#click_insert' => TRUE, + '#dialog' => TRUE, + ); + } } else { $form['view']['no_view_help'] = array( @@ -104,7 +133,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 +162,13 @@ 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']; + if (isset($this->field['settings']['handler_settings']['view']['cleanup'])) { + $cleanup = $this->field['settings']['handler_settings']['view']['cleanup']; + } + else { + $cleanup = FALSE; + } + $args = $this->handleArgs($this->field['settings']['handler_settings']['view']['args'], $cleanup); $result = array(); if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) { // Get the results. @@ -163,7 +198,51 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio public function entityFieldQueryAlter(SelectQueryInterface $query) { } + + /** + * Handles arguments for views. + * + * Replaces tokens using token_replace(). + * + * @param array $args + * Usually $this->field['settings']['handler_settings']['view']['args']. + * + * @param $cleanup + * $this->field['settings']['handler_settings']['view']['cleanup']. + * + * @return array + * The arguments to be send to the View. + */ + protected function handleArgs($args, $cleanup = FALSE) { + // Parameters for token_replace(). + $data = array(); + $options = array('clear' => TRUE); + + if ($entity = $this->entity) { + // D7 HACK: For new entities, entity and revision id are not set. This leads to + // * token replacement emitting PHP warnings + // * views choking on empty arguments + // We workaround this by filling in '0' for these IDs + // and use a clone to leave no traces of our unholy doings. + $info = entity_get_info($this->instance['entity_type']); + if (!isset($entity->{$info['entity keys']['id']})) { + $entity = clone $entity; + $entity->{$info['entity keys']['id']} = '0'; + if (!empty($info['entity keys']['revision'])) { + $entity->{$info['entity keys']['revision']} = '0'; + } + } + $data[$info['token type']] = $entity; + } + // Replace tokens for each argument. + foreach ($args as $key => $arg) { + $args[$key] = token_replace($arg, $data, $options); + if ($cleanup && empty($args[$key])) + unset($args[$key]); + } + return $args; + } } function entityreference_view_settings_validate($element, &$form_state, $form) {