3 namespace Drupal\Core\Render\Element;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Render\Element;
9 * Provides an action button form element.
11 * When the button is pressed, the form will be submitted to Drupal, where it is
12 * validated and rebuilt. The submit handler is not invoked.
15 * - #limit_validation_errors: An array of form element keys that will block
16 * form submission when validation for these elements or any child elements
17 * fails. Specify an empty array to suppress all form validation errors.
18 * - #value: The text to be shown on the button.
23 * $form['actions']['preview'] = array(
24 * '#type' => 'button',
25 * '#value' => $this->t('Preview'),
29 * @see \Drupal\Core\Render\Element\Submit
31 * @FormElement("button")
33 class Button extends FormElement {
38 public function getInfo() {
39 $class = get_class($this);
44 '#executes_submit_callback' => FALSE,
45 '#limit_validation_errors' => FALSE,
47 [$class, 'processButton'],
48 [$class, 'processAjaxForm'],
51 [$class, 'preRenderButton'],
53 '#theme_wrappers' => ['input__submit'],
58 * Processes a form button element.
60 public static function processButton(&$element, FormStateInterface $form_state, &$complete_form) {
61 // If this is a button intentionally allowing incomplete form submission
62 // (e.g., a "Previous" or "Add another item" button), then also skip
63 // client-side validation.
64 if (isset($element['#limit_validation_errors']) && $element['#limit_validation_errors'] !== FALSE) {
65 $element['#attributes']['formnovalidate'] = 'formnovalidate';
71 * Prepares a #type 'button' render element for input.html.twig.
73 * @param array $element
74 * An associative array containing the properties of the element.
75 * Properties used: #attributes, #button_type, #name, #value. The
76 * #button_type property accepts any value, though core themes have CSS that
77 * styles the following button_types appropriately: 'primary', 'danger'.
80 * The $element with prepared variables ready for input.html.twig.
82 public static function preRenderButton($element) {
83 $element['#attributes']['type'] = 'submit';
84 Element::setAttributes($element, ['id', 'name', 'value']);
86 $element['#attributes']['class'][] = 'button';
87 if (!empty($element['#button_type'])) {
88 $element['#attributes']['class'][] = 'button--' . $element['#button_type'];
90 $element['#attributes']['class'][] = 'js-form-submit';
91 $element['#attributes']['class'][] = 'form-submit';
93 if (!empty($element['#attributes']['disabled'])) {
94 $element['#attributes']['class'][] = 'is-disabled';