annotate core/modules/taxonomy/src/Entity/Vocabulary.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 1fec387a4317
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 * handlers = {
Chris@0 16 * "storage" = "Drupal\taxonomy\VocabularyStorage",
Chris@0 17 * "list_builder" = "Drupal\taxonomy\VocabularyListBuilder",
Chris@0 18 * "form" = {
Chris@0 19 * "default" = "Drupal\taxonomy\VocabularyForm",
Chris@0 20 * "reset" = "Drupal\taxonomy\Form\VocabularyResetForm",
Chris@0 21 * "delete" = "Drupal\taxonomy\Form\VocabularyDeleteForm"
Chris@0 22 * }
Chris@0 23 * },
Chris@0 24 * admin_permission = "administer taxonomy",
Chris@0 25 * config_prefix = "vocabulary",
Chris@0 26 * bundle_of = "taxonomy_term",
Chris@0 27 * entity_keys = {
Chris@0 28 * "id" = "vid",
Chris@0 29 * "label" = "name",
Chris@0 30 * "weight" = "weight"
Chris@0 31 * },
Chris@0 32 * links = {
Chris@0 33 * "add-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/add",
Chris@0 34 * "delete-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/delete",
Chris@0 35 * "reset-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/reset",
Chris@0 36 * "overview-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/overview",
Chris@0 37 * "edit-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}",
Chris@0 38 * "collection" = "/admin/structure/taxonomy",
Chris@0 39 * },
Chris@0 40 * config_export = {
Chris@0 41 * "name",
Chris@0 42 * "vid",
Chris@0 43 * "description",
Chris@0 44 * "hierarchy",
Chris@0 45 * "weight",
Chris@0 46 * }
Chris@0 47 * )
Chris@0 48 */
Chris@0 49 class Vocabulary extends ConfigEntityBundleBase implements VocabularyInterface {
Chris@0 50
Chris@0 51 /**
Chris@0 52 * The taxonomy vocabulary ID.
Chris@0 53 *
Chris@0 54 * @var string
Chris@0 55 */
Chris@0 56 protected $vid;
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Name of the vocabulary.
Chris@0 60 *
Chris@0 61 * @var string
Chris@0 62 */
Chris@0 63 protected $name;
Chris@0 64
Chris@0 65 /**
Chris@0 66 * Description of the vocabulary.
Chris@0 67 *
Chris@0 68 * @var string
Chris@0 69 */
Chris@0 70 protected $description;
Chris@0 71
Chris@0 72 /**
Chris@0 73 * The type of hierarchy allowed within the vocabulary.
Chris@0 74 *
Chris@0 75 * Possible values:
Chris@0 76 * - VocabularyInterface::HIERARCHY_DISABLED: No parents.
Chris@0 77 * - VocabularyInterface::HIERARCHY_SINGLE: Single parent.
Chris@0 78 * - VocabularyInterface::HIERARCHY_MULTIPL: Multiple parents.
Chris@0 79 *
Chris@0 80 * @var int
Chris@0 81 */
Chris@0 82 protected $hierarchy = VocabularyInterface::HIERARCHY_DISABLED;
Chris@0 83
Chris@0 84 /**
Chris@0 85 * The weight of this vocabulary in relation to other vocabularies.
Chris@0 86 *
Chris@0 87 * @var int
Chris@0 88 */
Chris@0 89 protected $weight = 0;
Chris@0 90
Chris@0 91 /**
Chris@0 92 * {@inheritdoc}
Chris@0 93 */
Chris@0 94 public function getHierarchy() {
Chris@0 95 return $this->hierarchy;
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * {@inheritdoc}
Chris@0 100 */
Chris@0 101 public function setHierarchy($hierarchy) {
Chris@0 102 $this->hierarchy = $hierarchy;
Chris@0 103 return $this;
Chris@0 104 }
Chris@0 105
Chris@0 106 /**
Chris@0 107 * {@inheritdoc}
Chris@0 108 */
Chris@0 109 public function id() {
Chris@0 110 return $this->vid;
Chris@0 111 }
Chris@0 112
Chris@0 113 /**
Chris@0 114 * {@inheritdoc}
Chris@0 115 */
Chris@0 116 public function getDescription() {
Chris@0 117 return $this->description;
Chris@0 118 }
Chris@0 119
Chris@0 120 /**
Chris@0 121 * {@inheritdoc}
Chris@0 122 */
Chris@0 123 public static function preDelete(EntityStorageInterface $storage, array $entities) {
Chris@0 124 parent::preDelete($storage, $entities);
Chris@0 125
Chris@0 126 // Only load terms without a parent, child terms will get deleted too.
Chris@0 127 entity_delete_multiple('taxonomy_term', $storage->getToplevelTids(array_keys($entities)));
Chris@0 128 }
Chris@0 129
Chris@0 130 /**
Chris@0 131 * {@inheritdoc}
Chris@0 132 */
Chris@0 133 public static function postDelete(EntityStorageInterface $storage, array $entities) {
Chris@0 134 parent::postDelete($storage, $entities);
Chris@0 135
Chris@0 136 // Reset caches.
Chris@0 137 $storage->resetCache(array_keys($entities));
Chris@0 138
Chris@0 139 if (reset($entities)->isSyncing()) {
Chris@0 140 return;
Chris@0 141 }
Chris@0 142
Chris@0 143 $vocabularies = [];
Chris@0 144 foreach ($entities as $vocabulary) {
Chris@0 145 $vocabularies[$vocabulary->id()] = $vocabulary->id();
Chris@0 146 }
Chris@0 147 // Load all Taxonomy module fields and delete those which use only this
Chris@0 148 // vocabulary.
Chris@0 149 $field_storages = entity_load_multiple_by_properties('field_storage_config', ['module' => 'taxonomy']);
Chris@0 150 foreach ($field_storages as $field_storage) {
Chris@0 151 $modified_storage = FALSE;
Chris@0 152 // Term reference fields may reference terms from more than one
Chris@0 153 // vocabulary.
Chris@0 154 foreach ($field_storage->getSetting('allowed_values') as $key => $allowed_value) {
Chris@0 155 if (isset($vocabularies[$allowed_value['vocabulary']])) {
Chris@0 156 $allowed_values = $field_storage->getSetting('allowed_values');
Chris@0 157 unset($allowed_values[$key]);
Chris@0 158 $field_storage->setSetting('allowed_values', $allowed_values);
Chris@0 159 $modified_storage = TRUE;
Chris@0 160 }
Chris@0 161 }
Chris@0 162 if ($modified_storage) {
Chris@0 163 $allowed_values = $field_storage->getSetting('allowed_values');
Chris@0 164 if (empty($allowed_values)) {
Chris@0 165 $field_storage->delete();
Chris@0 166 }
Chris@0 167 else {
Chris@0 168 // Update the field definition with the new allowed values.
Chris@0 169 $field_storage->save();
Chris@0 170 }
Chris@0 171 }
Chris@0 172 }
Chris@0 173 }
Chris@0 174
Chris@0 175 }