Token.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. use Drupal\Core\Form\FormStateInterface;
  4. /**
  5. * Stores token data in a hidden form field.
  6. *
  7. * This is generally used to protect against cross-site forgeries. A token
  8. * element is automatically added to each Drupal form by an implementation of
  9. * \Drupal\Core\Form\FormBuilderInterface::prepareForm() so you don't generally
  10. * have to add one yourself.
  11. *
  12. * @FormElement("token")
  13. */
  14. class Token extends Hidden {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function getInfo() {
  19. $class = get_class($this);
  20. return [
  21. '#input' => TRUE,
  22. '#pre_render' => [
  23. [$class, 'preRenderHidden'],
  24. ],
  25. '#theme' => 'input__hidden',
  26. ];
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  32. if ($input !== FALSE && $input !== NULL) {
  33. // This should be a string, but allow other scalars since they might be
  34. // valid input in programmatic form submissions.
  35. return is_scalar($input) ? (string) $input : '';
  36. }
  37. return NULL;
  38. }
  39. }