EntityType.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Component\Plugin\Definition\PluginDefinition;
  4. use Drupal\Core\DependencyInjection\DependencySerializationTrait;
  5. use Drupal\Core\Entity\Exception\EntityTypeIdLengthException;
  6. use Drupal\Core\StringTranslation\StringTranslationTrait;
  7. use Drupal\Core\StringTranslation\TranslatableMarkup;
  8. /**
  9. * Provides an implementation of an entity type and its metadata.
  10. *
  11. * @ingroup entity_api
  12. */
  13. class EntityType extends PluginDefinition implements EntityTypeInterface {
  14. use DependencySerializationTrait;
  15. use StringTranslationTrait;
  16. /**
  17. * Indicates whether entities should be statically cached.
  18. *
  19. * @var bool
  20. */
  21. protected $static_cache = TRUE;
  22. /**
  23. * Indicates whether the rendered output of entities should be cached.
  24. *
  25. * @var bool
  26. */
  27. protected $render_cache = TRUE;
  28. /**
  29. * Indicates if the persistent cache of field data should be used.
  30. *
  31. * @var bool
  32. */
  33. protected $persistent_cache = TRUE;
  34. /**
  35. * An array of entity keys.
  36. *
  37. * @var array
  38. */
  39. protected $entity_keys = [];
  40. /**
  41. * The unique identifier of this entity type.
  42. *
  43. * @var string
  44. */
  45. protected $id;
  46. /**
  47. * The name of the original entity type class.
  48. *
  49. * This is only set if the class name is changed.
  50. *
  51. * @var string
  52. */
  53. protected $originalClass;
  54. /**
  55. * An array of handlers.
  56. *
  57. * @var array
  58. */
  59. protected $handlers = [];
  60. /**
  61. * The name of the default administrative permission.
  62. *
  63. * @var string
  64. */
  65. protected $admin_permission;
  66. /**
  67. * The permission granularity level.
  68. *
  69. * The allowed values are respectively "entity_type" or "bundle".
  70. *
  71. * @var string
  72. */
  73. protected $permission_granularity = 'entity_type';
  74. /**
  75. * Link templates using the URI template syntax.
  76. *
  77. * @var array
  78. */
  79. protected $links = [];
  80. /**
  81. * The name of a callback that returns the label of the entity.
  82. *
  83. * @var callable|null
  84. *
  85. * @deprecated in Drupal 8.0.x-dev and will be removed before Drupal 9.0.0.
  86. * Use Drupal\Core\Entity\EntityInterface::label() for complex label
  87. * generation as needed.
  88. *
  89. * @see \Drupal\Core\Entity\EntityInterface::label()
  90. *
  91. * @todo Remove usages of label_callback https://www.drupal.org/node/2450793.
  92. */
  93. protected $label_callback = NULL;
  94. /**
  95. * The name of the entity type which provides bundles.
  96. *
  97. * @var string
  98. */
  99. protected $bundle_entity_type = NULL;
  100. /**
  101. * The name of the entity type for which bundles are provided.
  102. *
  103. * @var string|null
  104. */
  105. protected $bundle_of = NULL;
  106. /**
  107. * The human-readable name of the entity bundles, e.g. Vocabulary.
  108. *
  109. * @var string|null
  110. */
  111. protected $bundle_label = NULL;
  112. /**
  113. * The name of the entity type's base table.
  114. *
  115. * @var string|null
  116. */
  117. protected $base_table = NULL;
  118. /**
  119. * The name of the entity type's revision data table.
  120. *
  121. * @var string|null
  122. */
  123. protected $revision_data_table = NULL;
  124. /**
  125. * The name of the entity type's revision table.
  126. *
  127. * @var string|null
  128. */
  129. protected $revision_table = NULL;
  130. /**
  131. * The name of the entity type's data table.
  132. *
  133. * @var string|null
  134. */
  135. protected $data_table = NULL;
  136. /**
  137. * Indicates whether the entity data is internal.
  138. *
  139. * @var bool
  140. */
  141. protected $internal = FALSE;
  142. /**
  143. * Indicates whether entities of this type have multilingual support.
  144. *
  145. * @var bool
  146. */
  147. protected $translatable = FALSE;
  148. /**
  149. * Indicates whether the revision form fields should be added to the form.
  150. *
  151. * @var bool
  152. */
  153. protected $show_revision_ui = FALSE;
  154. /**
  155. * The human-readable name of the type.
  156. *
  157. * @var string
  158. *
  159. * @see \Drupal\Core\Entity\EntityTypeInterface::getLabel()
  160. */
  161. protected $label = '';
  162. /**
  163. * The human-readable label for a collection of entities of the type.
  164. *
  165. * @var string
  166. *
  167. * @see \Drupal\Core\Entity\EntityTypeInterface::getCollectionLabel()
  168. */
  169. protected $label_collection = '';
  170. /**
  171. * The indefinite singular name of the type.
  172. *
  173. * @var string
  174. *
  175. * @see \Drupal\Core\Entity\EntityTypeInterface::getSingularLabel()
  176. */
  177. protected $label_singular = '';
  178. /**
  179. * The indefinite plural name of the type.
  180. *
  181. * @var string
  182. *
  183. * @see \Drupal\Core\Entity\EntityTypeInterface::getPluralLabel()
  184. */
  185. protected $label_plural = '';
  186. /**
  187. * A definite singular/plural name of the type.
  188. *
  189. * Needed keys: "singular" and "plural".
  190. *
  191. * @var string[]
  192. *
  193. * @see \Drupal\Core\Entity\EntityTypeInterface::getCountLabel()
  194. */
  195. protected $label_count = [];
  196. /**
  197. * A callable that can be used to provide the entity URI.
  198. *
  199. * @var callable|null
  200. */
  201. protected $uri_callback = NULL;
  202. /**
  203. * The machine name of the entity type group.
  204. */
  205. protected $group;
  206. /**
  207. * The human-readable name of the entity type group.
  208. */
  209. protected $group_label;
  210. /**
  211. * The route name used by field UI to attach its management pages.
  212. *
  213. * @var string
  214. */
  215. protected $field_ui_base_route;
  216. /**
  217. * Indicates whether this entity type is commonly used as a reference target.
  218. *
  219. * This is used by the Entity reference field to promote an entity type in the
  220. * add new field select list in Field UI.
  221. *
  222. * @var bool
  223. */
  224. protected $common_reference_target = FALSE;
  225. /**
  226. * The list cache contexts for this entity type.
  227. *
  228. * @var string[]
  229. */
  230. protected $list_cache_contexts = [];
  231. /**
  232. * The list cache tags for this entity type.
  233. *
  234. * @var string[]
  235. */
  236. protected $list_cache_tags = [];
  237. /**
  238. * Entity constraint definitions.
  239. *
  240. * @var array[]
  241. */
  242. protected $constraints = [];
  243. /**
  244. * Any additional properties and values.
  245. *
  246. * @var array
  247. */
  248. protected $additional = [];
  249. /**
  250. * Constructs a new EntityType.
  251. *
  252. * @param array $definition
  253. * An array of values from the annotation.
  254. *
  255. * @throws \Drupal\Core\Entity\Exception\EntityTypeIdLengthException
  256. * Thrown when attempting to instantiate an entity type with too long ID.
  257. */
  258. public function __construct($definition) {
  259. // Throw an exception if the entity type ID is longer than 32 characters.
  260. if (mb_strlen($definition['id']) > static::ID_MAX_LENGTH) {
  261. throw new EntityTypeIdLengthException('Attempt to create an entity type with an ID longer than ' . static::ID_MAX_LENGTH . " characters: {$definition['id']}.");
  262. }
  263. foreach ($definition as $property => $value) {
  264. $this->set($property, $value);
  265. }
  266. // Ensure defaults.
  267. $this->entity_keys += [
  268. 'revision' => '',
  269. 'bundle' => '',
  270. 'langcode' => '',
  271. 'default_langcode' => 'default_langcode',
  272. 'revision_translation_affected' => 'revision_translation_affected',
  273. ];
  274. $this->handlers += [
  275. 'access' => 'Drupal\Core\Entity\EntityAccessControlHandler',
  276. ];
  277. if (isset($this->handlers['storage'])) {
  278. $this->checkStorageClass($this->handlers['storage']);
  279. }
  280. // Automatically add the "EntityChanged" constraint if the entity type
  281. // tracks the changed time.
  282. if ($this->entityClassImplements(EntityChangedInterface::class)) {
  283. $this->addConstraint('EntityChanged');
  284. }
  285. // Automatically add the "EntityUntranslatableFields" constraint if we have
  286. // an entity type supporting translatable fields and pending revisions.
  287. if ($this->entityClassImplements(ContentEntityInterface::class)) {
  288. $this->addConstraint('EntityUntranslatableFields');
  289. }
  290. // Ensure a default list cache tag is set.
  291. if (empty($this->list_cache_tags)) {
  292. $this->list_cache_tags = [$definition['id'] . '_list'];
  293. }
  294. }
  295. /**
  296. * {@inheritdoc}
  297. */
  298. public function get($property) {
  299. if (property_exists($this, $property)) {
  300. $value = isset($this->{$property}) ? $this->{$property} : NULL;
  301. }
  302. else {
  303. $value = isset($this->additional[$property]) ? $this->additional[$property] : NULL;
  304. }
  305. return $value;
  306. }
  307. /**
  308. * {@inheritdoc}
  309. */
  310. public function set($property, $value) {
  311. if (property_exists($this, $property)) {
  312. $this->{$property} = $value;
  313. }
  314. else {
  315. $this->additional[$property] = $value;
  316. }
  317. return $this;
  318. }
  319. /**
  320. * {@inheritdoc}
  321. */
  322. public function isInternal() {
  323. return $this->internal;
  324. }
  325. /**
  326. * {@inheritdoc}
  327. */
  328. public function isStaticallyCacheable() {
  329. return $this->static_cache;
  330. }
  331. /**
  332. * {@inheritdoc}
  333. */
  334. public function isRenderCacheable() {
  335. return $this->render_cache;
  336. }
  337. /**
  338. * {@inheritdoc}
  339. */
  340. public function isPersistentlyCacheable() {
  341. return $this->persistent_cache;
  342. }
  343. /**
  344. * {@inheritdoc}
  345. */
  346. public function getKeys() {
  347. return $this->entity_keys;
  348. }
  349. /**
  350. * {@inheritdoc}
  351. */
  352. public function getKey($key) {
  353. $keys = $this->getKeys();
  354. return isset($keys[$key]) ? $keys[$key] : FALSE;
  355. }
  356. /**
  357. * {@inheritdoc}
  358. */
  359. public function hasKey($key) {
  360. $keys = $this->getKeys();
  361. return !empty($keys[$key]);
  362. }
  363. /**
  364. * {@inheritdoc}
  365. */
  366. public function getOriginalClass() {
  367. return $this->originalClass ?: $this->class;
  368. }
  369. /**
  370. * {@inheritdoc}
  371. */
  372. public function setClass($class) {
  373. if (!$this->originalClass && $this->class) {
  374. // If the original class is currently not set, set it to the current
  375. // class, assume that is the original class name.
  376. $this->originalClass = $this->class;
  377. }
  378. return parent::setClass($class);
  379. }
  380. /**
  381. * {@inheritdoc}
  382. */
  383. public function entityClassImplements($interface) {
  384. return is_subclass_of($this->getClass(), $interface);
  385. }
  386. /**
  387. * {@inheritdoc}
  388. */
  389. public function isSubclassOf($class) {
  390. return $this->entityClassImplements($class);
  391. }
  392. /**
  393. * {@inheritdoc}
  394. */
  395. public function getHandlerClasses() {
  396. return $this->handlers;
  397. }
  398. /**
  399. * {@inheritdoc}
  400. */
  401. public function getHandlerClass($handler_type, $nested = FALSE) {
  402. if ($this->hasHandlerClass($handler_type, $nested)) {
  403. $handlers = $this->getHandlerClasses();
  404. return $nested ? $handlers[$handler_type][$nested] : $handlers[$handler_type];
  405. }
  406. }
  407. /**
  408. * {@inheritdoc}
  409. */
  410. public function setHandlerClass($handler_type, $value) {
  411. $this->handlers[$handler_type] = $value;
  412. return $this;
  413. }
  414. /**
  415. * {@inheritdoc}
  416. */
  417. public function hasHandlerClass($handler_type, $nested = FALSE) {
  418. $handlers = $this->getHandlerClasses();
  419. if (!isset($handlers[$handler_type]) || ($nested && !isset($handlers[$handler_type][$nested]))) {
  420. return FALSE;
  421. }
  422. $handler = $handlers[$handler_type];
  423. if ($nested) {
  424. $handler = $handler[$nested];
  425. }
  426. return class_exists($handler);
  427. }
  428. /**
  429. * {@inheritdoc}
  430. */
  431. public function getStorageClass() {
  432. return $this->getHandlerClass('storage');
  433. }
  434. /**
  435. * {@inheritdoc}
  436. */
  437. public function setStorageClass($class) {
  438. $this->checkStorageClass($class);
  439. $this->handlers['storage'] = $class;
  440. return $this;
  441. }
  442. /**
  443. * Checks that the provided class is compatible with the current entity type.
  444. *
  445. * @param string $class
  446. * The class to check.
  447. */
  448. protected function checkStorageClass($class) {
  449. // Nothing to check by default.
  450. }
  451. /**
  452. * {@inheritdoc}
  453. */
  454. public function getFormClass($operation) {
  455. return $this->getHandlerClass('form', $operation);
  456. }
  457. /**
  458. * {@inheritdoc}
  459. */
  460. public function setFormClass($operation, $class) {
  461. $this->handlers['form'][$operation] = $class;
  462. return $this;
  463. }
  464. /**
  465. * {@inheritdoc}
  466. */
  467. public function hasFormClasses() {
  468. return !empty($this->handlers['form']);
  469. }
  470. /**
  471. * {@inheritdoc}
  472. */
  473. public function hasRouteProviders() {
  474. return !empty($this->handlers['route_provider']);
  475. }
  476. /**
  477. * {@inheritdoc}
  478. */
  479. public function getListBuilderClass() {
  480. return $this->getHandlerClass('list_builder');
  481. }
  482. /**
  483. * {@inheritdoc}
  484. */
  485. public function setListBuilderClass($class) {
  486. $this->handlers['list_builder'] = $class;
  487. return $this;
  488. }
  489. /**
  490. * {@inheritdoc}
  491. */
  492. public function hasListBuilderClass() {
  493. return $this->hasHandlerClass('list_builder');
  494. }
  495. /**
  496. * {@inheritdoc}
  497. */
  498. public function getViewBuilderClass() {
  499. return $this->getHandlerClass('view_builder');
  500. }
  501. /**
  502. * {@inheritdoc}
  503. */
  504. public function setViewBuilderClass($class) {
  505. $this->handlers['view_builder'] = $class;
  506. return $this;
  507. }
  508. /**
  509. * {@inheritdoc}
  510. */
  511. public function hasViewBuilderClass() {
  512. return $this->hasHandlerClass('view_builder');
  513. }
  514. /**
  515. * {@inheritdoc}
  516. */
  517. public function getRouteProviderClasses() {
  518. return !empty($this->handlers['route_provider']) ? $this->handlers['route_provider'] : [];
  519. }
  520. /**
  521. * {@inheritdoc}
  522. */
  523. public function getAccessControlClass() {
  524. return $this->getHandlerClass('access');
  525. }
  526. /**
  527. * {@inheritdoc}
  528. */
  529. public function setAccessClass($class) {
  530. $this->handlers['access'] = $class;
  531. return $this;
  532. }
  533. /**
  534. * {@inheritdoc}
  535. */
  536. public function getAdminPermission() {
  537. return $this->admin_permission ?: FALSE;
  538. }
  539. /**
  540. * {@inheritdoc}
  541. */
  542. public function getPermissionGranularity() {
  543. return $this->permission_granularity;
  544. }
  545. /**
  546. * {@inheritdoc}
  547. */
  548. public function getLinkTemplates() {
  549. return $this->links;
  550. }
  551. /**
  552. * {@inheritdoc}
  553. */
  554. public function getLinkTemplate($key) {
  555. $links = $this->getLinkTemplates();
  556. return isset($links[$key]) ? $links[$key] : FALSE;
  557. }
  558. /**
  559. * {@inheritdoc}
  560. */
  561. public function hasLinkTemplate($key) {
  562. $links = $this->getLinkTemplates();
  563. return isset($links[$key]);
  564. }
  565. /**
  566. * {@inheritdoc}
  567. */
  568. public function setLinkTemplate($key, $path) {
  569. if ($path[0] !== '/') {
  570. throw new \InvalidArgumentException('Link templates accepts paths, which have to start with a leading slash.');
  571. }
  572. $this->links[$key] = $path;
  573. return $this;
  574. }
  575. /**
  576. * {@inheritdoc}
  577. */
  578. public function getLabelCallback() {
  579. return $this->label_callback;
  580. }
  581. /**
  582. * {@inheritdoc}
  583. */
  584. public function setLabelCallback($callback) {
  585. $this->label_callback = $callback;
  586. return $this;
  587. }
  588. /**
  589. * {@inheritdoc}
  590. */
  591. public function hasLabelCallback() {
  592. return isset($this->label_callback);
  593. }
  594. /**
  595. * {@inheritdoc}
  596. */
  597. public function getBundleEntityType() {
  598. return $this->bundle_entity_type;
  599. }
  600. /**
  601. * {@inheritdoc}
  602. */
  603. public function getBundleOf() {
  604. return $this->bundle_of;
  605. }
  606. /**
  607. * {@inheritdoc}
  608. */
  609. public function getBundleLabel() {
  610. // If there is no bundle label defined, try to provide some sensible
  611. // fallbacks.
  612. if (!empty($this->bundle_label)) {
  613. return (string) $this->bundle_label;
  614. }
  615. elseif ($bundle_entity_type_id = $this->getBundleEntityType()) {
  616. return (string) \Drupal::entityTypeManager()->getDefinition($bundle_entity_type_id)->getLabel();
  617. }
  618. return (string) new TranslatableMarkup('@type_label bundle', ['@type_label' => $this->getLabel()], [], $this->getStringTranslation());
  619. }
  620. /**
  621. * {@inheritdoc}
  622. */
  623. public function getBaseTable() {
  624. return $this->base_table;
  625. }
  626. /**
  627. * {@inheritdoc}
  628. */
  629. public function showRevisionUi() {
  630. return $this->isRevisionable() && $this->show_revision_ui;
  631. }
  632. /**
  633. * {@inheritdoc}
  634. */
  635. public function isTranslatable() {
  636. return !empty($this->translatable);
  637. }
  638. /**
  639. * {@inheritdoc}
  640. */
  641. public function isRevisionable() {
  642. // Entity types are revisionable if a revision key has been specified.
  643. return $this->hasKey('revision');
  644. }
  645. /**
  646. * {@inheritdoc}
  647. */
  648. public function getRevisionDataTable() {
  649. return $this->revision_data_table;
  650. }
  651. /**
  652. * {@inheritdoc}
  653. */
  654. public function getRevisionTable() {
  655. return $this->revision_table;
  656. }
  657. /**
  658. * {@inheritdoc}
  659. */
  660. public function getDataTable() {
  661. return $this->data_table;
  662. }
  663. /**
  664. * {@inheritdoc}
  665. */
  666. public function getLabel() {
  667. return $this->label;
  668. }
  669. /**
  670. * {@inheritdoc}
  671. */
  672. public function getLowercaseLabel() {
  673. return mb_strtolower($this->getLabel());
  674. }
  675. /**
  676. * {@inheritdoc}
  677. */
  678. public function getCollectionLabel() {
  679. if (empty($this->label_collection)) {
  680. $label = $this->getLabel();
  681. $this->label_collection = new TranslatableMarkup('@label entities', ['@label' => $label], [], $this->getStringTranslation());
  682. }
  683. return $this->label_collection;
  684. }
  685. /**
  686. * {@inheritdoc}
  687. */
  688. public function getSingularLabel() {
  689. if (empty($this->label_singular)) {
  690. $lowercase_label = $this->getLowercaseLabel();
  691. $this->label_singular = $lowercase_label;
  692. }
  693. return $this->label_singular;
  694. }
  695. /**
  696. * {@inheritdoc}
  697. */
  698. public function getPluralLabel() {
  699. if (empty($this->label_plural)) {
  700. $lowercase_label = $this->getLowercaseLabel();
  701. $this->label_plural = new TranslatableMarkup('@label entities', ['@label' => $lowercase_label], [], $this->getStringTranslation());
  702. }
  703. return $this->label_plural;
  704. }
  705. /**
  706. * {@inheritdoc}
  707. */
  708. public function getCountLabel($count) {
  709. if (empty($this->label_count)) {
  710. return $this->formatPlural($count, '@count @label', '@count @label entities', ['@label' => $this->getLowercaseLabel()], ['context' => 'Entity type label']);
  711. }
  712. $context = isset($this->label_count['context']) ? $this->label_count['context'] : 'Entity type label';
  713. return $this->formatPlural($count, $this->label_count['singular'], $this->label_count['plural'], ['context' => $context]);
  714. }
  715. /**
  716. * {@inheritdoc}
  717. */
  718. public function getUriCallback() {
  719. return $this->uri_callback;
  720. }
  721. /**
  722. * {@inheritdoc}
  723. */
  724. public function setUriCallback($callback) {
  725. $this->uri_callback = $callback;
  726. return $this;
  727. }
  728. /**
  729. * {@inheritdoc}
  730. */
  731. public function getGroup() {
  732. return $this->group;
  733. }
  734. /**
  735. * {@inheritdoc}
  736. */
  737. public function getGroupLabel() {
  738. return !empty($this->group_label) ? $this->group_label : $this->t('Other', [], ['context' => 'Entity type group']);
  739. }
  740. /**
  741. * {@inheritdoc}
  742. */
  743. public function getListCacheContexts() {
  744. return $this->list_cache_contexts;
  745. }
  746. /**
  747. * {@inheritdoc}
  748. */
  749. public function getListCacheTags() {
  750. return $this->list_cache_tags;
  751. }
  752. /**
  753. * {@inheritdoc}
  754. */
  755. public function getConfigDependencyKey() {
  756. // Return 'content' for the default implementation as important distinction
  757. // is that dependencies on other configuration entities are hard
  758. // dependencies and have to exist before creating the dependent entity.
  759. return 'content';
  760. }
  761. /**
  762. * {@inheritdoc}
  763. */
  764. public function isCommonReferenceTarget() {
  765. return $this->common_reference_target;
  766. }
  767. /**
  768. * {@inheritdoc}
  769. */
  770. public function getConstraints() {
  771. return $this->constraints;
  772. }
  773. /**
  774. * {@inheritdoc}
  775. */
  776. public function setConstraints(array $constraints) {
  777. $this->constraints = $constraints;
  778. return $this;
  779. }
  780. /**
  781. * {@inheritdoc}
  782. */
  783. public function addConstraint($constraint_name, $options = NULL) {
  784. $this->constraints[$constraint_name] = $options;
  785. return $this;
  786. }
  787. /**
  788. * {@inheritdoc}
  789. */
  790. public function getBundleConfigDependency($bundle) {
  791. // If this entity type uses entities to manage its bundles then depend on
  792. // the bundle entity.
  793. if ($bundle_entity_type_id = $this->getBundleEntityType()) {
  794. if (!$bundle_entity = \Drupal::entityManager()->getStorage($bundle_entity_type_id)->load($bundle)) {
  795. throw new \LogicException(sprintf('Missing bundle entity, entity type %s, entity id %s.', $bundle_entity_type_id, $bundle));
  796. }
  797. $config_dependency = [
  798. 'type' => 'config',
  799. 'name' => $bundle_entity->getConfigDependencyName(),
  800. ];
  801. }
  802. else {
  803. // Depend on the provider of the entity type.
  804. $config_dependency = [
  805. 'type' => 'module',
  806. 'name' => $this->getProvider(),
  807. ];
  808. }
  809. return $config_dependency;
  810. }
  811. }