summaryrefslogtreecommitdiffstats
path: root/yamlform.api.php
blob: f7d4d70df025151fb4b4a36387074514897d7cc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php

/**
 * @file
 * Hooks related to YAML Form module.
 */

/**
 * @addtogroup hooks
 * @{
 */

/**
 * Alter the information provided in \Drupal\yamlform\Annotation\YamlFormElement.
 *
 * @param array $elements
 *   The array of form handlers, keyed on the machine-readable element name.
 */
function hook_yamlform_element_info_alter(array &$elements) {

}

/**
 * Alter the information provided in \Drupal\yamlform\Annotation\YamlFormHandler.
 *
 * @param array $handlers
 *   The array of form handlers, keyed on the machine-readable handler name.
 */
function hook_yamlform_handler_info_alter(array &$handlers) {

}

/**
 * Alter form elements.
 *
 * @param array $element
 *   The form element.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form.
 * @param array $context
 *   An associative array containing the following key-value pairs:
 *   - form: The form structure to which elements is being attached.
 *
 * @see \Drupal\yamlform\YamlFormSubmissionForm::prepareElements()
 * @see hook_yamlform_element_ELEMENT_TYPE_form_alter()
 */
function hook_yamlform_element_alter(array &$element, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
  // Code here acts on all elements included in a form.
  /** @var \Drupal\yamlform\YamlFormSubmissionForm $form_object */
  $form_object = $form_state->getFormObject();
  /** @var \Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission */
  $yamlform_submission = $form_object->getEntity();
  /** @var \Drupal\yamlform\YamlFormInterface $yamlform */
  $yamlform = $yamlform_submission->getYamlForm();

  // Add custom data attributes to all elements.
  $element['#attributes']['data-custom'] = '{custom data goes here}';
}

/**
 * Alter form elements for a specific type.
 *
 * Modules can implement hook_yamlform_element_ELEMENT_TYPE_form_alter() to
 * modify a specific form element, rather than using
 * hook_yamlform_element_alter() and checking the element type.
 *
 * @param array $element
 *   The form element.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form.
 * @param array $context
 *   An associative array. See hook_field_widget_form_alter() for the structure
 *   and content of the array.
 *
 * @see \Drupal\yamlform\YamlFormSubmissionForm::prepareElements()
 * @see hook_yamlform_element_alter(()
 */
function hook_yamlform_element_ELEMENT_TYPE_form_alter(array &$element, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
  // Add custom data attributes to a specific element type.
  $element['#attributes']['data-custom'] = '{custom data goes here}';

  // Attach a custom library to the element type.
  $element['#attached']['library'][] = 'MODULE/MODULE.element.ELEMENT_TYPE';
}

/**
 * Alter the form options by id.
 *
 * @param array $options
 *   An associative array of options.
 * @param array $element
 *   The form element that the options is for.
 */
function hook_yamlform_options_YAMLFORM_OPTIONS_ID_alter(array &$options, array &$element = []) {

}

/**
 * Perform alterations before a form submission form is rendered.
 *
 * This hook is identical to hook_form_alter() but allows the
 * hook_yamlform_submission_form_alter() function to be stored in a dedicated
 * include file and it also allows the YAML Form module to implement form alter
 * logic on another module's behalf.
 *
 * @param array $form
 *   Nested array of form elements that comprise the form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form. The arguments that
 *   \Drupal::formBuilder()->getForm() was originally called with are available
 *   in the array $form_state->getBuildInfo()['args'].
 * @param string $form_id
 *   String representing the form's id.
 *
 * @see yamlform.honeypot.inc
 * @see hook_form_BASE_FORM_ID_alter()
 * @see hook_form_FORM_ID_alter()
 *
 * @ingroup form_api
 */
function hook_yamlform_submission_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

}

/**
 * @} End of "addtogroup hooks".
 */