Mercurial > hg > isophonics-drupal-site
diff core/modules/taxonomy/src/Entity/Term.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 1fec387a4317 |
children | af1871eacc83 |
line wrap: on
line diff
--- a/core/modules/taxonomy/src/Entity/Term.php Tue Jul 10 15:07:59 2018 +0100 +++ b/core/modules/taxonomy/src/Entity/Term.php Thu Feb 28 13:21:36 2019 +0000 @@ -4,10 +4,12 @@ use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Entity\EntityChangedTrait; +use Drupal\Core\Entity\EntityPublishedTrait; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\taxonomy\TermInterface; +use Drupal\user\StatusItem; /** * Defines the taxonomy term entity. @@ -15,6 +17,13 @@ * @ContentEntityType( * id = "taxonomy_term", * label = @Translation("Taxonomy term"), + * label_collection = @Translation("Taxonomy terms"), + * label_singular = @Translation("taxonomy term"), + * label_plural = @Translation("taxonomy terms"), + * label_count = @PluralTranslation( + * singular = "@count taxonomy term", + * plural = "@count taxonomy terms", + * ), * bundle_label = @Translation("Vocabulary"), * handlers = { * "storage" = "Drupal\taxonomy\TermStorage", @@ -38,7 +47,8 @@ * "bundle" = "vid", * "label" = "name", * "langcode" = "langcode", - * "uuid" = "uuid" + * "uuid" = "uuid", + * "published" = "status", * }, * bundle_entity_type = "taxonomy_vocabulary", * field_ui_base_route = "entity.taxonomy_vocabulary.overview_form", @@ -55,6 +65,7 @@ class Term extends ContentEntityBase implements TermInterface { use EntityChangedTrait; + use EntityPublishedTrait; /** * {@inheritdoc} @@ -64,22 +75,28 @@ // See if any of the term's children are about to be become orphans. $orphans = []; - foreach (array_keys($entities) as $tid) { - if ($children = $storage->loadChildren($tid)) { + /** @var \Drupal\taxonomy\TermInterface $term */ + foreach ($entities as $tid => $term) { + if ($children = $storage->getChildren($term)) { + /** @var \Drupal\taxonomy\TermInterface $child */ foreach ($children as $child) { + $parent = $child->get('parent'); + // Update child parents item list. + $parent->filter(function ($item) use ($tid) { + return $item->target_id != $tid; + }); + // If the term has multiple parents, we don't delete it. - $parents = $storage->loadParents($child->id()); - if (empty($parents)) { + if ($parent->count()) { + $child->save(); + } + else { $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); } @@ -88,14 +105,11 @@ /** * {@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); + public function preSave(EntityStorageInterface $storage) { + parent::preSave($storage); + // Terms with no parents are mandatory children of <root>. + if (!$this->get('parent')->count()) { + $this->parent->target_id = 0; } } @@ -106,6 +120,12 @@ /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */ $fields = parent::baseFieldDefinitions($entity_type); + // Add the published field. + $fields += static::publishedBaseFieldDefinitions($entity_type); + // @todo Remove the usage of StatusItem in + // https://www.drupal.org/project/drupal/issues/2936864. + $fields['status']->getItemDefinition()->setClass(StatusItem::class); + $fields['tid']->setLabel(t('Term ID')) ->setDescription(t('The term ID.')); @@ -156,8 +176,7 @@ ->setLabel(t('Term Parents')) ->setDescription(t('The parents of this term.')) ->setSetting('target_type', 'taxonomy_term') - ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED) - ->setCustomStorage(TRUE); + ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED); $fields['changed'] = BaseFieldDefinition::create('changed') ->setLabel(t('Changed')) @@ -170,6 +189,16 @@ /** * {@inheritdoc} */ + public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { + // Only terms in the same bundle can be a parent. + $fields['parent'] = clone $base_field_definitions['parent']; + $fields['parent']->setSetting('handler_settings', ['target_bundles' => [$bundle => $bundle]]); + return $fields; + } + + /** + * {@inheritdoc} + */ public function getDescription() { return $this->get('description')->value; } @@ -235,17 +264,4 @@ 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; - } - }