3 namespace Drupal\Core\Form;
5 use Drupal\Core\Render\Element;
8 * Provides common functionality for form elements.
10 class FormElementHelper {
13 * Retrieves a form element.
16 * The name of the form element. If the #parents property of your form
17 * element is ['foo', 'bar', 'baz'] then the name is 'foo][bar][baz'.
19 * An associative array containing the structure of the form.
24 public static function getElementByName($name, array $form) {
25 foreach (Element::children($form) as $key) {
26 if (implode('][', $form[$key]['#parents']) === $name) {
29 elseif ($element = static::getElementByName($name, $form[$key])) {
37 * Returns the title for the element.
39 * If the element has no title, this will recurse through all children of the
40 * element until a title is found.
42 * @param array $element
43 * An associative array containing the properties of the form element.
46 * The title of the element, or an empty string if none is found.
48 public static function getElementTitle(array $element) {
50 if (isset($element['#title'])) {
51 $title = $element['#title'];
54 foreach (Element::children($element) as $key) {
55 if ($title = static::getElementTitle($element[$key])) {