annotate core/modules/taxonomy/src/Entity/Vocabulary.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\taxonomy\Entity;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
Chris@0 6 use Drupal\Core\Entity\EntityStorageInterface;
Chris@0 7 use Drupal\taxonomy\VocabularyInterface;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Defines the taxonomy vocabulary entity.
Chris@0 11 *
Chris@0 12 * @ConfigEntityType(
Chris@0 13 * id = "taxonomy_vocabulary",
Chris@0 14 * label = @Translation("Taxonomy vocabulary"),
Chris@14 15 * label_singular = @Translation("vocabulary"),
Chris@14 16 * label_plural = @Translation("vocabularies"),
Chris@14 17 * label_collection = @Translation("Taxonomy"),
Chris@14 18 * label_count = @PluralTranslation(
Chris@14 19 * singular = "@count vocabulary",
Chris@14 20 * plural = "@count vocabularies"
Chris@14 21 * ),
Chris@0 22 * handlers = {
Chris@0 23 * "storage" = "Drupal\taxonomy\VocabularyStorage",
Chris@0 24 * "list_builder" = "Drupal\taxonomy\VocabularyListBuilder",
Chris@14 25 * "access" = "Drupal\taxonomy\VocabularyAccessControlHandler",
Chris@0 26 * "form" = {
Chris@0 27 * "default" = "Drupal\taxonomy\VocabularyForm",
Chris@0 28 * "reset" = "Drupal\taxonomy\Form\VocabularyResetForm",
Chris@14 29 * "delete" = "Drupal\taxonomy\Form\VocabularyDeleteForm",
Chris@14 30 * "overview" = "Drupal\taxonomy\Form\OverviewTerms"
Chris@14 31 * },
Chris@14 32 * "route_provider" = {
Chris@14 33 * "html" = "Drupal\taxonomy\Entity\Routing\VocabularyRouteProvider",
Chris@0 34 * }
Chris@0 35 * },
Chris@0 36 * admin_permission = "administer taxonomy",
Chris@0 37 * config_prefix = "vocabulary",
Chris@0 38 * bundle_of = "taxonomy_term",
Chris@0 39 * entity_keys = {
Chris@0 40 * "id" = "vid",
Chris@0 41 * "label" = "name",
Chris@0 42 * "weight" = "weight"
Chris@0 43 * },
Chris@0 44 * links = {
Chris@14 45 * "add-form" = "/admin/structure/taxonomy/add",
Chris@0 46 * "delete-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/delete",
Chris@0 47 * "reset-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/reset",
Chris@0 48 * "overview-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/overview",
Chris@0 49 * "edit-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}",
Chris@0 50 * "collection" = "/admin/structure/taxonomy",
Chris@0 51 * },
Chris@0 52 * config_export = {
Chris@0 53 * "name",
Chris@0 54 * "vid",
Chris@0 55 * "description",
Chris@0 56 * "weight",
Chris@0 57 * }
Chris@0 58 * )
Chris@0 59 */
Chris@0 60 class Vocabulary extends ConfigEntityBundleBase implements VocabularyInterface {
Chris@0 61
Chris@0 62 /**
Chris@0 63 * The taxonomy vocabulary ID.
Chris@0 64 *
Chris@0 65 * @var string
Chris@0 66 */
Chris@0 67 protected $vid;
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Name of the vocabulary.
Chris@0 71 *
Chris@0 72 * @var string
Chris@0 73 */
Chris@0 74 protected $name;
Chris@0 75
Chris@0 76 /**
Chris@0 77 * Description of the vocabulary.
Chris@0 78 *
Chris@0 79 * @var string
Chris@0 80 */
Chris@0 81 protected $description;
Chris@0 82
Chris@0 83 /**
Chris@0 84 * The weight of this vocabulary in relation to other vocabularies.
Chris@0 85 *
Chris@0 86 * @var int
Chris@0 87 */
Chris@0 88 protected $weight = 0;
Chris@0 89
Chris@0 90 /**
Chris@0 91 * {@inheritdoc}
Chris@0 92 */
Chris@0 93 public function getHierarchy() {
Chris@18 94 @trigger_error('\Drupal\taxonomy\VocabularyInterface::getHierarchy() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.x. Use \Drupal\taxonomy\TermStorage::getVocabularyHierarchyType() instead.', E_USER_DEPRECATED);
Chris@18 95 return $this->entityTypeManager()->getStorage('taxonomy_term')->getVocabularyHierarchyType($this->id());
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * {@inheritdoc}
Chris@0 100 */
Chris@0 101 public function setHierarchy($hierarchy) {
Chris@18 102 @trigger_error('\Drupal\taxonomy\VocabularyInterface::setHierarchy() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.x. Reset the cache of the taxonomy_term storage controller instead.', E_USER_DEPRECATED);
Chris@18 103 $this->entityTypeManager()->getStorage('taxonomy_term')->resetCache();
Chris@0 104 return $this;
Chris@0 105 }
Chris@0 106
Chris@0 107 /**
Chris@0 108 * {@inheritdoc}
Chris@0 109 */
Chris@0 110 public function id() {
Chris@0 111 return $this->vid;
Chris@0 112 }
Chris@0 113
Chris@0 114 /**
Chris@0 115 * {@inheritdoc}
Chris@0 116 */
Chris@0 117 public function getDescription() {
Chris@0 118 return $this->description;
Chris@0 119 }
Chris@0 120
Chris@0 121 /**
Chris@0 122 * {@inheritdoc}
Chris@0 123 */
Chris@0 124 public static function preDelete(EntityStorageInterface $storage, array $entities) {
Chris@0 125 parent::preDelete($storage, $entities);
Chris@0 126
Chris@0 127 // Only load terms without a parent, child terms will get deleted too.
Chris@0 128 entity_delete_multiple('taxonomy_term', $storage->getToplevelTids(array_keys($entities)));
Chris@0 129 }
Chris@0 130
Chris@0 131 /**
Chris@0 132 * {@inheritdoc}
Chris@0 133 */
Chris@0 134 public static function postDelete(EntityStorageInterface $storage, array $entities) {
Chris@0 135 parent::postDelete($storage, $entities);
Chris@0 136
Chris@0 137 // Reset caches.
Chris@0 138 $storage->resetCache(array_keys($entities));
Chris@0 139
Chris@0 140 if (reset($entities)->isSyncing()) {
Chris@0 141 return;
Chris@0 142 }
Chris@0 143
Chris@0 144 $vocabularies = [];
Chris@0 145 foreach ($entities as $vocabulary) {
Chris@0 146 $vocabularies[$vocabulary->id()] = $vocabulary->id();
Chris@0 147 }
Chris@0 148 // Load all Taxonomy module fields and delete those which use only this
Chris@0 149 // vocabulary.
Chris@0 150 $field_storages = entity_load_multiple_by_properties('field_storage_config', ['module' => 'taxonomy']);
Chris@0 151 foreach ($field_storages as $field_storage) {
Chris@0 152 $modified_storage = FALSE;
Chris@0 153 // Term reference fields may reference terms from more than one
Chris@0 154 // vocabulary.
Chris@0 155 foreach ($field_storage->getSetting('allowed_values') as $key => $allowed_value) {
Chris@0 156 if (isset($vocabularies[$allowed_value['vocabulary']])) {
Chris@0 157 $allowed_values = $field_storage->getSetting('allowed_values');
Chris@0 158 unset($allowed_values[$key]);
Chris@0 159 $field_storage->setSetting('allowed_values', $allowed_values);
Chris@0 160 $modified_storage = TRUE;
Chris@0 161 }
Chris@0 162 }
Chris@0 163 if ($modified_storage) {
Chris@0 164 $allowed_values = $field_storage->getSetting('allowed_values');
Chris@0 165 if (empty($allowed_values)) {
Chris@0 166 $field_storage->delete();
Chris@0 167 }
Chris@0 168 else {
Chris@0 169 // Update the field definition with the new allowed values.
Chris@0 170 $field_storage->save();
Chris@0 171 }
Chris@0 172 }
Chris@0 173 }
Chris@0 174 }
Chris@0 175
Chris@0 176 }