Hidden.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. use Drupal\Core\Render\Element;
  4. /**
  5. * Provides a form element for an HTML 'hidden' input element.
  6. *
  7. * Specify either #default_value or #value but not both.
  8. *
  9. * Properties:
  10. * - #default_value: The initial value of the form element. JavaScript may
  11. * alter the value prior to submission.
  12. * - #value: The value of the form element. The Form API ensures that this
  13. * value remains unchanged by the browser.
  14. *
  15. * Usage example:
  16. * @code
  17. * $form['entity_id'] = array('#type' => 'hidden', '#value' => $entity_id);
  18. * @endcode
  19. *
  20. * @see \Drupal\Core\Render\Element\Value
  21. *
  22. * @FormElement("hidden")
  23. */
  24. class Hidden extends FormElement {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getInfo() {
  29. $class = get_class($this);
  30. return [
  31. '#input' => TRUE,
  32. '#process' => [
  33. [$class, 'processAjaxForm'],
  34. ],
  35. '#pre_render' => [
  36. [$class, 'preRenderHidden'],
  37. ],
  38. '#theme' => 'input__hidden',
  39. ];
  40. }
  41. /**
  42. * Prepares a #type 'hidden' render element for input.html.twig.
  43. *
  44. * @param array $element
  45. * An associative array containing the properties of the element.
  46. * Properties used: #name, #value, #attributes.
  47. *
  48. * @return array
  49. * The $element with prepared variables ready for input.html.twig.
  50. */
  51. public static function preRenderHidden($element) {
  52. $element['#attributes']['type'] = 'hidden';
  53. Element::setAttributes($element, ['name', 'value']);
  54. return $element;
  55. }
  56. }