You are here

function multiversion_entity_base_field_info in Multiversion 8

Same name and namespace in other branches
  1. 8.2 multiversion.module \multiversion_entity_base_field_info()

Implements hook_entity_base_field_info().

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type:

Return value

\Drupal\Core\Field\BaseFieldDefinition[]

File

./multiversion.module, line 131

Code

function multiversion_entity_base_field_info(EntityTypeInterface $entity_type) {

  /** @var \Drupal\multiversion\MultiversionManagerInterface $manager */
  $manager = \Drupal::service('multiversion.manager');
  if ($manager
    ->allowToAlter($entity_type)) {
    $fields = [];

    // Get the minor version only from the \Drupal::VERSION string.
    $minor_version = substr(\Drupal::VERSION, 0, 3);

    // @todo: Alter the entity label field to make it revisionable.
    // In some scenarios where's in a state of limbo where we've already
    // altered and enabled the entity type but we're given an old entity type
    // definition for this hook and we get an empty revision key. However,
    // these are always the entity types that Multiversion has enabled revisions
    // on, so we can assume the same name of the revision key.
    $revision_key = $entity_type
      ->getKey('revision') ?: 'revision_id';

    // This will essentially overwrite the revision field definition but also
    // ensure that entity types that we enabled revisions for get a revision
    // field definition of a type that we expect.
    $fields[$revision_key] = BaseFieldDefinition::create('integer')
      ->setLabel(t('Revision ID'))
      ->setDescription(t('The local revision ID of the entity.'))
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);

    // Add the revision_default field on 8.5 or higher.
    if (version_compare($minor_version, '8.5', '>=')) {
      $fields['revision_default'] = BaseFieldDefinition::create('boolean')
        ->setLabel(t('Default revision'))
        ->setDescription(t('A flag indicating whether this was a default revision when it was saved.'))
        ->setStorageRequired(TRUE)
        ->setTranslatable(FALSE)
        ->setRevisionable(TRUE)
        ->setInitialValue(TRUE);
    }

    // This field shouldn't really be revisionable since all revisions for an
    // entity will only ever exist in one and the same workspace. But we mark
    // this as revisionable to make the storage query more performance because
    // then we don't need to join the data table (which it isn't by default).
    if ($entity_type
      ->get('workspace') !== FALSE) {
      $fields['workspace'] = BaseFieldDefinition::create('workspace_reference')
        ->setLabel(t('Workspace reference'))
        ->setDescription(t('The workspace this entity belongs to.'))
        ->setSetting('target_type', 'workspace')
        ->setRevisionable(FALSE)
        ->setTranslatable(FALSE)
        ->setCardinality(1)
        ->setReadOnly(TRUE);
    }
    $fields['_deleted'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Deleted flag'))
      ->setDescription(t('Indicates if the entity is flagged as deleted or not.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(FALSE)
      ->setDefaultValue(FALSE)
      ->setCardinality(1);
    $fields['_rev'] = BaseFieldDefinition::create('revision_token')
      ->setLabel(t('Revision token'))
      ->setDescription(t('The token for this entity revision.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(FALSE)
      ->setCardinality(1)
      ->setReadOnly(TRUE);

    // Add the 'revision_translation_affected' field if needed. Limit this to
    // Drupal version 8.4 and higher.
    if (version_compare($minor_version, '8.4', '>=') && $entity_type
      ->isTranslatable()) {
      $fields[$entity_type
        ->getKey('revision_translation_affected')] = BaseFieldDefinition::create('boolean')
        ->setName($entity_type
        ->getKey('revision_translation_affected'))
        ->setTargetEntityTypeId($entity_type
        ->id())
        ->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);
    }
    return $fields;
  }
}