EntityManager.php 22.3 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\Core\Entity\EntityManager.
 */

namespace Drupal\Core\Entity;

10
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
11
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
12
use Drupal\Component\Plugin\PluginManagerBase;
13
use Drupal\Component\Utility\NestedArray;
14
use Drupal\Component\Utility\String;
15
use Drupal\Core\Field\FieldDefinition;
16
use Drupal\Core\Cache\Cache;
17
use Drupal\Core\Cache\CacheBackendInterface;
18 19
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManager;
20
use Drupal\Core\Language\Language;
21
use Drupal\Core\Plugin\Discovery\AlterDecorator;
22
use Drupal\Core\Plugin\Discovery\CacheDecorator;
23
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
24
use Drupal\Core\Plugin\Discovery\InfoHookDecorator;
25
use Drupal\Core\StringTranslation\TranslationInterface;
26
use Drupal\Core\TypedData\TranslatableInterface;
27
use Symfony\Component\DependencyInjection\ContainerInterface;
28 29 30 31

/**
 * Manages entity type plugin definitions.
 *
32
 * Each entity type definition array is set in the entity type's
33
 * annotation and altered by hook_entity_type_alter().
34 35
 *
 * The defaults for the plugin definition are provided in
36
 * \Drupal\Core\Entity\EntityManagerInterface::defaults.
37
 *
38 39
 * @see \Drupal\Core\Entity\Annotation\EntityType
 * @see \Drupal\Core\Entity\EntityInterface
40
 * @see \Drupal\Core\Entity\EntityTypeInterface
41
 * @see hook_entity_type_alter()
42
 */
43
class EntityManager extends PluginManagerBase implements EntityManagerInterface {
44

45 46 47 48 49 50 51
  /**
   * The injection container that should be passed into the controller factory.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

52 53 54 55 56 57 58
  /**
   * Contains instantiated controllers keyed by controller type and entity type.
   *
   * @var array
   */
  protected $controllers = array();

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The cache backend to use.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManager
   */
  protected $languageManager;

  /**
81
   * Static cache of base field definitions.
82 83 84
   *
   * @var array
   */
85
  protected $baseFieldDefinitions;
86 87 88 89 90 91 92 93

  /**
   * Static cache of field definitions per bundle and entity type.
   *
   * @var array
   */
  protected $fieldDefinitions;

94 95 96
  /**
   * The root paths.
   *
97
   * @see self::__construct().
98 99 100 101 102
   *
   * @var \Traversable
   */
  protected $namespaces;

103 104 105 106 107 108 109
  /**
   * The string translationManager.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $translationManager;

110
  /**
111 112 113 114 115 116
   * Static cache of bundle information.
   *
   * @var array
   */
  protected $bundleInfo;

117 118 119 120 121 122 123
  /**
   * Static cache of display modes information.
   *
   * @var array
   */
  protected $displayModeInfo = array();

124 125
  /**
   * Constructs a new Entity plugin manager.
126
   *
127 128 129
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
   *   keyed by the corresponding namespace to look for plugin implementations,
130 131
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The service container this object should use.
132 133 134 135 136 137
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache backend to use.
   * @param \Drupal\Core\Language\LanguageManager $language_manager
   *   The language manager.
138 139
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
   *   The string translationManager.
140
   */
141
  public function __construct(\Traversable $namespaces, ContainerInterface $container, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, LanguageManager $language_manager, TranslationInterface $translation_manager) {
142
    // Allow the plugin definition to be altered by hook_entity_type_alter().
143 144 145 146

    $this->moduleHandler = $module_handler;
    $this->cache = $cache;
    $this->languageManager = $language_manager;
147
    $this->namespaces = $namespaces;
148
    $this->translationManager = $translation_manager;
149

150
    $this->discovery = new AnnotatedClassDiscovery('Entity', $namespaces, 'Drupal\Core\Entity\Annotation\EntityType');
151 152 153
    $this->discovery = new InfoHookDecorator($this->discovery, 'entity_type_build');
    $this->discovery = new AlterDecorator($this->discovery, 'entity_type');
    $this->discovery = new CacheDecorator($this->discovery, 'entity_type:' . $this->languageManager->getCurrentLanguage()->id, 'cache', Cache::PERMANENT, array('entity_types' => TRUE));
154

155
    $this->container = $container;
156 157
  }

158 159 160 161 162 163 164
  /**
   * {@inheritdoc}
   */
  public function clearCachedDefinitions() {
    parent::clearCachedDefinitions();

    $this->bundleInfo = NULL;
165
    $this->displayModeInfo = array();
166 167
  }

168
  /**
169
   * {@inheritdoc}
170
   */
171 172 173
  public function getDefinition($entity_type_id, $exception_on_invalid = FALSE) {
    if (($entity_type = parent::getDefinition($entity_type_id)) && class_exists($entity_type->getClass())) {
      return $entity_type;
174
    }
175 176 177 178
    elseif (!$exception_on_invalid) {
      return NULL;
    }

179
    throw new PluginNotFoundException($entity_type_id, sprintf('The "%s" entity type does not exist.', $entity_type_id));
180 181
  }

182
  /**
183
   * {@inheritdoc}
184
   */
185 186 187
  public function hasController($entity_type, $controller_type) {
    if ($definition = $this->getDefinition($entity_type)) {
      return $definition->hasControllerClass($controller_type);
188
    }
189
    return FALSE;
190 191 192
  }

