You are here

public function View::getTokenInfo in Views Field View 8

Get available field tokens, code/logic stolen from views_handler_field.inc.

Return value

string A full HTML string, containing a list of available tokens.

1 call to View::getTokenInfo()
View::buildOptionsForm in src/Plugin/views/field/View.php
Default options form that provides the label widget that all fields should have.

File

src/Plugin/views/field/View.php, line 367

Class

View
Plugin annotation @ViewsField("view");

Namespace

Drupal\views_field_view\Plugin\views\field

Code

public function getTokenInfo() {
  $output = [];

  // Get a list of the available fields and arguments for token replacement.
  $options = [];
  foreach ($this->view->display_handler
    ->getHandlers('field') as $field => $handler) {
    $options[(string) $this
      ->t('Fields')]["{{ raw_fields.{$field} }}"] = $handler
      ->adminLabel() . ' (' . $this
      ->t('raw') . ')';
    $options[(string) $this
      ->t('Fields')]["{{ fields.{$field} }}"] = $handler
      ->adminLabel() . ' (' . $this
      ->t('rendered') . ')';

    // We only use fields up to (and including) this one.
    if ($field == $this->options['id']) {
      break;
    }
  }

  // This lets us prepare the key as we want it printed.
  $count = 0;
  foreach ($this->view->display_handler
    ->getHandlers('argument') as $id => $handler) {
    $options[(string) $this
      ->t('Arguments')]["{{ arguments.{$id} }}"] = $this
      ->t('@argument title', [
      '@argument' => $handler
        ->adminLabel(),
    ]);
    $options[(string) $this
      ->t('Arguments')]["{{ raw_arguments.{$id} }}"] = $this
      ->t('@argument input', [
      '@argument' => $handler
        ->adminLabel(),
    ]);
  }
  $this
    ->documentSelfTokens($options[(string) $this
    ->t('Fields')]);

  // We have some options, so make a list.
  if (!empty($options)) {
    $items = [];
    foreach (array_keys($options) as $type) {
      if (!empty($options[$type])) {
        foreach ($options[$type] as $key => $value) {
          $items[] = $key . ' == ' . $value;
        }
      }
    }
    $output = [
      '#theme' => 'item_list',
      '#items' => $items,
      '#type' => $type,
      '#prefix' => '<p>' . $this
        ->t('The following tokens are available
              for this field. Note that due to rendering order, you cannot use
              fields that come after this field; if you need a field that is not
              listed here, re-arrange  your fields.') . '</p>',
      '#suffix' => '<p><em>' . $this
        ->t('Using rendered tokens ("fields" / "arguments") can
              cause unexpected behaviour, as this will use the last output of
              the field. This could be re written output also. If no prefix is
              used in the token pattern, "raw_fields" / "raw_arguments" will be used as a default.') . '</em></p>',
    ];
  }
  else {
    $output = [
      '#markup' => '<p>' . $this
        ->t('You must add some additional fields to
          this display before using this field. These fields may be marked as
          <em>Exclude from display</em> if you prefer. Note that due to
          rendering order,you cannot use fields that come after this field; if
          you need a field not listed here, rearrange your fields.') . '</p>',
    ];
  }
  return $output;
}