block_content.install 5.38 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

/**
 * @file
 * Install, update and uninstall functions for the block_content module.
 */

use Drupal\Core\Field\BaseFieldDefinition;
9 10
use Drupal\Core\StringTranslation\TranslatableMarkup;

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/**
 * Add 'revision_translation_affected' field to 'block_content' entities.
 */
function block_content_update_8001() {
  // Install the definition that this field had in
  // \Drupal\block_content\Entity\BlockContent::baseFieldDefinitions()
  // at the time that this update function was written. If/when code is
  // deployed that changes that definition, the corresponding module must
  // implement an update function that invokes
  // \Drupal::entityDefinitionUpdateManager()->updateFieldStorageDefinition()
  // with the new definition.
  $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);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('revision_translation_affected', 'block_content', 'block_content', $storage_definition);
}
32 33 34 35 36 37

/**
 * Generalizes the d6_block_content_type and d6_block_content_body_field
 * migrations.
 */
function block_content_update_8002() {
38 39 40 41
  // Removed in issue #2569605. The Migrate and Migrate Drupal modules are
  // marked experimental and do not need to support the update path until they
  // are stable.
  // @see https://www.drupal.org/node/2569469
42
}
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

/**
 * Add 'revision_created' and 'revision_user' fields to 'block_content' entities.
 */
function block_content_update_8003() {
  $revision_created = BaseFieldDefinition::create('created')
    ->setLabel(t('Revision create time'))
    ->setDescription(t('The time that the current revision was created.'))
    ->setRevisionable(TRUE);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('revision_created', 'block_content', 'block_content', $revision_created);

  $revision_user = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Revision user'))
    ->setDescription(t('The user ID of the author of the current revision.'))
    ->setSetting('target_type', 'user')
    ->setRevisionable(TRUE);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('revision_user', 'block_content', 'block_content', $revision_user);
}
65 66 67 68 69 70 71 72 73

/**
 * Fix the block_content entity type to specify its revision data table.
 */
function block_content_update_8300() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = $definition_update_manager->getEntityType('block_content');
  $entity_type->set('revision_data_table', 'block_content_field_revision');
  $definition_update_manager->updateEntityType($entity_type);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

/**
 * Add a publishing status field for block_content entities.
 */
function block_content_update_8400() {
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  // Add the published entity key to the block_content entity type.
  $entity_type = $definition_update_manager->getEntityType('block_content');
  $entity_keys = $entity_type->getKeys();
  $entity_keys['published'] = 'status';
  $entity_type->set('entity_keys', $entity_keys);
  $definition_update_manager->updateEntityType($entity_type);

  // Add the publishing status field to the block_content entity type.
  $status = BaseFieldDefinition::create('boolean')
    ->setLabel(new TranslatableMarkup('Publishing status'))
    ->setDescription(new TranslatableMarkup('A boolean indicating the published state.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue(TRUE);

97
  $has_content_translation_status_field = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'block_content');
98
  if ($has_content_translation_status_field) {
99
    $status->setInitialValueFromField('content_translation_status', TRUE);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  }
  else {
    $status->setInitialValue(TRUE);
  }
  $definition_update_manager->installFieldStorageDefinition('status', 'block_content', 'block_content', $status);

  // Uninstall the 'content_translation_status' field if needed.
  $database = \Drupal::database();
  if ($has_content_translation_status_field) {
    // First we have to remove the field data.
    $database->update($entity_type->getDataTable())
      ->fields(['content_translation_status' => NULL])
      ->execute();

    // A site may have disabled revisionability for this entity type.
    if ($entity_type->isRevisionable()) {
      $database->update($entity_type->getRevisionDataTable())
        ->fields(['content_translation_status' => NULL])
        ->execute();
    }
120

121 122 123
    $content_translation_status = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'block_content');
    $definition_update_manager->uninstallFieldStorageDefinition($content_translation_status);
  }
124
}
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

/**
 * Add 'reusable' field to 'block_content' entities.
 */
function block_content_update_8600() {
  $reusable = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Reusable'))
    ->setDescription(t('A boolean indicating whether this block is reusable.'))
    ->setTranslatable(FALSE)
    ->setRevisionable(FALSE)
    ->setDefaultValue(TRUE)
    ->setInitialValue(TRUE);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('reusable', 'block_content', 'block_content', $reusable);
}