Created
November 27, 2019 00:00
-
-
Save Mykola-Veryha/95ff8e59a0c6992b3cdba34a0c32fa66 to your computer and use it in GitHub Desktop.
How to remove a deleted term entity from Entity Reference field after deleting the term entity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\node\NodeInterface; | |
use Drupal\taxonomy\TermInterface; | |
/** | |
* Implements hook_ENTITY_TYPE_delete() for taxonomy_term entities. | |
* | |
* @throws \Drupal\Core\Entity\EntityStorageException | |
*/ | |
function dh_extranet_taxonomy_term_delete(TermInterface $term) { | |
$node_ids = _dh_extranet_load_related_nids_by_term($term); | |
$nodes = Node::loadMultiple($node_ids); | |
$entity_reference_class = 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem'; | |
foreach ($nodes as $node) { | |
/** @var \Drupal\Core\Field\BaseFieldDefinition $field */ | |
foreach ($node->getFieldDefinitions() as $field) { | |
$field_name = $field->getName(); | |
/** @var \Drupal\field\Entity\FieldConfig $class */ | |
$class = $field->getItemDefinition()->getClass(); | |
$is_entity_reference_class = ($class === $entity_reference_class) || is_subclass_of($class, $entity_reference_class); | |
if ($is_entity_reference_class && $field->getSetting('target_type') == 'taxonomy_term') { | |
_dh_extranet_remove_term_reference_from_node_field($node, $field_name, $term); | |
} | |
} | |
} | |
} | |
/** | |
* Remove term reference from a node field. | |
* | |
* @param \Drupal\node\NodeInterface $node | |
* The node entity. | |
* @param string $field_name | |
* The field name which contains the term. | |
* @param \Drupal\taxonomy\TermInterface $term | |
* The taxonomy term entity. | |
* | |
* @throws \Drupal\Core\Entity\EntityStorageException | |
*/ | |
function _dh_extranet_remove_term_reference_from_node_field( | |
NodeInterface $node, | |
string $field_name, | |
TermInterface $term | |
) { | |
/** @var \Drupal\Core\Language\LanguageInterface $language */ | |
foreach ($node->getTranslationLanguages() as $language) { | |
/** @var \Drupal\Core\Entity\ContentEntityInterface $translation */ | |
$translation = $node->getTranslation($language->getId()); | |
/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item */ | |
foreach ($translation->$field_name as $key => $item) { | |
if (!$item->isEmpty() && $item->target_id == $term->id()) { | |
$translation->$field_name->removeItem($key); | |
$translation->save(); | |
} | |
} | |
} | |
} | |
/** | |
* Load nids that are using the term in any entity reference field. | |
* | |
* @param \Drupal\taxonomy\TermInterface $term | |
* A taxonomy term entity. | |
* | |
* @return array | |
* An array with the node ids. | |
*/ | |
function _dh_extranet_load_related_nids_by_term(TermInterface $term) { | |
$query = Drupal::database()->select('taxonomy_index', 'ti'); | |
$query->fields('ti', array('nid')); | |
$query->condition('ti.tid', $term->id()); | |
$query->distinct(TRUE); | |
$node_ids = $query->execute()->fetchCol(); | |
if (empty($node_ids)) { | |
return []; | |
} | |
return $node_ids; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment