Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 297938

Taxonomy terms are created with a parent 0

$
0
0

Problem/Motivation

When creating a new term, the parent field references a taxonomy term with ID 0 (which doesn't exist). It's also saved to the database like this.

\Drupal\taxonomy\TermStorage::create() apparently does this, for undocumented reasons.

/**
 * {@inheritdoc}
 *
 * @param array $values
 *   An array of values to set, keyed by property name. A value for the
 *   vocabulary ID ('vid') is required.
 */
public function create(array $values = []) {
  // Save new terms with no parents by default.
  if (empty($values['parent'])) {
    $values['parent'] = [0];
  }
  $entity = parent::create($values);
  return $entity;
}

Steps to reproduce

$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->create(['vid' => 'tags']);
$term->get('parent')->isEmpty(); // returns FALSE
$term->get('parent')->getValue(); // returns [['target_id' => 0]]

Proposed resolution

If there's no reason for doing this, let's remove this code.
If there is a reason for doing this, I think it's appropriate to document this (preferably in the code).

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet


Viewing all articles
Browse latest Browse all 297938

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>