viewsTokenReplace example



    $more_link = '';
    if (!empty($alter['trim']) && !empty($alter['max_length'])) {
      $length = strlen($value);
      $value = $this->renderTrimText($alter$value);
      if ($this->options['alter']['more_link'] && strlen($value) < $length) {
        $tokens = $this->getRenderTokens($alter);
        $more_link_text = $this->options['alter']['more_link_text'] ? $this->options['alter']['more_link_text'] : $this->t('more');
        $more_link_text = strtr(Xss::filterAdmin($more_link_text)$tokens);
        $more_link_path = $this->options['alter']['more_link_path'];
        $more_link_path = strip_tags(Html::decodeEntities($this->viewsTokenReplace($more_link_path$tokens)));

        // Make sure that paths which were run through URL generation work as         // well.         $base_path = base_path();
        // Checks whether the path starts with the base_path.         if (str_starts_with($more_link_path$base_path)) {
          $more_link_path = mb_substr($more_link_pathmb_strlen($base_path));
        }

        // @todo Views should expect and store a leading /. See         // https://www.drupal.org/node/2423913.
$this->testPluginBase = new TestPluginBase();
  }

  /** * Tests that the token replacement in views works correctly. */
  public function testViewsTokenReplace() {
    $text = '{{ langcode__value }} means {{ langcode }}';
    $tokens = ['{{ langcode }}' => Markup::create('English'), '{{ langcode__value }}' => 'en'];

    $result = \Drupal::service('renderer')->executeInRenderContext(new RenderContext()function D) use ($text$tokens) {
      return $this->testPluginBase->viewsTokenReplace($text$tokens);
    });

    $this->assertSame('en means English', $result);
  }

  /** * Tests that the token replacement in views works correctly with dots. */
  public function testViewsTokenReplaceWithDots() {
    $text = '{{ argument.first }} comes before {{ argument.second }}';
    $tokens = ['{{ argument.first }}' => 'first', '{{ argument.second }}' => 'second'];

    
// Return the display URL if there is no custom URL.     if ($this->getOption('link_display') !== 'custom_url' || empty($path)) {
      return $this->view->getUrl(NULL, $this->display['id']);
    }

    $parts = UrlHelper::parse($path);
    $options = $parts;
    $tokens = $this->getArgumentsTokens();

    // If there are no tokens there is nothing else to do.     if (!empty($tokens)) {
      $parts['path'] = $this->viewsTokenReplace($parts['path']$tokens);
      $parts['fragment'] = $this->viewsTokenReplace($parts['fragment']$tokens);

      // Handle query parameters where the key is part of an array.       // For example, f[0] for facets.       array_walk_recursive($parts['query']function D&$value) use ($tokens) {
        $value = $this->viewsTokenReplace($value$tokens);
      });
      $options = $parts;
    }

    $path = $options['path'];
    
/** * Take a value and apply token replacement logic to it. */
  public function tokenizeValue($value$row_index) {
    if (str_contains($value, '{{')) {
      // Row tokens might be empty, for example for node row style.       $tokens = $this->rowTokens[$row_index] ?? [];
      if (!empty($this->view->build_info['substitutions'])) {
        $tokens += $this->view->build_info['substitutions'];
      }

      $value = $this->viewsTokenReplace($value$tokens);
    }
    else {
      // ::viewsTokenReplace() will run Xss::filterAdmin on the       // resulting string. We do the same here for consistency.       $value = Xss::filterAdmin($value);
    }
    return $value;
  }

  /** * Determines if the style plugin is rendered even if the view is empty. */
$title = $this->view->field[$field]->last_render_text;
      $path = '';
      $url = NULL;
      if (!empty($this->view->field[$field]->options['alter']['path'])) {
        $path = $this->view->field[$field]->options['alter']['path'];
      }
      elseif (!empty($this->view->field[$field]->options['alter']['url']) && $this->view->field[$field]->options['alter']['url'] instanceof UrlObject) {
        $url = $this->view->field[$field]->options['alter']['url'];
      }
      // Make sure that tokens are replaced for this paths as well.       $tokens = $this->getRenderTokens([]);
      $path = strip_tags(Html::decodeEntities($this->viewsTokenReplace($path$tokens)));

      $links[$field] = [
        'url' => $path ? UrlObject::fromUri('internal:/' . $path) : $url,
        'title' => $title,
      ];
      if (!empty($this->options['destination'])) {
        $links[$field]['query'] = $this->redirectDestination->getAsArray();
      }
    }

    return $links;
  }
Home | Imprint | This part of the site doesn't use cookies.