annotate core/modules/taxonomy/src/Entity/Vocabulary.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
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@0 15 * label_singular = @Translation("vocabulary"),
Chris@0 16 * label_plural = @Translation("vocabularies"),
Chris@0 17 * label_collection = @Translation("Taxonomy"),
Chris@0 18 * label_count = @PluralTranslation(
Chris@0 19 * singular = "@count vocabulary",
Chris@0 20 * plural = "@count vocabularies"
Chris@0 21 * ),
Chris@0 22 * handlers = {
Chris@0 23 * "storage" = "Drupal\taxonomy\VocabularyStorage",
Chris@0 24 * "list_builder" = "Drupal\taxonomy\VocabularyListBuilder",
Chris@0 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@0 29 * "delete" = "Drupal\taxonomy\Form\VocabularyDeleteForm",
Chris@0 30 * "overview" = "Drupal\taxonomy\Form\OverviewTerms"
Chris@0 31 * },
Chris@0 32 * "route_provider" = {
Chris@0 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@0 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 * "hierarchy",
Chris@0 57 * "weight",
Chris@0 58 * }
Chris@0 59 * )
Chris@0 60 */
Chris@0 61 class Vocabulary extends ConfigEntityBundleBase implements VocabularyInterface {
Chris@0 62
Chris@0 63 /**
Chris@0 64 * The taxonomy vocabulary ID.
Chris@0 65 *
Chris@0 66 * @var string
Chris@0 67 */
Chris@0 68 protected $vid;
Chris@0 69
Chris@0 70 /**
Chris@0 71 * Name of the vocabulary.
Chris@0 72 *
Chris@0 73 * @var string
Chris@0 74 */
Chris@0 75 protected $name;
Chris@0 76
Chris@0 77 /**
Chris@0 78 * Description of the vocabulary.
Chris@0 79 *
Chris@0 80 * @var string
Chris@0 81 */
Chris@0 82 protected $description;
Chris@0 83
Chris@0 84 /**
Chris@0 85 * The type of hierarchy allowed within the vocabulary.
Chris@0 86 *
Chris@0 87 * Possible values:
Chris@0 88 * - VocabularyInterface::HIERARCHY_DISABLED: No parents.
Chris@0 89 * - VocabularyInterface::HIERARCHY_SINGLE: Single parent.
Chris@0 90 * - VocabularyInterface::HIERARCHY_MULTIPL: Multiple parents.
Chris@0 91 *
Chris@0 92 * @var int
Chris@0 93 */
Chris@0 94 protected $hierarchy = VocabularyInterface::HIERARCHY_DISABLED;
Chris@0 95
Chris@0 96 /**
Chris@0 97 * The weight of this vocabulary in relation to other vocabularies.
Chris@0 98 *
Chris@0 99 * @var int
Chris@0 100 */
Chris@0 101 protected $weight = 0;
Chris@0 102
Chris@0 103 /**
Chris@0 104 * {@inheritdoc}
Chris@0 105 */
Chris@0 106 public function getHierarchy() {
Chris@0 107 return $this->hierarchy;
Chris@0 108 }
Chris@0 109
Chris@0 110 /**
Chris@0 111 * {@inheritdoc}
Chris@0 112 */
Chris@0 113 public function setHierarchy($hierarchy) {
Chris@0 114 $this->hierarchy = $hierarchy;
Chris@0 115 return $this;
Chris@0 116 }
Chris@0 117
Chris@0 118 /**
Chris@0 119 * {@inheritdoc}
Chris@0 120 */
Chris@0 121 public function id() {
Chris@0 122 return $this->vid;
Chris@0 123 }
Chris@0 124
Chris@0 125 /**
Chris@0 126 * {@inheritdoc}
Chris@0 127 */
Chris@0 128 public function getDescription() {
Chris@0 129 return $this->description;
Chris@0 130 }
Chris@0 131
Chris@0 132 /**
Chris@0 133 * {@inheritdoc}
Chris@0 134 */
Chris@0 135 public static function preDelete(EntityStorageInterface $storage, array $entities) {
Chris@0 136 parent::preDelete($storage, $entities);
Chris@0 137
Chris@0 138 // Only load terms without a parent, child terms will get deleted too.
Chris@0 139 entity_delete_multiple('taxonomy_term', $storage->getToplevelTids(array_keys($entities)));
Chris@0 140 }
Chris@0 141
Chris@0 142 /**
Chris@0 143 * {@inheritdoc}
Chris@0 144 */
Chris@0 145 public static function postDelete(EntityStorageInterface $storage, array $entities) {
Chris@0 146 parent::postDelete($storage, $entities);
Chris@0 147
Chris@0 148 // Reset caches.
Chris@0 149 $storage->resetCache(array_keys($entities));
Chris@0 150
Chris@0 151 if (reset($entities)->isSyncing()) {
Chris@0 152 return;
Chris@0 153 }
Chris@0 154
Chris@0 155 $vocabularies = [];
Chris@0 156 foreach ($entities as $vocabulary) {
Chris@0 157 $vocabularies[$vocabulary->id()] = $vocabulary->id();
Chris@0 158 }
Chris@0 159 // Load all Taxonomy module fields and delete those which use only this
Chris@0 160 // vocabulary.
Chris@0 161 $field_storages = entity_load_multiple_by_properties('field_storage_config', ['module' => 'taxonomy']);
Chris@0 162 foreach ($field_storages as $field_storage) {
Chris@0 163 $modified_storage = FALSE;
Chris@0 164 // Term reference fields may reference terms from more than one
Chris@0 165 // vocabulary.
Chris@0 166 foreach ($field_storage->getSetting('allowed_values') as $key => $allowed_value) {
Chris@0 167 if (isset($vocabularies[$allowed_value['vocabulary']])) {
Chris@0 168 $allowed_values = $field_storage->getSetting('allowed_values');
Chris@0 169 unset($allowed_values[$key]);
Chris@0 170 $field_storage->setSetting('allowed_values', $allowed_values);
Chris@0 171 $modified_storage = TRUE;
Chris@0 172 }
Chris@0 173 }
Chris@0 174 if ($modified_storage) {
Chris@0 175 $allowed_values = $field_storage->getSetting('allowed_values');
Chris@0 176 if (empty($allowed_values)) {
Chris@0 177 $field_storage->delete();
Chris@0 178 }
Chris@0 179 else {
Chris@0 180 // Update the field definition with the new allowed values.
Chris@0 181 $field_storage->save();
Chris@0 182 }
Chris@0 183 }
Chris@0 184 }
Chris@0 185 }
Chris@0 186
Chris@0 187 }