You are here

function Field::add_self_tokens in Views (for Drupal 7) 8.3

Add any special tokens this field might use for itself.

This method is intended to be overridden by items that generate fields as a list. For example, the field that displays all terms on a node might have tokens for the tid and the term.

By convention, tokens should follow the format of [token-subtoken] where token is the field ID and subtoken is the field. If the field ID is terms, then the tokens might be [terms-tid] and [terms-name].

Overrides FieldPluginBase::add_self_tokens

File

lib/Views/field/Plugin/views/field/Field.php, line 808
Definition of Views\field\Plugin\views\field\Field.

Class

Field
A field that displays fieldapi fields.

Namespace

Views\field\Plugin\views\field

Code

function add_self_tokens(&$tokens, $item) {
  $field = $this->field_info;
  foreach ($field['columns'] as $id => $column) {

    // Use filter_xss_admin because it's user data and we can't be sure it is safe.
    // We know nothing about the data, though, so we can't really do much else.
    if (isset($item['raw'])) {

      // If $item['raw'] is an array then we can use as is, if it's an object
      // we cast it to an array, if it's neither, we can't use it.
      $raw = is_array($item['raw']) ? $item['raw'] : (is_object($item['raw']) ? (array) $item['raw'] : NULL);
    }
    if (isset($raw) && isset($raw[$id]) && is_scalar($raw[$id])) {
      $tokens['[' . $this->options['id'] . '-' . $id . ']'] = filter_xss_admin($raw[$id]);
    }
    else {

      // Take sure that empty values are replaced as well.
      $tokens['[' . $this->options['id'] . '-' . $id . ']'] = '';
    }
  }
}