EntityDisplayRepository::getDisplayModeOptionsByBundle

protected EntityDisplayRepository::getDisplayModeOptionsByBundle($display_type, $entity_type_id, $bundle)

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 = array();
  // 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;
}
doc_Drupal
2016-10-29 09:05:53
Comments
Leave a Comment

Please login to continue.