You are here

public function EntityTypeRepository::getEntityTypeLabels in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Entity/EntityTypeRepository.php \Drupal\Core\Entity\EntityTypeRepository::getEntityTypeLabels()

Builds a list of entity type labels suitable for a Form API options list.

Parameters

bool $group: (optional) Whether to group entity types by plugin group (e.g. 'content', 'config'). Defaults to FALSE.

Return value

array An array of entity type labels, keyed by entity type name.

Overrides EntityTypeRepositoryInterface::getEntityTypeLabels

File

core/lib/Drupal/Core/Entity/EntityTypeRepository.php, line 50
Contains \Drupal\Core\Entity\EntityTypeRepository.

Class

EntityTypeRepository
Provides helper methods for loading entity types.

Namespace

Drupal\Core\Entity

Code

public function getEntityTypeLabels($group = FALSE) {
  $options = [];
  $definitions = $this->entityTypeManager
    ->getDefinitions();
  foreach ($definitions as $entity_type_id => $definition) {
    if ($group) {
      $options[(string) $definition
        ->getGroupLabel()][$entity_type_id] = $definition
        ->getLabel();
    }
    else {
      $options[$entity_type_id] = $definition
        ->getLabel();
    }
  }
  if ($group) {
    foreach ($options as &$group_options) {

      // Sort the list alphabetically by group label.
      array_multisort($group_options, SORT_ASC, SORT_NATURAL);
    }

    // Make sure that the 'Content' group is situated at the top.
    $content = $this
      ->t('Content', array(), array(
      'context' => 'Entity type group',
    ));
    $options = array(
      (string) $content => $options[(string) $content],
    ) + $options;
  }
  return $options;
}