EntityDisplayModeBase.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Config\Entity\ConfigEntityBase;
  4. use Drupal\Core\Config\Entity\ConfigEntityInterface;
  5. /**
  6. * Base class for config entity types with settings for form and view modes.
  7. */
  8. abstract class EntityDisplayModeBase extends ConfigEntityBase implements EntityDisplayModeInterface {
  9. /**
  10. * The ID of the form or view mode.
  11. *
  12. * @var string
  13. */
  14. protected $id;
  15. /**
  16. * The human-readable name of the form or view mode.
  17. *
  18. * @var string
  19. */
  20. protected $label;
  21. /**
  22. * The entity type this form or view mode is used for.
  23. *
  24. * This is not to be confused with EntityDisplayModeBase::$entityType which is
  25. * inherited from Entity::$entityType.
  26. *
  27. * @var string
  28. */
  29. protected $targetEntityType;
  30. /**
  31. * Whether or not this form or view mode has custom settings by default.
  32. *
  33. * If FALSE, entities displayed in this mode will reuse the 'default' display
  34. * settings by default (e.g. right after the module exposing the form or view
  35. * mode is enabled), but administrators can later use the Field UI to apply
  36. * custom display settings specific to the form or view mode.
  37. *
  38. * @var bool
  39. */
  40. protected $status = TRUE;
  41. /**
  42. * Whether or not the rendered output of this view mode is cached by default.
  43. *
  44. * @var bool
  45. */
  46. protected $cache = TRUE;
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
  51. /** @var \Drupal\Core\Entity\EntityDisplayModeInterface $a */
  52. /** @var \Drupal\Core\Entity\EntityDisplayModeInterface $b */
  53. // Sort by the type of entity the view mode is used for.
  54. $a_type = $a->getTargetType();
  55. $b_type = $b->getTargetType();
  56. $type_order = strnatcasecmp($a_type, $b_type);
  57. return $type_order != 0 ? $type_order : parent::sort($a, $b);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getTargetType() {
  63. return $this->targetEntityType;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function setTargetType($target_entity_type) {
  69. $this->targetEntityType = $target_entity_type;
  70. return $this;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function calculateDependencies() {
  76. parent::calculateDependencies();
  77. $target_entity_type = \Drupal::entityTypeManager()->getDefinition($this->targetEntityType);
  78. $this->addDependency('module', $target_entity_type->getProvider());
  79. return $this;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function preSave(EntityStorageInterface $storage) {
  85. parent::preSave($storage);
  86. \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public static function preDelete(EntityStorageInterface $storage, array $entities) {
  92. parent::preDelete($storage, $entities);
  93. \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. protected function urlRouteParameters($rel) {
  99. $uri_route_parameters = parent::urlRouteParameters($rel);
  100. if ($rel === 'add-form') {
  101. $uri_route_parameters['entity_type_id'] = $this->getTargetType();
  102. }
  103. return $uri_route_parameters;
  104. }
  105. }