function system_update_8402

Add the 'revision_translation_affected' field to all entity types.

File

core/modules/system/system.install, line 2400

Code

function system_update_8402() {
    $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
    // Clear the cached entity type definitions so we get the new
    // 'revision_translation_affected' entity key.
    \Drupal::entityTypeManager()->clearCachedDefinitions();
    // Get a list of revisionable and translatable entity types.
    
    /** @var \Drupal\Core\Entity\ContentEntityTypeInterface[] $definitions */
    $definitions = array_filter(\Drupal::entityTypeManager()->getDefinitions(), function (EntityTypeInterface $entity_type) use ($definition_update_manager) {
        if ($entity_type = $definition_update_manager->getEntityType($entity_type->id())) {
            return $entity_type->isRevisionable() && $entity_type->isTranslatable();
        }
        return FALSE;
    });
    foreach ($definitions as $entity_type_id => $entity_type) {
        $field_name = $entity_type->getKey('revision_translation_affected');
        // Install the 'revision_translation_affected' field if needed.
        if (!$definition_update_manager->getFieldStorageDefinition($field_name, $entity_type_id)) {
            $storage_definition = BaseFieldDefinition::create('boolean')->setLabel(t('Revision translation affected'))
                ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
                ->setReadOnly(TRUE)
                ->setRevisionable(TRUE)
                ->setTranslatable(TRUE)
                ->setInitialValue(TRUE);
            $definition_update_manager->installFieldStorageDefinition($field_name, $entity_type_id, $entity_type_id, $storage_definition);
        }
    }
}

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