block_content.install 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the block_content module.
  5. */
  6. use Drupal\Core\Field\BaseFieldDefinition;
  7. use Drupal\Core\StringTranslation\TranslatableMarkup;
  8. /**
  9. * Add 'revision_translation_affected' field to 'block_content' entities.
  10. */
  11. function block_content_update_8001() {
  12. // Install the definition that this field had in
  13. // \Drupal\block_content\Entity\BlockContent::baseFieldDefinitions()
  14. // at the time that this update function was written. If/when code is
  15. // deployed that changes that definition, the corresponding module must
  16. // implement an update function that invokes
  17. // \Drupal::entityDefinitionUpdateManager()->updateFieldStorageDefinition()
  18. // with the new definition.
  19. $storage_definition = BaseFieldDefinition::create('boolean')
  20. ->setLabel(t('Revision translation affected'))
  21. ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
  22. ->setReadOnly(TRUE)
  23. ->setRevisionable(TRUE)
  24. ->setTranslatable(TRUE);
  25. \Drupal::entityDefinitionUpdateManager()
  26. ->installFieldStorageDefinition('revision_translation_affected', 'block_content', 'block_content', $storage_definition);
  27. }
  28. /**
  29. * Generalizes the d6_block_content_type and d6_block_content_body_field
  30. * migrations.
  31. */
  32. function block_content_update_8002() {
  33. // Removed in issue #2569605. The Migrate and Migrate Drupal modules are
  34. // marked experimental and do not need to support the update path until they
  35. // are stable.
  36. // @see https://www.drupal.org/node/2569469
  37. }
  38. /**
  39. * Add 'revision_created' and 'revision_user' fields to 'block_content' entities.
  40. */
  41. function block_content_update_8003() {
  42. $revision_created = BaseFieldDefinition::create('created')
  43. ->setLabel(t('Revision create time'))
  44. ->setDescription(t('The time that the current revision was created.'))
  45. ->setRevisionable(TRUE);
  46. \Drupal::entityDefinitionUpdateManager()
  47. ->installFieldStorageDefinition('revision_created', 'block_content', 'block_content', $revision_created);
  48. $revision_user = BaseFieldDefinition::create('entity_reference')
  49. ->setLabel(t('Revision user'))
  50. ->setDescription(t('The user ID of the author of the current revision.'))
  51. ->setSetting('target_type', 'user')
  52. ->setRevisionable(TRUE);
  53. \Drupal::entityDefinitionUpdateManager()
  54. ->installFieldStorageDefinition('revision_user', 'block_content', 'block_content', $revision_user);
  55. }
  56. /**
  57. * Fix the block_content entity type to specify its revision data table.
  58. */
  59. function block_content_update_8300() {
  60. $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  61. $entity_type = $definition_update_manager->getEntityType('block_content');
  62. $entity_type->set('revision_data_table', 'block_content_field_revision');
  63. $definition_update_manager->updateEntityType($entity_type);
  64. }
  65. /**
  66. * Add a publishing status field for block_content entities.
  67. */
  68. function block_content_update_8400() {
  69. $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  70. // Add the published entity key to the block_content entity type.
  71. $entity_type = $definition_update_manager->getEntityType('block_content');
  72. $entity_keys = $entity_type->getKeys();
  73. $entity_keys['published'] = 'status';
  74. $entity_type->set('entity_keys', $entity_keys);
  75. $definition_update_manager->updateEntityType($entity_type);
  76. // Add the publishing status field to the block_content entity type.
  77. $status = BaseFieldDefinition::create('boolean')
  78. ->setLabel(new TranslatableMarkup('Publishing status'))
  79. ->setDescription(new TranslatableMarkup('A boolean indicating the published state.'))
  80. ->setRevisionable(TRUE)
  81. ->setTranslatable(TRUE)
  82. ->setDefaultValue(TRUE);
  83. $has_content_translation_status_field = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'block_content');
  84. if ($has_content_translation_status_field) {
  85. $status->setInitialValueFromField('content_translation_status', TRUE);
  86. }
  87. else {
  88. $status->setInitialValue(TRUE);
  89. }
  90. $definition_update_manager->installFieldStorageDefinition('status', 'block_content', 'block_content', $status);
  91. // Uninstall the 'content_translation_status' field if needed.
  92. $database = \Drupal::database();
  93. if ($has_content_translation_status_field) {
  94. // First we have to remove the field data.
  95. $database->update($entity_type->getDataTable())
  96. ->fields(['content_translation_status' => NULL])
  97. ->execute();
  98. // A site may have disabled revisionability for this entity type.
  99. if ($entity_type->isRevisionable()) {
  100. $database->update($entity_type->getRevisionDataTable())
  101. ->fields(['content_translation_status' => NULL])
  102. ->execute();
  103. }
  104. $content_translation_status = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'block_content');
  105. $definition_update_manager->uninstallFieldStorageDefinition($content_translation_status);
  106. }
  107. }
  108. /**
  109. * Add 'reusable' field to 'block_content' entities.
  110. */
  111. function block_content_update_8600() {
  112. $reusable = BaseFieldDefinition::create('boolean')
  113. ->setLabel(t('Reusable'))
  114. ->setDescription(t('A boolean indicating whether this block is reusable.'))
  115. ->setTranslatable(FALSE)
  116. ->setRevisionable(FALSE)
  117. ->setDefaultValue(TRUE)
  118. ->setInitialValue(TRUE);
  119. \Drupal::entityDefinitionUpdateManager()
  120. ->installFieldStorageDefinition('reusable', 'block_content', 'block_content', $reusable);
  121. }