function FormElementHelper::getElementTitle

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

Returns the title for the element.

If the element has no title, this will recurse through all children of the element until a title is found.

Parameters

array $element: An associative array containing the properties of the form element.

Return value

string The title of the element, or an empty string if none is found.

2 calls to FormElementHelper::getElementTitle()
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 48

Class

FormElementHelper
Provides common functionality for form elements.

Namespace

Drupal\Core\Form

Code

public static function getElementTitle(array $element) {
    $title = '';
    if (isset($element['#title'])) {
        $title = $element['#title'];
    }
    else {
        foreach (Element::children($element) as $key) {
            if ($title = static::getElementTitle($element[$key])) {
                break;
            }
        }
    }
    return $title;
}

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