function taxonomy_post_update_make_taxonomy_term_revisionable

Update taxonomy terms to be revisionable.

1 string reference to 'taxonomy_post_update_make_taxonomy_term_revisionable'
taxonomy_requirements in core/modules/taxonomy/taxonomy.install
Implements hook_requirements().

File

core/modules/taxonomy/taxonomy.post_update.php, line 154

Code

function taxonomy_post_update_make_taxonomy_term_revisionable(&$sandbox) {
    $finished = _taxonomy_post_update_make_taxonomy_term_revisionable__fix_default_langcode($sandbox);
    if (!$finished) {
        $sandbox['#finished'] = 0;
        return NULL;
    }
    $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
    
    /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $last_installed_schema_repository */
    $last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
    $entity_type = $definition_update_manager->getEntityType('taxonomy_term');
    $field_storage_definitions = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions('taxonomy_term');
    // Update the entity type definition.
    $entity_keys = $entity_type->getKeys();
    $entity_keys['revision'] = 'revision_id';
    $entity_keys['revision_translation_affected'] = 'revision_translation_affected';
    $entity_type->set('entity_keys', $entity_keys);
    $entity_type->set('revision_table', 'taxonomy_term_revision');
    $entity_type->set('revision_data_table', 'taxonomy_term_field_revision');
    $revision_metadata_keys = [
        'revision_default' => 'revision_default',
        'revision_user' => 'revision_user',
        'revision_created' => 'revision_created',
        'revision_log_message' => 'revision_log_message',
    ];
    $entity_type->set('revision_metadata_keys', $revision_metadata_keys);
    // Update the field storage definitions and add the new ones required by a
    // revisionable entity type.
    $field_storage_definitions['langcode']->setRevisionable(TRUE);
    $field_storage_definitions['name']->setRevisionable(TRUE);
    $field_storage_definitions['description']->setRevisionable(TRUE);
    $field_storage_definitions['changed']->setRevisionable(TRUE);
    $field_storage_definitions['revision_id'] = BaseFieldDefinition::create('integer')->setName('revision_id')
        ->setTargetEntityTypeId('taxonomy_term')
        ->setTargetBundle(NULL)
        ->setLabel(new TranslatableMarkup('Revision ID'))
        ->setReadOnly(TRUE)
        ->setSetting('unsigned', TRUE);
    $field_storage_definitions['revision_default'] = BaseFieldDefinition::create('boolean')->setName('revision_default')
        ->setTargetEntityTypeId('taxonomy_term')
        ->setTargetBundle(NULL)
        ->setLabel(new TranslatableMarkup('Default revision'))
        ->setDescription(new TranslatableMarkup('A flag indicating whether this was a default revision when it was saved.'))
        ->setStorageRequired(TRUE)
        ->setInternal(TRUE)
        ->setTranslatable(FALSE)
        ->setRevisionable(TRUE);
    $field_storage_definitions['revision_translation_affected'] = BaseFieldDefinition::create('boolean')->setName('revision_translation_affected')
        ->setTargetEntityTypeId('taxonomy_term')
        ->setTargetBundle(NULL)
        ->setLabel(new TranslatableMarkup('Revision translation affected'))
        ->setDescription(new TranslatableMarkup('Indicates if the last edit of a translation belongs to current revision.'))
        ->setReadOnly(TRUE)
        ->setRevisionable(TRUE)
        ->setTranslatable(TRUE);
    $field_storage_definitions['revision_created'] = BaseFieldDefinition::create('created')->setName('revision_created')
        ->setTargetEntityTypeId('taxonomy_term')
        ->setTargetBundle(NULL)
        ->setLabel(new TranslatableMarkup('Revision create time'))
        ->setDescription(new TranslatableMarkup('The time that the current revision was created.'))
        ->setRevisionable(TRUE);
    $field_storage_definitions['revision_user'] = BaseFieldDefinition::create('entity_reference')->setName('revision_user')
        ->setTargetEntityTypeId('taxonomy_term')
        ->setTargetBundle(NULL)
        ->setLabel(new TranslatableMarkup('Revision user'))
        ->setDescription(new TranslatableMarkup('The user ID of the author of the current revision.'))
        ->setSetting('target_type', 'user')
        ->setRevisionable(TRUE);
    $field_storage_definitions['revision_log_message'] = BaseFieldDefinition::create('string_long')->setName('revision_log_message')
        ->setTargetEntityTypeId('taxonomy_term')
        ->setTargetBundle(NULL)
        ->setLabel(new TranslatableMarkup('Revision log message'))
        ->setDescription(new TranslatableMarkup('Briefly describe the changes you have made.'))
        ->setRevisionable(TRUE)
        ->setDefaultValue('');
    $definition_update_manager->updateFieldableEntityType($entity_type, $field_storage_definitions, $sandbox);
    if (!empty($sandbox['data_fix']['default_langcode']['processed'])) {
        $count = $sandbox['data_fix']['default_langcode']['processed'];
        if (\Drupal::moduleHandler()->moduleExists('dblog')) {
            // @todo Simplify with https://www.drupal.org/node/2548095
            $base_url = str_replace('/update.php', '', \Drupal::request()->getBaseUrl());
            $args = [
                ':url' => Url::fromRoute('dblog.overview', [], [
                    'query' => [
                        'type' => [
                            'update',
                        ],
                        'severity' => [
                            RfcLogLevel::WARNING,
                        ],
                    ],
                ])->setOption('base_url', $base_url)
                    ->toString(TRUE)
                    ->getGeneratedUrl(),
            ];
            return new PluralTranslatableMarkup($count, 'Taxonomy terms have been converted to be revisionable. One term with data integrity issues was restored. More details have been <a href=":url">logged</a>.', 'Taxonomy terms have been converted to be revisionable. @count terms with data integrity issues were restored. More details have been <a href=":url">logged</a>.', $args);
        }
        else {
            return new PluralTranslatableMarkup($count, 'Taxonomy terms have been converted to be revisionable. One term with data integrity issues was restored. More details have been logged.', 'Taxonomy terms have been converted to be revisionable. @count terms with data integrity issues were restored. More details have been logged.');
        }
    }
    else {
        return t('Taxonomy terms have been converted to be revisionable.');
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.