EntityType.php 20 KB

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