  /**
193
   * {@inheritdoc}
194 195
   */
  public function getStorageController($entity_type) {
196
    return $this->getController($entity_type, 'storage', 'getStorageClass');
197 198 199
  }

  /**
200
   * {@inheritdoc}
201
   */
202 203
  public function getListBuilder($entity_type) {
    return $this->getController($entity_type, 'list_builder', 'getListBuilderClass');
204 205 206
  }

  /**
207
   * {@inheritdoc}
208 209 210
   */
  public function getFormController($entity_type, $operation) {
    if (!isset($this->controllers['form'][$operation][$entity_type])) {
211 212 213
      if (!$class = $this->getDefinition($entity_type, TRUE)->getFormClass($operation)) {
        throw new InvalidPluginDefinitionException($entity_type, sprintf('The "%s" entity type did not specify a "%s" form class.', $entity_type, $operation));
      }
214
      if (in_array('Drupal\Core\DependencyInjection\ContainerInjectionInterface', class_implements($class))) {
215
        $controller = $class::create($this->container);
216 217
      }
      else {
218
        $controller = new $class();
219
      }
220 221 222 223 224 225

      $controller
        ->setTranslationManager($this->translationManager)
        ->setModuleHandler($this->moduleHandler)
        ->setOperation($operation);
      $this->controllers['form'][$operation][$entity_type] = $controller;
226 227 228 229 230
    }
    return $this->controllers['form'][$operation][$entity_type];
  }

  /**
231
   * {@inheritdoc}
232
   */
233
  public function getViewBuilder($entity_type) {
234
    return $this->getController($entity_type, 'view_builder', 'getViewBuilderClass');
235 236 237
  }

  /**
238
   * {@inheritdoc}
239 240
   */
  public function getAccessController($entity_type) {
241
    return $this->getController($entity_type, 'access', 'getAccessClass');
242 243 244 245 246 247
  }

  /**
   * Creates a new controller instance.
   *
   * @param string $entity_type
248
   *   The entity type for this controller.
249 250
   * @param string $controller_type
   *   The controller type to create an instance for.
251 252
   * @param string $controller_class_getter
   *   (optional) The method to call on the entity type object to get the controller class.
253
   *
254
   * @return mixed
255
   *   A controller instance.
256 257
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
258
   */
259
  public function getController($entity_type, $controller_type, $controller_class_getter = NULL) {
260
    if (!isset($this->controllers[$controller_type][$entity_type])) {
261 262 263
      $definition = $this->getDefinition($entity_type, TRUE);
      if ($controller_class_getter) {
        $class = $definition->{$controller_class_getter}();
264 265
      }
      else {
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
        $class = $definition->getControllerClass($controller_type);
      }
      if (!$class) {
        throw new InvalidPluginDefinitionException($entity_type, sprintf('The "%s" entity type did not specify a %s class.', $entity_type, $controller_type));
      }
      if (is_subclass_of($class, 'Drupal\Core\Entity\EntityControllerInterface')) {
        $controller = $class::createInstance($this->container, $definition);
      }
      else {
        $controller = new $class($definition);
      }
      if (method_exists($controller, 'setModuleHandler')) {
        $controller->setModuleHandler($this->moduleHandler);
      }
      if (method_exists($controller, 'setTranslationManager')) {
        $controller->setTranslationManager($this->translationManager);
282
      }
283
      $this->controllers[$controller_type][$entity_type] = $controller;
284
    }
285
    return $this->controllers[$controller_type][$entity_type];
286 287
  }

288
  /**
289
   * {@inheritdoc}
290
   */
291 292
  public function getAdminRouteInfo($entity_type_id, $bundle) {
    if (($entity_type = $this->getDefinition($entity_type_id)) && $admin_form = $entity_type->getLinkTemplate('admin-form')) {
293 294 295
      return array(
        'route_name' => $admin_form,
        'route_parameters' => array(
296
          $entity_type->getBundleEntityType() => $bundle,
297 298
        ),
      );
299
    }
300 301
  }

302
  /**
303
   * {@inheritdoc}
304
   */
305 306 307 308 309
  public function getBaseFieldDefinitions($entity_type_id) {
    // Check the static cache.
    if (!isset($this->baseFieldDefinitions[$entity_type_id])) {
      // Not prepared, try to load from cache.
      $cid = 'entity_base_field_definitions:' . $entity_type_id . ':' . $this->languageManager->getCurrentLanguage()->id;
310
      if ($cache = $this->cache->get($cid)) {
311
        $this->baseFieldDefinitions[$entity_type_id] = $cache->data;
312 313
      }
      else {
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
        // Rebuild the definitions and put it into the cache.
        $this->baseFieldDefinitions[$entity_type_id] = $this->buildBaseFieldDefinitions($entity_type_id);
        $this->cache->set($cid, $this->baseFieldDefinitions[$entity_type_id], Cache::PERMANENT, array('entity_types' => TRUE, 'entity_field_info' => TRUE));
       }
     }
    return $this->baseFieldDefinitions[$entity_type_id];
  }

