Vocabulary.php

Same filename in this branch
  1. 11.x core/modules/taxonomy/src/Plugin/migrate/source/d6/Vocabulary.php
  2. 11.x core/modules/taxonomy/src/Plugin/migrate/source/d7/Vocabulary.php
Same filename in other branches
  1. 9 core/modules/taxonomy/src/Entity/Vocabulary.php
  2. 9 core/modules/taxonomy/src/Plugin/migrate/source/d6/Vocabulary.php
  3. 9 core/modules/taxonomy/src/Plugin/migrate/source/d7/Vocabulary.php
  4. 8.9.x core/modules/taxonomy/src/Entity/Vocabulary.php
  5. 8.9.x core/modules/taxonomy/src/Plugin/migrate/source/d6/Vocabulary.php
  6. 8.9.x core/modules/taxonomy/src/Plugin/migrate/source/d7/Vocabulary.php
  7. 10 core/modules/taxonomy/src/Entity/Vocabulary.php
  8. 10 core/modules/taxonomy/src/Plugin/migrate/source/d6/Vocabulary.php
  9. 10 core/modules/taxonomy/src/Plugin/migrate/source/d7/Vocabulary.php

Namespace

Drupal\taxonomy\Entity

File

core/modules/taxonomy/src/Entity/Vocabulary.php

View source
<?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.
 */
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;
    }

}

Classes

Title Deprecated Summary
Vocabulary Defines the taxonomy vocabulary entity.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.