public function EntityTypeRepository::getEntityTypeLabels

public EntityTypeRepository::getEntityTypeLabels($group = FALSE)

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 45

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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityTypeRepository.php/function/EntityTypeRepository::getEntityTypeLabels/8.1.x