Button.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\Core\Render\Element;
  5. /**
  6. * Provides an action button form element.
  7. *
  8. * When the button is pressed, the form will be submitted to Drupal, where it is
  9. * validated and rebuilt. The submit handler is not invoked.
  10. *
  11. * Properties:
  12. * - #limit_validation_errors: An array of form element keys that will block
  13. * form submission when validation for these elements or any child elements
  14. * fails. Specify an empty array to suppress all form validation errors.
  15. * - #value: The text to be shown on the button.
  16. *
  17. *
  18. * Usage Example:
  19. * @code
  20. * $form['actions']['preview'] = array(
  21. * '#type' => 'button',
  22. * '#value' => $this->t('Preview'),
  23. * );
  24. * @endcode
  25. *
  26. * @see \Drupal\Core\Render\Element\Submit
  27. *
  28. * @FormElement("button")
  29. */
  30. class Button extends FormElement {
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getInfo() {
  35. $class = get_class($this);
  36. return [
  37. '#input' => TRUE,
  38. '#name' => 'op',
  39. '#is_button' => TRUE,
  40. '#executes_submit_callback' => FALSE,
  41. '#limit_validation_errors' => FALSE,
  42. '#process' => [
  43. [$class, 'processButton'],
  44. [$class, 'processAjaxForm'],
  45. ],
  46. '#pre_render' => [
  47. [$class, 'preRenderButton'],
  48. ],
  49. '#theme_wrappers' => ['input__submit'],
  50. ];
  51. }
  52. /**
  53. * Processes a form button element.
  54. */
  55. public static function processButton(&$element, FormStateInterface $form_state, &$complete_form) {
  56. // If this is a button intentionally allowing incomplete form submission
  57. // (e.g., a "Previous" or "Add another item" button), then also skip
  58. // client-side validation.
  59. if (isset($element['#limit_validation_errors']) && $element['#limit_validation_errors'] !== FALSE) {
  60. $element['#attributes']['formnovalidate'] = 'formnovalidate';
  61. }
  62. return $element;
  63. }
  64. /**
  65. * Prepares a #type 'button' render element for input.html.twig.
  66. *
  67. * @param array $element
  68. * An associative array containing the properties of the element.
  69. * Properties used: #attributes, #button_type, #name, #value. The
  70. * #button_type property accepts any value, though core themes have CSS that
  71. * styles the following button_types appropriately: 'primary', 'danger'.
  72. *
  73. * @return array
  74. * The $element with prepared variables ready for input.html.twig.
  75. */
  76. public static function preRenderButton($element) {
  77. $element['#attributes']['type'] = 'submit';
  78. Element::setAttributes($element, ['id', 'name', 'value']);
  79. $element['#attributes']['class'][] = 'button';
  80. if (!empty($element['#button_type'])) {
  81. $element['#attributes']['class'][] = 'button--' . $element['#button_type'];
  82. }
  83. $element['#attributes']['class'][] = 'js-form-submit';
  84. $element['#attributes']['class'][] = 'form-submit';
  85. if (!empty($element['#attributes']['disabled'])) {
  86. $element['#attributes']['class'][] = 'is-disabled';
  87. }
  88. return $element;
  89. }
  90. }