Missing $display check in entity_extra_field_form_alter (edge case)

Created on 30 November 2023, 7 months ago
Updated 12 March 2024, 3 months ago

Problem/Motivation

The following error is emitted when trying to open the Product add form with Commerce installed and no Stores configured:

TypeError: entity_extra_field_display(): Argument #4 ($display) must be of type Drupal\Core\Entity\Display\EntityDisplayInterface, null given, called in /var/www/html/web/modules/contrib/entity_extra_field/entity_extra_field.module on line 78 in entity_extra_field_display() (line 111 of modules/contrib/entity_extra_field/entity_extra_field.module).

The problem here is that while$form_object->getFormDisplay($form_state); is expected to return EntityFormDisplayInterface, there's an edge case with Commerce; the entity form may not be fully initialized if there are not Stores configured. Instead, it just displays the warning message (and doesn't call parent buildForm where the form display initialization should be called from).

From \Drupal\commerce_product\Form\ProductForm:

  public function buildForm(array $form, FormStateInterface $form_state) {
    // Skip building the form if there are no available stores.
    $store_query = $this->entityTypeManager->getStorage('commerce_store')->getQuery()->accessCheck(TRUE);
    if ($store_query->count()->execute() == 0) {
      $link = Link::createFromRoute('Add a new store.', 'entity.commerce_store.add_page');
      $form['warning'] = [
        '#markup' => $this->t("Products can't be created until a store has been added. @link", ['@link' => $link->toString()]),
      ];
      return $form;
    }

    return parent::buildForm($form, $form_state);
  }

As a workaround, the Store should be created before opening the form.

Steps to reproduce

1) Install entity_extra_field and commerce.
2) Do NOT create Store.
3) Open /product/add/default.

Expected: the page opens, there's a warning message instead of the form.
Actual: error message / WSOD.

Proposed resolution

As a workaround for Commerce forms behavior, check if $display is not null in entity_extra_field_form_alter.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

๐Ÿ› Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

๐Ÿ‡บ๐Ÿ‡ฆUkraine abramm Lutsk

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024