getElementByName example


  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 [];
  }

  /** * 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. * * @param array $element * An associative array containing the properties of the form element. * * @return string * The title of the element, or an empty string if none is found. */

    public function saveConfig(Template $template, array $values)
    {
        foreach ($values as $data) {
            // Get the element using the name             $element = $this->getElementByName(
                $template->getElements(),
                $data['elementName']
            );

            if (!($element instanceof Element)) {
                continue;
            }

            $value = $this->getElementShopValue(
                $element->getValues(),
                $data['shopId']
            );

class FormElementHelperTest extends UnitTestCase {

  /** * Tests the getElementByName() method. * * @covers ::getElementByName * * @dataProvider getElementByNameProvider */
  public function testGetElementByName($name$form$expected) {
    $this->assertSame($expected, FormElementHelper::getElementByName($name$form));
  }

  /** * Provides test data. */
  public function getElementByNameProvider() {
    $data = [];
    $data[] = ['id', [][]];
    $data[] = [
      'id',
      [
        
protected function displayErrorMessages(array $form, FormStateInterface $form_state) {
    // Skip generating inline form errors when opted out.     if (!empty($form['#disable_inline_form_errors'])) {
      parent::displayErrorMessages($form$form_state);
      return;
    }

    $error_links = [];
    $errors = $form_state->getErrors();
    // Loop through all form errors and check if we need to display a link.     foreach ($errors as $name => $error) {
      $form_element = FormElementHelper::getElementByName($name$form);
      $title = FormElementHelper::getElementTitle($form_element);

      // Only show links to erroneous elements that are visible.       $is_visible_element = Element::isVisibleElement($form_element);
      // Only show links for elements that have a title themselves or have       // children with a title.       $has_title = !empty($title);
      // Only show links for elements with an ID.       $has_id = !empty($form_element['#id']);

      // Do not show links to elements with suppressed messages. Most often
Home | Imprint | This part of the site doesn't use cookies.