I added a name form field to a custom form and am seeing this error.

Error: Cannot use object of type Drupal\Core\Form\FormState as array in name_element_expand() (line 245 of web/modules/contrib/name/name.module) #0 [internal function]: name_element_expand(Array, Object(Drupal\Core\Form\FormState), Array)   admin (1)   Error

The error comes from this code:

/**
 * The #process callback to create the element.
 */
  function name_element_expand($element, &$form_state, $complete_form) {
  /**
   * @var \Drupal\Core\Language\Language $language
   */
  $language = Drupal::languageManager()->getCurrentLanguage();

  $element['#tree'] = TRUE;
  if (empty($element['#value'])) {
    $element['#value'] = array();
  }
  $parts = _name_translations();
  $components = $element['#components'];
  $min_components = (array) $element['#minimum_components'];
  if (empty($element['#field'])) {
    $element['#field'] = isset($form_state['field']) ? $form_state['field'] : NULL;
  }

Specifically this line:

$element['#field'] = isset($form_state['field']) ? $form_state['field'] : NULL;

I'm happy to supply a patch, but I'm not 100% sure what this check does and if it is needed. I am able to comment the line out and the module seems to work fine. So perhaps someone else can weigh in. I am going to post a patch that simply disables this for now to unblock myself while we sort out if this is truly needed in D8 and how to recreate the logic if it is.

Comments

adamzimmermann created an issue. See original summary.

adamzimmermann’s picture

alan d.’s picture

Status: Active » Needs review
Issue tags: +Needs tests
StatusFileSize
new1.14 KB

That appears to be something that was needed in D7 to get the description working for some unknown reason :P

Anyways, it doesn't appear to be needed and there was a second change required for the description working using:

    $form['name_test'] = [
      '#type' => 'name',
      '#title' => $this->t('Test title'),
      '#description' => $this->t('Test description'),
    ];

Flagging the tests to run, but this does show that there are tests required to check the FAPI usage.

alan d.’s picture

Status: Needs review » Needs work
Parent issue: » #2949574: [meta] Name Field Full Release

Should do some tests before committing this, but it'll be in the next release

nkoporec’s picture

Status: Needs work » Needs review

Marking it as needs review, so other people will test it too.

alan d.’s picture

Status: Needs review » Fixed
Issue tags: -Needs tests

Decoupled the test through to #3014971: FAPI tests needed as I'll unlikely find the time in the next month or three and this was a simple bug fix that was clearly broken.

The need for #3013908: Drupal\name\Element\Name should extend FormElement rather than RenderElement? probably indicates that there is a fundamental error in the original ported code, but other than to update the signature and to implement the FormElement::valueCallback() method, that patch that was pushed through didn't attempt to address that, it was only pushed through to ensure that the class was extending the right base class to minimise any API changes.

@nkoporec
Both open issues if you would like something to throw yourself at either :)

  • Alan D. committed fdc0033 on 8.x-1.x
    Issue #3013682 by adamzimmermann, Alan D.: Error thrown when $form_state...

Status: Fixed » Closed (fixed)

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