  /**
   * Builds base field definitions for an entity type.
   *
   * @param string $entity_type_id
   *   The entity type ID. Only entity types that implement
   *   \Drupal\Core\Entity\ContentEntityInterface are supported
   *
   * @return \Drupal\Core\Field\FieldDefinitionInterface[]
   *   An array of field definitions, keyed by field name.
   *
   * @throws \LogicException
   *   Thrown if one of the entity keys is flagged as translatable.
   */
  protected function buildBaseFieldDefinitions($entity_type_id) {
    $entity_type = $this->getDefinition($entity_type_id);
    $class = $entity_type->getClass();

    $base_field_definitions = $class::baseFieldDefinitions($entity_type);

    // Invoke hook.
    $result = $this->moduleHandler->invokeAll('entity_base_field_info', array($entity_type));
    $base_field_definitions = NestedArray::mergeDeep($base_field_definitions, $result);

    // Automatically set the field name for non-configurable fields.
    foreach ($base_field_definitions as $field_name => $base_field_definition) {
      if ($base_field_definition instanceof FieldDefinition) {
        $base_field_definition->setName($field_name);
        $base_field_definition->setTargetEntityTypeId($entity_type_id);
      }
    }

    // Invoke alter hook.
    $this->moduleHandler->alter('entity_base_field_info', $base_field_definitions, $entity_type);

356 357 358 359 360 361 362 363 364
    // Ensure defined entity keys are there and have proper revisionable and
    // translatable values.
    $keys = array_filter($entity_type->getKeys() + array('langcode' => 'langcode'));
    foreach ($keys as $key => $field_name) {
      if (isset($base_field_definitions[$field_name]) && in_array($key, array('id', 'revision', 'uuid', 'bundle')) && $base_field_definitions[$field_name]->isRevisionable()) {
        throw new \LogicException(String::format('The @field field cannot be revisionable as it is used as @key entity key.', array('@field' => $base_field_definitions[$field_name]->getLabel(), '@key' => $key)));
      }
      if (isset($base_field_definitions[$field_name]) && in_array($key, array('id', 'revision', 'uuid', 'bundle', 'langcode')) && $base_field_definitions[$field_name]->isTranslatable()) {
        throw new \LogicException(String::format('The @field field cannot be translatable as it is used as @key entity key.', array('@field' => $base_field_definitions[$field_name]->getLabel(), '@key' => $key)));
365 366
      }
    }
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    return $base_field_definitions;
  }

  /**
   * {@inheritdoc}
   */
  public function getFieldDefinitions($entity_type_id, $bundle) {
    if (!isset($this->fieldDefinitions[$entity_type_id][$bundle])) {
      $base_field_definitions = $this->getBaseFieldDefinitions($entity_type_id);
      // Not prepared, try to load from cache.
      $cid = 'entity_bundle_field_definitions:' . $entity_type_id . ':' . $bundle . ':' . $this->languageManager->getCurrentLanguage()->id;
      if ($cache = $this->cache->get($cid)) {
        $bundle_field_definitions = $cache->data;
      }
      else {
        // Rebuild the definitions and put it into the cache.
383
        $bundle_field_definitions = $this->buildBundleFieldDefinitions($entity_type_id, $bundle, $base_field_definitions);
384 385 386 387 388 389 390
        $this->cache->set($cid, $bundle_field_definitions, Cache::PERMANENT, array('entity_types' => TRUE, 'entity_field_info' => TRUE));
      }
      // Field definitions consist of the bundle specific overrides and the
      // base fields, merge them together. Use array_replace() to replace base
      // fields with by bundle overrides and keep them in order, append
      // additional by bundle fields.
      $this->fieldDefinitions[$entity_type_id][$bundle] = array_replace($base_field_definitions, $bundle_field_definitions);
391
    }
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    return $this->fieldDefinitions[$entity_type_id][$bundle];
  }

