From 6b40833ea748b3836727507e3a634c199d6c86cf Mon Sep 17 00:00:00 2001
From: Axel Rutz <axel.rutz@clever-systems.net>
Date: Wed, 18 Sep 2013 02:56:43 +0200
Subject: [PATCH] Issue #2010898 by Volx, axel.rutz: Added Pass entity tokens
 to view for entity selection.

---
 ...ntityReference_SelectionHandler_Views.class.php |   24 ++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/plugins/selection/EntityReference_SelectionHandler_Views.class.php b/plugins/selection/EntityReference_SelectionHandler_Views.class.php
index 1b036a7..684c4ec 100644
--- a/plugins/selection/EntityReference_SelectionHandler_Views.class.php
+++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php
@@ -9,18 +9,19 @@ 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);
   }
 
-  protected function __construct($field, $instance) {
+  protected function __construct($field, $instance, $entity = NULL) {
     $this->field = $field;
     $this->instance = $instance;
+    $this->entity = $entity;
   }
 
   /**
    * Implements EntityReferenceHandler::settingsForm().
    */
-  public static function settingsForm($field, $instance) {
+  public static function settingsForm($field, $instance, $entity = NULL) {
     $view_settings = empty($field['settings']['handler_settings']['view']) ? '' : $field['settings']['handler_settings']['view'];
     $displays = views_get_applicable_views('entityreference display');
     // Filter views that list the entity type we want, and group the separate
@@ -57,8 +58,19 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio
         '#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. Tokens like [node:nid] are supported.'),
+        '#maxlength' => 1024, // Tokens may be more verbose than 128 chars.
       );
+      if (!module_exists('token')) {
+        $form['view']['args_notokens']['#markup'] = t('A full listing of tokens may be listed here by installing the <a href="http://drupal.org/project/token">Token module</a>.');
+      }
+      else {
+        $form['view']['args_token_tree'] = array(
+          '#theme' => 'token_tree',
+          '#token_types' => array($instance['entity_type']),
+          '#dialog' => TRUE,
+        );
+      }
     }
     else {
       $form['view']['no_view_help'] = array(
@@ -105,6 +117,8 @@ 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'];
+    $data = array($this->field['settings']['target_type'] => $this->entity);
+    $args = array_map(function($args) use($data) {return token_replace($args, $data);}, $args);
     $result = array();
     if ($this->initializeView($match, $match_operator, $limit)) {
       // Get the results.
@@ -134,6 +148,8 @@ 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'];
+    $data = array($this->field['settings']['target_type'] => $this->entity);
+    $args = array_map(function($args) use($data) {return token_replace($args, $data);}, $args);
     $result = array();
     if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) {
       // Get the results.
-- 
1.7.9.5

