Token.php

Same filename in this branch
  1. 9 core/lib/Drupal/Core/Utility/Token.php
Same filename in other branches
  1. 8.9.x core/lib/Drupal/Core/Render/Element/Token.php
  2. 8.9.x core/lib/Drupal/Core/Utility/Token.php
  3. 10 core/lib/Drupal/Core/Render/Element/Token.php
  4. 10 core/lib/Drupal/Core/Utility/Token.php
  5. 11.x core/lib/Drupal/Core/Render/Element/Token.php
  6. 11.x core/lib/Drupal/Core/Utility/Token.php

Namespace

Drupal\Core\Render\Element

File

core/lib/Drupal/Core/Render/Element/Token.php

View source
<?php

namespace Drupal\Core\Render\Element;

use Drupal\Core\Form\FormStateInterface;

/**
 * Stores token data in a hidden form field.
 *
 * This is generally used to protect against cross-site forgeries. A token
 * element is automatically added to each Drupal form by an implementation of
 * \Drupal\Core\Form\FormBuilderInterface::prepareForm() so you don't generally
 * have to add one yourself.
 *
 * @FormElement("token")
 */
class Token extends Hidden {
    
    /**
     * {@inheritdoc}
     */
    public function getInfo() {
        $class = static::class;
        return [
            '#input' => TRUE,
            '#pre_render' => [
                [
                    $class,
                    'preRenderHidden',
                ],
            ],
            '#theme' => 'input__hidden',
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
        if ($input !== FALSE && $input !== NULL) {
            // This should be a string, but allow other scalars since they might be
            // valid input in programmatic form submissions.
            return is_scalar($input) ? (string) $input : '';
        }
        return NULL;
    }

}

Classes

Title Deprecated Summary
Token Stores token data in a hidden form field.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.