loadChildren($tid)) { foreach ($children as $child) { // If the term has multiple parents, we don't delete it. $parents = $storage->loadParents($child->id()); if (empty($parents)) { $orphans[] = $child; } } } } // Delete term hierarchy information after looking up orphans but before // deleting them so that their children/parent information is consistent. $storage->deleteTermHierarchy(array_keys($entities)); if (!empty($orphans)) { $storage->delete($orphans); } } /** * {@inheritdoc} */ public function postSave(EntityStorageInterface $storage, $update = TRUE) { parent::postSave($storage, $update); // Only change the parents if a value is set, keep the existing values if // not. if (isset($this->parent->target_id)) { $storage->deleteTermHierarchy([$this->id()]); $storage->updateTermHierarchy($this); } } /** * {@inheritdoc} */ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */ $fields = parent::baseFieldDefinitions($entity_type); $fields['tid']->setLabel(t('Term ID')) ->setDescription(t('The term ID.')); $fields['uuid']->setDescription(t('The term UUID.')); $fields['vid']->setLabel(t('Vocabulary')) ->setDescription(t('The vocabulary to which the term is assigned.')); $fields['langcode']->setDescription(t('The term language code.')); $fields['name'] = BaseFieldDefinition::create('string') ->setLabel(t('Name')) ->setTranslatable(TRUE) ->setRequired(TRUE) ->setSetting('max_length', 255) ->setDisplayOptions('view', [ 'label' => 'hidden', 'type' => 'string', 'weight' => -5, ]) ->setDisplayOptions('form', [ 'type' => 'string_textfield', 'weight' => -5, ]) ->setDisplayConfigurable('form', TRUE); $fields['description'] = BaseFieldDefinition::create('text_long') ->setLabel(t('Description')) ->setTranslatable(TRUE) ->setDisplayOptions('view', [ 'label' => 'hidden', 'type' => 'text_default', 'weight' => 0, ]) ->setDisplayConfigurable('view', TRUE) ->setDisplayOptions('form', [ 'type' => 'text_textfield', 'weight' => 0, ]) ->setDisplayConfigurable('form', TRUE); $fields['weight'] = BaseFieldDefinition::create('integer') ->setLabel(t('Weight')) ->setDescription(t('The weight of this term in relation to other terms.')) ->setDefaultValue(0); $fields['parent'] = BaseFieldDefinition::create('entity_reference') ->setLabel(t('Term Parents')) ->setDescription(t('The parents of this term.')) ->setSetting('target_type', 'taxonomy_term') ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED) ->setCustomStorage(TRUE); $fields['changed'] = BaseFieldDefinition::create('changed') ->setLabel(t('Changed')) ->setDescription(t('The time that the term was last edited.')) ->setTranslatable(TRUE); return $fields; } /** * {@inheritdoc} */ public function getDescription() { return $this->get('description')->value; } /** * {@inheritdoc} */ public function setDescription($description) { $this->set('description', $description); return $this; } /** * {@inheritdoc} */ public function getFormat() { return $this->get('description')->format; } /** * {@inheritdoc} */ public function setFormat($format) { $this->get('description')->format = $format; return $this; } /** * {@inheritdoc} */ public function getName() { return $this->label(); } /** * {@inheritdoc} */ public function setName($name) { $this->set('name', $name); return $this; } /** * {@inheritdoc} */ public function getWeight() { return $this->get('weight')->value; } /** * {@inheritdoc} */ public function setWeight($weight) { $this->set('weight', $weight); return $this; } /** * {@inheritdoc} */ public function getVocabularyId() { @trigger_error('The ' . __METHOD__ . ' method is deprecated since version 8.4.0 and will be removed before 9.0.0. Use ' . __CLASS__ . '::bundle() instead to get the vocabulary ID.', E_USER_DEPRECATED); return $this->bundle(); } /** * {@inheritdoc} */ protected function getFieldsToSkipFromTranslationChangesCheck() { // @todo the current implementation of the parent field makes it impossible // for ::hasTranslationChanges() to correctly check the field for changes, // so it is currently skipped from the comparision and has to be fixed by // https://www.drupal.org/node/2843060. $fields = parent::getFieldsToSkipFromTranslationChangesCheck(); $fields[] = 'parent'; return $fields; } }