Problem/Motivation

When trying to use the widget in a render array (according to example in docs/examples/index.md and docs/api/overview.md), it gives a type error. If the '#components' key is removed then there is no error:

Gives Error

<pre>
    $form['material_information'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Material Information'),
      '#description' => $this->t('Please enter name as it appears in the publication.'),
      '#description_display' => 'before',
      [
        '#type' => 'fieldset',
        '#attributes' => [
          'class' => [
            'px-5',
          ],
        ],
        [
          '#type' => 'name',
          '#title' => $this->t('Author/Artist/Editor/Translator'),
          '#minimum_components' => [
            'given' => 'given',
            'family' => 'family',
          ],
          '#components' => [
            'given' => 'given',
            'family' => 'family',
            'title' => 'title',
            'generational' => 'generational',
          ],
          '#labels' => [
            'title' => $this->t('Title'),
            'given' => $this->t('Given/First Name'),
            'family' => $this->t('Surname/Last Name'),
          ],
        ],
      ],
    ];
</pre>

No Error but ignores #labels and #minimum_components

<pre>
    $form['material_information'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Material Information'),
      '#description' => $this->t('Please enter name as it appears in the publication.'),
      '#description_display' => 'before',
      [
        '#type' => 'fieldset',
        '#attributes' => [
          'class' => [
            'px-5',
          ],
        ],
        [
          '#type' => 'name',
          '#title' => $this->t('Author/Artist/Editor/Translator'),
          '#minimum_components' => [
            'given' => 'given',
            'family' => 'family',
          ],
          '#labels' => [
            'title' => $this->t('Title'),
            'given' => $this->t('Given/First Name'),
            'family' => $this->t('Surname/Last Name'),
          ],
        ],
      ],
    ];
</pre>
TypeError: Drupal\Core\Render\Element::properties(): Argument #1 ($element) must be of type array, string given, called in /opt/drupal/web/modules/contrib/name/name.module on line 456 in Drupal\Core\Render\Element::properties() (line 38 of /opt/drupal/web/core/lib/Drupal/Core/Render/Element.php).

Backtrace:

#0 /opt/drupal/web/modules/contrib/name/name.module(456): Drupal\Core\Render\Element::properties('title')
#1 /opt/drupal/web/modules/contrib/name/name.module(412): name_element_render_component(Array, 'title', Array, false)
#2 [internal function]: name_element_expand(Array, Object(Drupal\Core\Form\FormState), Array)
#3 /opt/drupal/web/core/lib/Drupal/Core/Form/FormBuilder.php(1011): call_user_func_array('name_element_ex...', Array)
#4 /opt/drupal/web/core/lib/Drupal/Core/Form/FormBuilder.php(1074): Drupal\Core\Form\FormBuilder->doBuildForm('msul_faculty_au...', Array, Object(Drupal\Core\Form\FormState))
#5 /opt/drupal/web/core/lib/Drupal/Core/Form/FormBuilder.php(1074): Drupal\Core\Form\FormBuilder->doBuildForm('msul_faculty_au...', Array, Object(Drupal\Core\Form\FormState))
#6 /opt/drupal/web/core/lib/Drupal/Core/Form/FormBuilder.php(1074): Drupal\Core\Form\FormBuilder->doBuildForm('msul_faculty_au...', Array, Object(Drupal\Core\Form\FormState))
#7 /opt/drupal/web/core/lib/Drupal/Core/Form/FormBuilder.php(579): Drupal\Core\Form\FormBuilder->doBuildForm('msul_faculty_au...', Array, Object(Drupal\Core\Form\FormState))
#8 /opt/drupal/web/core/lib/Drupal/Core/Form/FormBuilder.php(326): Drupal\Core\Form\FormBuilder->processForm('msul_faculty_au...', Array, Object(Drupal\Core\Form\FormState))
#9 /opt/drupal/web/core/lib/Drupal/Core/Controller/FormController.php(73): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\msul_faculty_authors\Form\FacultyAuthorInputForm), Object(Drupal\Core\Form\FormState))
#10 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
#11 /opt/drupal/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#12 /opt/drupal/web/core/lib/Drupal/Core/Render/Renderer.php(637): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#13 /opt/drupal/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#14 /opt/drupal/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#15 /opt/drupal/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#16 /opt/drupal/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#17 /opt/drupal/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#18 /opt/drupal/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#19 /opt/drupal/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#20 /opt/drupal/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#21 /opt/drupal/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(116): Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#22 /opt/drupal/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(90): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#23 /opt/drupal/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#24 /opt/drupal/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#25 /opt/drupal/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#26 /opt/drupal/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#27 /opt/drupal/web/core/lib/Drupal/Core/DrupalKernel.php(741): Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#28 /opt/drupal/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#29 {main}

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork name-3589027

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

kwfinken created an issue. See original summary.

kwfinken’s picture

Title: Using form in render API fails. » Using filed widget in form render API fails.
kwfinken’s picture

Title: Using filed widget in form render API fails. » Using field widget in form render API fails.
bluegeek9’s picture

I will look into this. I think the documentation might be wrong.

bluegeek9’s picture

Component: Code » Documentation
Assigned: Unassigned » bluegeek9
Category: Bug report » Support request
Priority: Major » Normal

The components need to contain the element array, not the strings.

I will update the documentation.


    $form['material_information'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Material Information'),
      '#description' => $this->t('Please enter name as it appears in the publication.'),
      '#description_display' => 'before',
      [
        '#type' => 'fieldset',
        '#attributes' => [
          'class' => [
            'px-5',
          ],
        ],
        [
          '#type' => 'name',
          '#title' => $this->t('Author/Artist/Editor/Translator'),
          '#minimum_components' => [
            'given' => 'given',
            'family' => 'family',
          ],
          '#components' => [
            'title' => [
              'type' => 'textfield',
              'title' => $this->t('Title'),
              'title_display' => 'description',
              'size' => 20,
              'maxlength' => 255,
              'autocomplete' => FALSE,
            ],
            'given' => [
              'type' => 'textfield',
              'title' => $this->t('Given/First Name'),
              'title_display' => 'description',
              'size' => 60,
              'maxlength' => 255,
              'autocomplete' => FALSE,
            ],
            'family' => [
              'type' => 'textfield',
              'title' => $this->t('Surname/Last Name'),
              'title_display' => 'description',
              'size' => 60,
              'maxlength' => 255,
              'autocomplete' => FALSE,
            ],
            'generational' => [
              'type' => 'textfield',
              'title' => $this->t('Generational'),
              'title_display' => 'description',
              'size' => 20,
              'maxlength' => 255,
              'autocomplete' => FALSE,
            ],
            'middle' => ['exclude' => TRUE],
            'credentials' => ['exclude' => TRUE],
          ],
          '#labels' => [
            'title' => $this->t('Title'),
            'given' => $this->t('Given/First Name'),
            'family' => $this->t('Surname/Last Name'),
          ],
        ],
      ],
    ];

  • bluegeek9 committed 822d652a on 8.x-1.x
    fix: #3589027 Using field widget in form render API fails.
    
bluegeek9’s picture

Assigned: bluegeek9 » Unassigned
Status: Active » Fixed
//www.flaticon.com/free-icons/thank-you Thank you for your contribution! Your continued support makes this project sustainable.
There are multiple ways to show appreciation for the work contributed to this project including:

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.