summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/taxonomy/src/Entity/Vocabulary.php
blob: e254e8ad0b45ea7899f2114d0dfc4ba107cdbf1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php

namespace Drupal\taxonomy\Entity;

use Drupal\Core\Entity\Attribute\ConfigEntityType;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\taxonomy\Entity\Routing\VocabularyRouteProvider;
use Drupal\taxonomy\Form\OverviewTerms;
use Drupal\taxonomy\Form\VocabularyDeleteForm;
use Drupal\taxonomy\Form\VocabularyResetForm;
use Drupal\taxonomy\VocabularyAccessControlHandler;
use Drupal\taxonomy\VocabularyForm;
use Drupal\taxonomy\VocabularyInterface;
use Drupal\taxonomy\VocabularyListBuilder;
use Drupal\taxonomy\VocabularyStorage;
use Drupal\user\Entity\EntityPermissionsRouteProvider;

/**
 * Defines the taxonomy vocabulary entity.
 */
#[ConfigEntityType(
  id: 'taxonomy_vocabulary',
  label: new TranslatableMarkup('Taxonomy vocabulary'),
  label_collection: new TranslatableMarkup('Taxonomy'),
  label_singular: new TranslatableMarkup('vocabulary'),
  label_plural: new TranslatableMarkup('vocabularies'),
  config_prefix: 'vocabulary',
  entity_keys: [
    'id' => 'vid',
    'label' => 'name',
    'weight' => 'weight',
  ],
  handlers: [
    'storage' => VocabularyStorage::class,
    'list_builder' => VocabularyListBuilder::class,
    'access' => VocabularyAccessControlHandler::class,
    'form' => [
      'default' => VocabularyForm::class,
      'reset' => VocabularyResetForm::class,
      'delete' => VocabularyDeleteForm::class,
      'overview' => OverviewTerms::class,
    ],
    'route_provider' => [
      'html' => VocabularyRouteProvider::class,
      'permissions' => EntityPermissionsRouteProvider::class,
    ],
  ],
  links: [
    'add-form' => '/admin/structure/taxonomy/add',
    'delete-form' => '/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/delete',
    'reset-form' => '/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/reset',
    'overview-form' => '/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/overview',
    'edit-form' => '/admin/structure/taxonomy/manage/{taxonomy_vocabulary}',
    'entity-permissions-form' => '/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/overview/permissions',
    'collection' => '/admin/structure/taxonomy',
  ],
  admin_permission: 'administer taxonomy',
  collection_permission: 'access taxonomy overview',
  bundle_of: 'taxonomy_term',
  label_count: [
    'singular' => '@count vocabulary',
    'plural' => '@count vocabularies',
  ],
  config_export: [
    'name',
    'vid',
    'description',
    'weight',
    'new_revision',
  ],
)]
class Vocabulary extends ConfigEntityBundleBase implements VocabularyInterface {

  /**
   * The taxonomy vocabulary ID.
   *
   * @var string
   */
  protected $vid;

  /**
   * Name of the vocabulary.
   *
   * @var string
   */
  protected $name;

  /**
   * Description of the vocabulary.
   *
   * @var string|null
   */
  protected $description = NULL;

  /**
   * The weight of this vocabulary in relation to other vocabularies.
   *
   * @var int
   */
  protected $weight = 0;

  /**
   * {@inheritdoc}
   */
  public function id() {
    return $this->vid;
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->description ?? '';
  }

  /**
   * The default revision setting for a vocabulary.
   *
   * @var bool
   */
  protected $new_revision = FALSE;

  /**
   * {@inheritdoc}
   */
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
    parent::preDelete($storage, $entities);

    // Only load terms without a parent, child terms will get deleted too.
    $term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
    $terms = $term_storage->loadMultiple($storage->getToplevelTids(array_keys($entities)));
    $term_storage->delete($terms);
  }

  /**
   * {@inheritdoc}
   */
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
    parent::postDelete($storage, $entities);

    // Reset caches.
    $storage->resetCache(array_keys($entities));

    if (reset($entities)->isSyncing()) {
      return;
    }

    $vocabularies = [];
    foreach ($entities as $vocabulary) {
      $vocabularies[$vocabulary->id()] = $vocabulary->id();
    }
    // Load all Taxonomy module fields and delete those which use only this
    // vocabulary.
    $field_storages = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties(['module' => 'taxonomy']);
    foreach ($field_storages as $field_storage) {
      $modified_storage = FALSE;
      // Term reference fields may reference terms from more than one
      // vocabulary.
      foreach ($field_storage->getSetting('allowed_values') as $key => $allowed_value) {
        if (isset($vocabularies[$allowed_value['vocabulary']])) {
          $allowed_values = $field_storage->getSetting('allowed_values');
          unset($allowed_values[$key]);
          $field_storage->setSetting('allowed_values', $allowed_values);
          $modified_storage = TRUE;
        }
      }
      if ($modified_storage) {
        $allowed_values = $field_storage->getSetting('allowed_values');
        if (empty($allowed_values)) {
          $field_storage->delete();
        }
        else {
          // Update the field definition with the new allowed values.
          $field_storage->save();
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setNewRevision($new_revision) {
    $this->new_revision = $new_revision;
  }

  /**
   * {@inheritdoc}
   */
  public function shouldCreateNewRevision() {
    return $this->new_revision;
  }

}