You are here

class TokenFieldRender in Token 8

Hierarchy

Expanded class hierarchy of TokenFieldRender

File

src/TokenFieldRender.php, line 8

Namespace

Drupal\token
View source
class TokenFieldRender implements TrustedCallbackInterface {

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'preRender',
    ];
  }

  /**
   * Pre-render callback for field output used with tokens.
   */
  public static function preRender($elements) {

    // Remove the field theme hook, attachments, and JavaScript states.
    unset($elements['#theme']);
    unset($elements['#states']);
    unset($elements['#attached']);

    // Prevent multi-value fields from appearing smooshed together by appending
    // a join suffix to all but the last value.
    $deltas = Element::getVisibleChildren($elements);
    $count = count($deltas);
    if ($count > 1) {
      $join = isset($elements['#token_options']['join']) ? $elements['#token_options']['join'] : ", ";
      foreach ($deltas as $index => $delta) {

        // Do not add a suffix to the last item.
        if ($index < $count - 1) {
          $elements[$delta] += [
            '#suffix' => $join,
          ];
        }
      }
    }
    return $elements;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TokenFieldRender::preRender public static function Pre-render callback for field output used with tokens.
TokenFieldRender::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.