You are here

public function UpdateHelper::checkFormDisplay in Menu Item Extras 8.2

Creates or load form display.

Parameters

string $display_mode: Name of display mode.

\Drupal\Core\Entity\EntityInterface $entity: Entity instance.

string $field_name: Field name.

array $settings: Widget settings.

Return value

\Drupal\Core\Entity\EntityInterface|null|static Form display instance.

File

src/Service/UpdateHelper.php, line 136

Class

UpdateHelper
Class MenuLinkContentUpdateHelper.

Namespace

Drupal\menu_item_extras\Service

Code

public function checkFormDisplay($display_mode, EntityInterface $entity, $field_name, array $settings) {
  $display = EntityFormDisplay::load($entity
    ->getEntityTypeId() . '.' . $entity
    ->bundle() . '.' . $display_mode);

  // If not found, create a fresh entity object.
  // We do not preemptively create new entity form display
  // configuration entries for each existing entity type
  // and bundle whenever a new form mode becomes available.
  // Instead, configuration entries are only created when
  // an entity form display is explicitly configured and saved.
  if (!$display) {
    $display = EntityFormDisplay::create([
      'targetEntityType' => $entity
        ->getEntityTypeId(),
      'bundle' => $entity
        ->bundle(),
      'mode' => $display_mode,
      'status' => TRUE,
    ]);
  }
  $component = $display
    ->getComponent($field_name);
  if (empty($component)) {

    // Assign widget settings for the 'default' form mode.
    $display
      ->setComponent($field_name, $settings);
    $display
      ->save();
  }
  return $display;
}