function FormElementHelper::getElementByName

Same name in other branches
  1. 9 core/lib/Drupal/Core/Form/FormElementHelper.php \Drupal\Core\Form\FormElementHelper::getElementByName()
  2. 8.9.x core/lib/Drupal/Core/Form/FormElementHelper.php \Drupal\Core\Form\FormElementHelper::getElementByName()
  3. 11.x core/lib/Drupal/Core/Form/FormElementHelper.php \Drupal\Core\Form\FormElementHelper::getElementByName()

Retrieves a form element.

Parameters

string $name: The name of the form element. If the #parents property of your form element is ['foo', 'bar', 'baz'] then the name is 'foo][bar][baz'.

array $form: An associative array containing the structure of the form.

Return value

array The form element.

3 calls to FormElementHelper::getElementByName()
FormElementHelperTest::testGetElementByName in core/tests/Drupal/Tests/Core/Form/FormElementHelperTest.php
Tests the getElementByName() method.
FormElementHelperTest::testGetElementTitle in core/tests/Drupal/Tests/Core/Form/FormElementHelperTest.php
Tests the getElementTitle() method.
FormErrorHandler::displayErrorMessages in core/modules/inline_form_errors/src/FormErrorHandler.php
Loops through and displays all form errors.

File

core/lib/Drupal/Core/Form/FormElementHelper.php, line 24

Class

FormElementHelper
Provides common functionality for form elements.

Namespace

Drupal\Core\Form

Code

public static function getElementByName($name, array $form) {
    foreach (Element::children($form) as $key) {
        if (implode('][', $form[$key]['#parents']) === $name) {
            return $form[$key];
        }
        elseif ($element = static::getElementByName($name, $form[$key])) {
            return $element;
        }
    }
    return [];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.