You are here

public function ViewsAddButtonField::tokenizeValue in Views Add Button 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/views/field/ViewsAddButtonField.php \Drupal\views_add_button\Plugin\views\field\ViewsAddButtonField::tokenizeValue()

Replaces a value with tokens from the last field.

This function actually figures out which field was last and uses its tokens so they will all be available.

Parameters

string $value: The raw string.

bool $row_index: The index of current row.

Overrides FieldPluginBase::tokenizeValue

2 calls to ViewsAddButtonField::tokenizeValue()
ViewsAddButtonField::checkButtonAccess in src/Plugin/views/field/ViewsAddButtonField.php
ViewsAddButtonField::render in src/Plugin/views/field/ViewsAddButtonField.php
Renders the field.

File

src/Plugin/views/field/ViewsAddButtonField.php, line 345

Class

ViewsAddButtonField
Defines a views field plugin.

Namespace

Drupal\views_add_button\Plugin\views\field

Code

public function tokenizeValue($value, $row_index = NULL) {
  if (strpos($value, '{{') !== FALSE) {
    $fake_item = [
      'alter_text' => TRUE,
      'text' => $value,
    ];

    // Use isset() because empty() will trigger on 0 and 0 is
    // the first row.
    if (isset($row_index) && isset($this->view->style_plugin->render_tokens[$row_index])) {
      $tokens = $this->view->style_plugin->render_tokens[$row_index];
    }
    elseif (!empty($tokens = $this
      ->getRenderTokens($value))) {

      // We defined $tokens in the if statement.
    }
    else {

      // Get tokens from the last field.
      $last_field = end($this->view->field);
      if (isset($last_field->last_tokens)) {
        $tokens = $last_field->last_tokens;
      }
      else {
        $tokens = $last_field
          ->getRenderTokens($fake_item);
      }
    }
    if (empty($this->options['preserve_tags'])) {
      $value = strip_tags($this
        ->renderAltered($fake_item, $tokens));
    }
    else {
      $ts = explode(' ', $this->options['preserve_tags']);
      $tags = [];
      foreach ($ts as $t) {
        $tags[] = trim($t);
      }
      $value = Xss::filter($this
        ->renderAltered($fake_item, $tokens), $tags);
    }
    if (!empty($this->options['alter']['trim_whitespace'])) {
      $value = trim($value);
    }
  }
  return $value;
}