You are here

public function UpdateHelper::checkViewDisplay in Menu Item Extras 8.2

Creates or load view display.

Parameters

string $display_mode: Name of display mode.

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

string $field_name: Field name.

array $settings: Formatter settings.

Return value

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

File

src/Service/UpdateHelper.php, line 94

Class

UpdateHelper
Class MenuLinkContentUpdateHelper.

Namespace

Drupal\menu_item_extras\Service

Code

public function checkViewDisplay($display_mode, EntityInterface $entity, $field_name, array $settings) {

  // Try loading the display from configuration.
  $display = EntityViewDisplay::load($entity
    ->getEntityTypeId() . '.' . $entity
    ->bundle() . '.' . $display_mode);

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

    // Assign display settings for the 'default' and 'teaser' view modes.
    $display
      ->setComponent($field_name, $settings);
    $display
      ->save();
  }
  return $display;
}