system.post_update.php 5.35 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Post update functions for System.
 */

8
use Drupal\Core\Config\Entity\ConfigEntityUpdater;
9
use Drupal\Core\Entity\Display\EntityDisplayInterface;
10
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
11 12 13
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;

14
/**
15
 * Re-save all configuration entities to recalculate dependencies.
16
 */
17 18 19 20 21
function system_post_update_recalculate_configuration_entity_dependencies(&$sandbox = NULL) {
  if (!isset($sandbox['config_names'])) {
    $sandbox['config_names'] = \Drupal::configFactory()->listAll();
    $sandbox['count'] = count($sandbox['config_names']);
  }
22 23
  /** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
  $config_manager = \Drupal::service('config.manager');
24 25 26 27

  $count = 0;
  foreach ($sandbox['config_names'] as $key => $config_name) {
    if ($entity = $config_manager->loadConfigEntityByName($config_name)) {
28 29
      $entity->save();
    }
30 31 32 33 34 35
    unset($sandbox['config_names'][$key]);
    $count++;
    // Do 50 at a time.
    if ($count == 50) {
      break;
    }
36 37
  }

38 39
  $sandbox['#finished'] = empty($sandbox['config_names']) ? 1 : ($sandbox['count'] - count($sandbox['config_names'])) / $sandbox['count'];
  return t('Configuration dependencies recalculated');
40 41
}

42 43 44 45 46 47 48 49 50 51 52
/**
 * Update entity displays to contain the region for each field.
 */
function system_post_update_add_region_to_entity_displays() {
  $entity_save = function (EntityDisplayInterface $entity) {
    // preSave() will fill in the correct region based on the 'type'.
    $entity->save();
  };
  array_map($entity_save, EntityViewDisplay::loadMultiple());
  array_map($entity_save, EntityFormDisplay::loadMultiple());
}
53 54

/**
55
 * Force caches using hashes to be cleared (Twig, render cache, etc.).
56
 */
57
function system_post_update_hashes_clear_cache() {
58 59
  // Empty post-update hook.
}
60 61 62 63 64 65 66 67 68

/**
 * Force plugin definitions to be cleared.
 *
 * @see https://www.drupal.org/node/2802663
 */
function system_post_update_timestamp_plugins() {
  // Empty post-update hook.
}
69 70 71 72 73 74 75 76

/**
 * Clear caches to ensure Classy's message library is always added.
 */
function system_post_update_classy_message_library() {
  // Empty post-update hook.
}

77 78 79 80 81 82 83 84
/**
 * Force field type plugin definitions to be cleared.
 *
 * @see https://www.drupal.org/node/2403703
 */
function system_post_update_field_type_plugins() {
  // Empty post-update hook.
}
85 86 87 88 89 90 91

/**
 * Clear caches due to schema changes in core.entity.schema.yml.
 */
function system_post_update_field_formatter_entity_schema() {
  // Empty post-update hook.
}
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

/**
 * Change plugin IDs of actions.
 */
function system_post_update_change_action_plugins() {
  $old_new_action_id_map = [
    'comment_publish_action' => 'entity:publish_action:comment',
    'comment_unpublish_action' => 'entity:unpublish_action:comment',
    'comment_save_action' => 'entity:save_action:comment',
    'node_publish_action' => 'entity:publish_action:node',
    'node_unpublish_action' => 'entity:unpublish_action:node',
    'node_save_action' => 'entity:save_action:node',
  ];

  /** @var \Drupal\system\Entity\Action[] $actions */
  $actions = \Drupal::entityTypeManager()->getStorage('action')->loadMultiple();
  foreach ($actions as $action) {
    if (isset($old_new_action_id_map[$action->getPlugin()->getPluginId()])) {
      $action->setPlugin($old_new_action_id_map[$action->getPlugin()->getPluginId()]);
      $action->save();
    }
  }
}
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

/**
 * Change plugin IDs of delete actions.
 */
function system_post_update_change_delete_action_plugins() {
  $old_new_action_id_map = [
    'comment_delete_action' => 'entity:delete_action:comment',
    'node_delete_action' => 'entity:delete_action:node',
  ];

  /** @var \Drupal\system\Entity\Action[] $actions */
  $actions = \Drupal::entityTypeManager()->getStorage('action')->loadMultiple();
  foreach ($actions as $action) {
    if (isset($old_new_action_id_map[$action->getPlugin()->getPluginId()])) {
      $action->setPlugin($old_new_action_id_map[$action->getPlugin()->getPluginId()]);
      $action->save();
    }
  }
}
134 135 136 137 138 139 140 141 142

/**
 * Force cache clear for language item callback.
 *
 * @see https://www.drupal.org/node/2851736
 */
function system_post_update_language_item_callback() {
  // Empty post-update hook.
}
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

/**
 * Update all entity displays that contain extra fields.
 */
function system_post_update_extra_fields(&$sandbox = NULL) {
  $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);
  $entity_field_manager = \Drupal::service('entity_field.manager');

  $callback = function (EntityDisplayInterface $display) use ($entity_field_manager) {
    $display_context = $display instanceof EntityViewDisplayInterface ? 'display' : 'form';
    $extra_fields = $entity_field_manager->getExtraFields($display->getTargetEntityTypeId(), $display->getTargetBundle());

    // If any extra fields are used as a component, resave the display with the
    // updated component information.
    $needs_save = FALSE;
    if (!empty($extra_fields[$display_context])) {
      foreach ($extra_fields[$display_context] as $name => $extra_field) {
        if ($component = $display->getComponent($name)) {
          $display->setComponent($name, $component);
          $needs_save = TRUE;
        }
      }
    }
    return $needs_save;
  };

  $config_entity_updater->update($sandbox, 'entity_form_display', $callback);
  $config_entity_updater->update($sandbox, 'entity_view_display', $callback);
}