  /**
   * Builds field definitions for a specific bundle within an entity type.
   *
   * @param string $entity_type_id
   *   The entity type ID. Only entity types that implement
   *   \Drupal\Core\Entity\ContentEntityInterface are supported.
   * @param string $bundle
   *   The bundle.
   * @param \Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions
   *   The list of base field definitions.
   *
   * @return \Drupal\Core\Field\FieldDefinitionInterface[]
   *   An array of bundle field definitions, keyed by field name. Does
   *   not include base fields.
   */
410
  protected function buildBundleFieldDefinitions($entity_type_id, $bundle, array $base_field_definitions) {
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    $entity_type = $this->getDefinition($entity_type_id);
    $class = $entity_type->getClass();

    // Allow the entity class to override the base fields.
    $bundle_field_definitions = $class::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions);

    // Invoke 'per bundle' hook.
    $result = $this->moduleHandler->invokeAll('entity_bundle_field_info', array($entity_type, $bundle, $base_field_definitions));
    $bundle_field_definitions = NestedArray::mergeDeep($bundle_field_definitions, $result);

    // Automatically set the field name for non-configurable fields.
    foreach ($bundle_field_definitions as $field_name => $field_definition) {
      if ($field_definition instanceof FieldDefinition) {
        $field_definition->setName($field_name);
        $field_definition->setTargetEntityTypeId($entity_type_id);
426 427
      }
    }
428 429 430 431 432

    // Invoke 'per bundle' alter hook.
    $this->moduleHandler->alter('entity_bundle_field_info', $bundle_field_definitions, $entity_type, $bundle);

    return $bundle_field_definitions;
433 434 435
  }

  /**
436
   * {@inheritdoc}
437 438
   */
  public function clearCachedFieldDefinitions() {
439 440
    $this->baseFieldDefinitions = array();
    $this->fieldDefinitions = array();
441
    Cache::deleteTags(array('entity_field_info' => TRUE));
442 443
  }

444
  /**
445
   * {@inheritdoc}
446 447 448 449 450 451 452
   */
  public function getBundleInfo($entity_type) {
    $bundle_info = $this->getAllBundleInfo();
    return isset($bundle_info[$entity_type]) ? $bundle_info[$entity_type] : array();
  }

  /**
453
   * {@inheritdoc}
454 455 456
   */
  public function getAllBundleInfo() {
    if (!isset($this->bundleInfo)) {
457
      $langcode = $this->languageManager->getCurrentLanguage()->id;
458 459 460 461 462 463
      if ($cache = $this->cache->get("entity_bundle_info:$langcode")) {
        $this->bundleInfo = $cache->data;
      }
      else {
        $this->bundleInfo = $this->moduleHandler->invokeAll('entity_bundle_info');
        // If no bundles are provided, use the entity type name and label.
464
        foreach ($this->getDefinitions() as $type => $entity_type) {
465
          if (!isset($this->bundleInfo[$type])) {
466
            $this->bundleInfo[$type][$type]['label'] = $entity_type->getLabel();
467 468 469
          }
        }
        $this->moduleHandler->alter('entity_bundle_info', $this->bundleInfo);
470
        $this->cache->set("entity_bundle_info:$langcode", $this->bundleInfo, Cache::PERMANENT, array('entity_types' => TRUE));
471 472 473 474 475 476
      }
    }

    return $this->bundleInfo;
  }

477
  /**
478
   * {@inheritdoc}
479 480 481 482
   */
  public function getEntityTypeLabels() {
    $options = array();
    foreach ($this->getDefinitions() as $entity_type => $definition) {
483
      $options[$entity_type] = $definition->getLabel();
484 485 486 487 488
    }

    return $options;
  }

