You are here

protected function EntityDisplayRepository::getDisplayModeOptionsByBundle in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/EntityDisplayRepository.php \Drupal\Core\Entity\EntityDisplayRepository::getDisplayModeOptionsByBundle()
  2. 9 core/lib/Drupal/Core/Entity/EntityDisplayRepository.php \Drupal\Core\Entity\EntityDisplayRepository::getDisplayModeOptionsByBundle()

Returns an array of enabled display mode options by bundle.

Parameters

$display_type: The display type to be retrieved. It can be "view_mode" or "form_mode".

string $entity_type_id: The entity type whose display mode options should be returned.

string $bundle: The name of the bundle.

Return value

array An array of display mode labels, keyed by the display mode ID.

File

core/lib/Drupal/Core/Entity/EntityDisplayRepository.php, line 210

Class

EntityDisplayRepository
Provides a repository for entity display objects (view modes and form modes).

Namespace

Drupal\Core\Entity

Code

protected function getDisplayModeOptionsByBundle($display_type, $entity_type_id, $bundle) {

  // Collect all the entity's display modes.
  $options = $this
    ->getDisplayModeOptions($display_type, $entity_type_id);

  // Filter out modes for which the entity display is disabled
  // (or non-existent).
  $load_ids = [];

  // Get the list of available entity displays for the current bundle.
  foreach (array_keys($options) as $mode) {
    $load_ids[] = $entity_type_id . '.' . $bundle . '.' . $mode;
  }

  // Load the corresponding displays.
  $displays = $this->entityTypeManager
    ->getStorage($display_type == 'form_mode' ? 'entity_form_display' : 'entity_view_display')
    ->loadMultiple($load_ids);

  // Unset the display modes that are not active or do not exist.
  foreach (array_keys($options) as $mode) {
    $display_id = $entity_type_id . '.' . $bundle . '.' . $mode;
    if (!isset($displays[$display_id]) || !$displays[$display_id]
      ->status()) {
      unset($options[$mode]);
    }
  }
  return $options;
}