commit e95a0a01706eb8e17f2f3393809426cacce4b2cf
Author: frega <fl@flink-solutions.de>
Date:   Mon Dec 9 17:33:50 2019 -0700

    Reroll patch https://www.drupal.org/files/issues/2019-08-08/drupal-dynamic_views_arguments_via_tokens-1699378-189.patch

diff --git a/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php b/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
index 10421abaae..ff31fbe258 100644
--- a/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
+++ b/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
@@ -4,6 +4,7 @@ namespace Drupal\Core\Entity\Element;

 use Drupal\Component\Utility\Crypt;
 use Drupal\Component\Utility\Tags;
+use Drupal\Core\Entity\EntityForm;
 use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface;
 use Drupal\Core\Entity\EntityReferenceSelection\SelectionWithAutocreateInterface;
@@ -127,6 +128,20 @@ class EntityAutocomplete extends Textfield {
     // 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) && $form_object instanceof EntityForm) {
+      $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 a559d9e870..bc17d02715 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 @@ abstract class OptionsWidgetBase extends WidgetBase {
    * {@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) && $form_object instanceof EntityForm) {
+      $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 0f75ebe430..4f6aba70b5 100644
--- a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php
+++ b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php
@@ -12,6 +12,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\Core\Render\RendererInterface;
 use Drupal\Core\Session\AccountInterface;
 use Drupal\Core\Url;
+use Drupal\Core\Utility\Token;
 use Drupal\views\Render\ViewsRenderPipelineMarkup;
 use Drupal\views\Views;
 use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -70,6 +71,13 @@ class ViewsSelection extends SelectionPluginBase implements ContainerFactoryPlug
    */
   protected $renderer;

+  /**
+   * The token service.
+   *
+   * @var \Drupal\Core\Utility\Token
+   */
+  protected $token;
+
   /**
    * Constructs a new ViewsSelection object.
    *
@@ -87,8 +95,10 @@ class ViewsSelection extends SelectionPluginBase implements ContainerFactoryPlug
    *   The current user.
    * @param \Drupal\Core\Render\RendererInterface|null $renderer
    *   The renderer.
+   * @param \Drupal\Core\Utility\Token $token
+   *   The token replacement instance.
    */
-  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user, RendererInterface $renderer = NULL) {
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user, RendererInterface $renderer = NULL, Token $token) {
     parent::__construct($configuration, $plugin_id, $plugin_definition);

     $this->entityTypeManager = $entity_type_manager;
@@ -100,6 +110,7 @@ class ViewsSelection extends SelectionPluginBase implements ContainerFactoryPlug
       $renderer = \Drupal::service('renderer');
     }
     $this->renderer = $renderer;
+    $this->token = $token;
   }

   /**
@@ -113,7 +124,8 @@ class ViewsSelection extends SelectionPluginBase implements ContainerFactoryPlug
       $container->get('entity_type.manager'),
       $container->get('module_handler'),
       $container->get('current_user'),
-      $container->get('renderer')
+      $container->get('renderer'),
+      $container->get('token')
     );
   }

@@ -176,7 +188,7 @@ class ViewsSelection extends SelectionPluginBase implements ContainerFactoryPlug
         '#title' => $this->t('View arguments'),
         '#default_value' => $default,
         '#required' => FALSE,
-        '#description' => $this->t('Provide a comma separated list of arguments to pass to the view.'),
+        '#description' => $this->t('Provide a comma separated list of arguments to pass to the view.') . '<br />' . $this->t('This field supports tokens.'),
       ];
     }
     else {
@@ -259,13 +271,17 @@ class ViewsSelection extends SelectionPluginBase implements ContainerFactoryPlug
    *   indicates no limiting.
    * @param array|null $ids
    *   Array of entity IDs. Defaults to NULL.
+   * @param bool $bubble_cacheable_metadata
+   *   If TRUE the cachebility metadata emitted during token replacement will be
+   *   bubbled to the render context. If FALSE then the it will not in order to
+   *   prevent leaked cacheability metadata during early rendering.
    *
    * @return array
    *   The results.
    */
-  protected function getDisplayExecutionResults(string $match = NULL, string $match_operator = 'CONTAINS', int $limit = 0, array $ids = NULL) {
+  protected function getDisplayExecutionResults(string $match = NULL, string $match_operator = 'CONTAINS', int $limit = 0, array $ids = NULL, $bubble_cacheable_metadata = TRUE): array {
     $display_name = $this->getConfiguration()['view']['display_name'];
-    $arguments = $this->getConfiguration()['view']['arguments'];
+    $arguments = $this->handleArgs($this->getConfiguration()['view']['arguments'], $bubble_cacheable_metadata);
     $results = [];
     if ($this->initializeView($match, $match_operator, $limit, $ids)) {
       $results = $this->view->executeDisplay($display_name, $arguments);
@@ -315,7 +331,7 @@ class ViewsSelection extends SelectionPluginBase implements ContainerFactoryPlug
    * {@inheritdoc}
    */
   public function validateReferenceableEntities(array $ids) {
-    $entities = $this->getDisplayExecutionResults(NULL, 'CONTAINS', 0, $ids);
+    $entities = $this->getDisplayExecutionResults(NULL, 'CONTAINS', 0, $ids, FALSE);
     $result = [];
     if ($entities) {
       $result = array_keys($entities);
@@ -356,4 +372,56 @@ class ViewsSelection extends SelectionPluginBase implements ContainerFactoryPlug
     $form_state->setValueForElement($element, $value);
   }

+  /**
+   * Handles replacing tokens in arguments for views.
+   *
+   * Replaces tokens using Token::replace.
+   *
+   * @param array $args
+   *   An array of arguments that may contain tokens.
+   * @param bool $bubble_cacheable_metadata
+   *   If TRUE the cachebility metadata emitted during token replacement will be
+   *   bubbled to the render context. If FALSE then the it will not in order to
+   *   prevent leaked cacheability metadata during early rendering.
+   *
+   * @return array
+   *   The arguments to be sent to the View.
+   */
+  protected function handleArgs($args, $bubble_cacheable_metadata = TRUE) {
+    $entities = [];
+    if (isset($this->configuration['handler_settings']['entity'])) {
+      $entities[] = $this->configuration['handler_settings']['entity'];
+    }
+    if (isset($this->configuration['entity'])) {
+      $entities[] = $this->configuration['entity'];
+    }
+
+    $data = [];
+    foreach ($entities as $entity) {
+      $token_type = $entity->getEntityTypeId();
+
+      // Taxonomy term token type doesn't match the entity type's machine name.
+      if ($token_type === 'taxonomy_term') {
+        $token_type = 'term';
+      }
+
+      if (!isset($data[$token_type])) {
+        $data[$token_type] = $entity;
+      }
+    }
+
+    // If cacheability metadata should not be bubbled then we need to pass in
+    // our own BubbleableMetadata which will prevent any metadata generated from
+    // automatically bubbling to the render context.
+    $bubbleable_metadata = $bubble_cacheable_metadata ? NULL : new BubbleableMetadata();
+
+    // Replace tokens for each argument.
+    foreach ($args as $key => $arg) {
+      $value = $this->token->replace($arg, $data, ['clear' => TRUE], $bubbleable_metadata);
+      $args[$key] = !empty($value) ? $value : NULL;
+    }
+
+    return $args;
+  }
+
 }
