Vocabulary::postDelete

public static Vocabulary::postDelete(EntityStorageInterface $storage, array $entities)

Acts on deleted entities before the delete hook is invoked.

Used after the entities are deleted but before invoking the delete hook.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

\Drupal\Core\Entity\EntityInterface[] $entities: An array of entities.

Overrides ConfigEntityBundleBase::postDelete

File

core/modules/taxonomy/src/Entity/Vocabulary.php, line 133

Class

Vocabulary
Defines the taxonomy vocabulary entity.

Namespace

Drupal\taxonomy\Entity

Code

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
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 = array();
  foreach ($entities as $vocabulary) {
    $vocabularies[$vocabulary->id()] = $vocabulary->id();
  }
  // Load all Taxonomy module fields and delete those which use only this
  // vocabulary.
  $field_storages = entity_load_multiple_by_properties('field_storage_config', array('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();
      }
    }
  }
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.