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