commit c42cbf7f91ebac930f8f665c744e8cbd7d6fcc87
Author: Tamás Brückner <tamas.bruckner@integralvision.hu>
Date:   Thu Jun 15 15:46:40 2017 +0200

    Entity reference wies with token

diff --git a/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php b/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
index 3e10f4a..b97bc3c 100644
--- a/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
+++ b/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
@@ -127,6 +127,20 @@ public static function processEntityAutocomplete(array &$element, FormStateInter
     // Store the selection settings in the key/value store and pass a hashed key
     // in the route parameters.
     $selection_settings = isset($element['#selection_settings']) ? $element['#selection_settings'] : [];
+
+    // Put entity into settings.
+    $form_object = $form_state->getFormObject();
+    if (isset($form_object)) {
+      $entity = $form_object->getEntity();
+      if (isset($entity)) {
+        $storage = $form_state->getStorage();
+        if (isset($storage['group'])) {
+          $entity->parent_group = $storage['group'];
+        }
+        $selection_settings['entity'] = $entity;
+      }
+    }
+
     $data = serialize($selection_settings) . $element['#target_type'] . $element['#selection_handler'];
     $selection_settings_key = Crypt::hmacBase64($data, Settings::getHashSalt());
 
diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php
index bb773cd..bb483b5 100644
--- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/OptionsWidgetBase.php
@@ -43,6 +43,25 @@ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInter
    * {@inheritdoc}
    */
   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
+    // Add form object to field definition.
+    $field_definition = $items->getFieldDefinition();
+
+    // Put entity into settings.
+    $form_object = $form_state->getFormObject();
+    if (isset($form_object)) {
+      $entity = $form_object->getEntity();
+      if (isset($entity)) {
+        $storage = $form_state->getStorage();
+        if (isset($storage['group'])) {
+          $entity->parent_group = $storage['group'];
+        }
+
+        $setting = $field_definition->getSetting('handler_settings');
+        $setting['entity'] = $entity;
+        $field_definition->setSetting('handler_settings', $setting);
+      }
+    }
+
     // Prepare some properties for the child methods to build the actual form
     // element.
     $this->required = $element['#required'];
diff --git a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php
index 05ac9b9..d813436 100644
--- a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php
+++ b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php
@@ -213,7 +213,7 @@ protected function initializeView($match = NULL, $match_operator = 'CONTAINS', $
   public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
     $handler_settings = $this->configuration['handler_settings'];
     $display_name = $handler_settings['view']['display_name'];
-    $arguments = $handler_settings['view']['arguments'];
+    $arguments = $this->handleArgs($handler_settings['view']['arguments']);
     $result = [];
     if ($this->initializeView($match, $match_operator, $limit)) {
       // Get the results.
@@ -244,7 +244,7 @@ public function countReferenceableEntities($match = NULL, $match_operator = 'CON
   public function validateReferenceableEntities(array $ids) {
     $handler_settings = $this->configuration['handler_settings'];
     $display_name = $handler_settings['view']['display_name'];
-    $arguments = $handler_settings['view']['arguments'];
+    $arguments = $this->handleArgs($handler_settings['view']['arguments']);
     $result = [];
     if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) {
       // Get the results.
@@ -288,4 +288,45 @@ public static function settingsFormValidate($element, FormStateInterface $form_s
    */
   public function entityQueryAlter(SelectInterface $query) { }
 
+  /**
+   * 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();
+    $options = array(
+      'clear' => TRUE,
+    );
+
+    $data = array();
+    if (isset($this->configuration['handler_settings']['entity'])) {
+      $entity = $this->configuration['handler_settings']['entity'];
+      $entity_type = $entity->getEntityTypeId();
+      if (!isset($data[$entity_type])) {
+        $data[$entity_type] = $entity;
+      }
+    }
+
+    if (isset($this->configuration['entity'])) {
+      $entity = $this->configuration['entity'];
+      $entity_type = $entity->getEntityTypeId();
+      if (!isset($data[$entity_type])) {
+        $data[$entity_type] = $entity;
+      }
+    }
+
+    // Replace tokens for each argument.
+    foreach ($args as $key => $arg) {
+      $args[$key] = $token_service->replace($arg, $data, $options);
+    }
+
+    return $args;
+  }
 }
