Change record status: 
Project: 
Introduced in branch: 
8.6.x
Introduced in version: 
8.6.0
Description: 

Taxonomy terms are now publishable, which allows them to take part in access workflows like other first-class entity types in core, for example Content and Custom blocks.

This functionality is being provided by a new field on taxonomy terms called status. The field is installed automatically by taxonomy_update_8601(), unless the taxonomy term entity type already defines a field with that name in your project. In this case, you need to provide a different field name via the published entity key and add an update function to install the new definition:

/**
 * Implements hook_entity_type_alter().
 */
function my_module_entity_type_alter(array &$entity_types) {
  /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
  $entity_type = $entity_types['taxonomy_term'];
  $entity_keys = $entity_type->getKeys();
  $entity_keys['published'] = 'status_core';
  $entity_type->set('entity_keys', $entity_keys);
}

/**
 * Add the "status_core" field to the "taxonomy_term" entity type.
 */
function my_module_update_8601() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = $definition_update_manager->getEntityType('taxonomy_term');

  // Add the 'published' entity key to the taxonomy_term entity type.
  $entity_keys = $entity_type->getKeys();
  $entity_keys['published'] = 'status_core';
  $entity_type->set('entity_keys', $entity_keys);

  $definition_update_manager->updateEntityType($entity_type);

  // Add the status field.
  $status = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Publishing status'))
    ->setDescription(t('A boolean indicating the published state.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue(TRUE);

  $has_content_translation_status_field = \Drupal::moduleHandler()->moduleExists('content_translation') && $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'taxonomy_term');
  if ($has_content_translation_status_field) {
    $status->setInitialValueFromField('content_translation_status', TRUE);
  }
  else {
    $status->setInitialValue(TRUE);
  }
  $definition_update_manager->installFieldStorageDefinition('status_core', 'taxonomy_term', 'taxonomy_term', $status);

  // Uninstall the 'content_translation_status' field if needed.
  if ($has_content_translation_status_field) {
    $content_translation_status = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'taxonomy_term');
    $definition_update_manager->uninstallFieldStorageDefinition($content_translation_status);
  }

  return t('The publishing status field has been added to taxonomy terms.');
}
Impacts: 
Site builders, administrators, editors
Module developers
Themers

Comments

ohorbatiuk’s picture

Will be cool to add the possibility to view own unpublished terms via permission.

amateescu’s picture

I think that can happen easily after https://www.drupal.org/project/drupal/issues/2809177 gets in.