489 490 491 492 493 494 495 496
  /**
   * {@inheritdoc}
   */
  public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = array()) {
    $translation = $entity;

    if ($entity instanceof TranslatableInterface) {
      if (empty($langcode)) {
497
        $langcode = $this->languageManager->getCurrentLanguage(Language::TYPE_CONTENT)->id;
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
      }

      // Retrieve language fallback candidates to perform the entity language
      // negotiation.
      $context['data'] = $entity;
      $context += array('operation' => 'entity_view');
      $candidates = $this->languageManager->getFallbackCandidates($langcode, $context);

      // Ensure the default language has the proper language code.
      $default_language = $entity->getUntranslated()->language();
      $candidates[$default_language->id] = Language::LANGCODE_DEFAULT;

      // Return the most fitting entity translation.
      foreach ($candidates as $candidate) {
        if ($entity->hasTranslation($candidate)) {
          $translation = $entity->getTranslation($candidate);
          break;
        }
      }
    }

    return $translation;
  }

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
  /**
   * {@inheritdoc}
   */
  public function getAllViewModes() {
    return $this->getAllDisplayModesByEntityType('view_mode');
  }

  /**
   * {@inheritdoc}
   */
  public function getViewModes($entity_type_id) {
    return $this->getDisplayModesByEntityType('view_mode', $entity_type_id);
  }

  /**
   * {@inheritdoc}
   */
  public function getAllFormModes() {
    return $this->getAllDisplayModesByEntityType('form_mode');
  }

  /**
   * {@inheritdoc}
   */
  public function getFormModes($entity_type_id) {
    return $this->getDisplayModesByEntityType('form_mode', $entity_type_id);
  }

  /**
   * Returns the entity display mode info for all entity types.
   *
   * @param string $display_type
   *   The display type to be retrieved. It can be "view_mode" or "form_mode".
   *
   * @return array
   *   The display mode info for all entity types.
   */
  protected function getAllDisplayModesByEntityType($display_type) {
    if (!isset($this->displayModeInfo[$display_type])) {
      $key = 'entity_' . $display_type . '_info';
      $langcode = $this->languageManager->getCurrentLanguage(Language::TYPE_INTERFACE)->id;
      if ($cache = $this->cache->get("$key:$langcode")) {
        $this->displayModeInfo[$display_type] = $cache->data;
      }
      else {
        $this->displayModeInfo[$display_type] = array();
        foreach ($this->getStorageController($display_type)->loadMultiple() as $display_mode) {
          list($display_mode_entity_type, $display_mode_name) = explode('.', $display_mode->id(), 2);
          $this->displayModeInfo[$display_type][$display_mode_entity_type][$display_mode_name] = (array) $display_mode;
        }
        $this->moduleHandler->alter($key, $this->displayModeInfo[$display_type]);
        $this->cache->set("$key:$langcode", $this->displayModeInfo[$display_type], CacheBackendInterface::CACHE_PERMANENT, array('entity_types' => TRUE));
      }
    }

    return $this->displayModeInfo[$display_type];
  }

  /**
   * Returns the entity display mode info for a specific entity type.
   *
   * @param string $display_type
   *   The display type to be retrieved. It can be "view_mode" or "form_mode".
   * @param string $entity_type_id
   *   The entity type whose display mode info should be returned.
   *
   * @return array
   *   The display mode info for a specific entity type.
   */
  protected function getDisplayModesByEntityType($display_type, $entity_type_id) {
    if (isset($this->displayModeInfo[$display_type][$entity_type_id])) {
      return $this->displayModeInfo[$display_type][$entity_type_id];
    }
    else {
      $display_modes = $this->getAllDisplayModesByEntityType($display_type);
      if (isset($display_modes[$entity_type_id])) {
        return $display_modes[$entity_type_id];
      }
    }
    return array();
  }

  /**
   * {@inheritdoc}
   */
  public function getViewModeOptions($entity_type, $include_disabled = FALSE) {
    return $this->getDisplayModeOptions('view_mode', $entity_type, $include_disabled);
  }

  /**
   * {@inheritdoc}
   */
  public function getFormModeOptions($entity_type, $include_disabled = FALSE) {
    return $this->getDisplayModeOptions('form_mode', $entity_type, $include_disabled);
  }

  /**
   * Returns an array of display mode options.
   *
   * @param string $display_type
   *   The display type to be retrieved. It can be "view_mode" or "form_mode".
   * @param string $entity_type_id
   *   The entity type whose display mode options should be returned.
   * @param bool $include_disabled
   *   Force to include disabled display modes. Defaults to FALSE.
   *
   * @return array
   *   An array of display mode labels, keyed by the display mode ID.
   */
  protected function getDisplayModeOptions($display_type, $entity_type_id, $include_disabled = FALSE) {
    $options = array('default' => t('Default'));
    foreach ($this->getDisplayModesByEntityType($display_type, $entity_type_id) as $mode => $settings) {
      if (!empty($settings['status']) || $include_disabled) {
        $options[$mode] = $settings['label'];
      }
    }
    return $options;
  }

641
}