You are here

layout_builder_restrictions.install in Layout Builder Restrictions 8.2

Contains install and update functions for Layout Builder Restrictions.

File

layout_builder_restrictions.install
View source
<?php

/**
 * @file
 * Contains install and update functions for Layout Builder Restrictions.
 */

/**
 * Move third-party settings.
 */
function layout_builder_restrictions_update_8201(&$sandbox) {

  // When upgrading from 8.x-1.x to 8.x-2.x, it is necessary to move existing
  // entity_view_mode_restriction configuration to its own namespace.
  $config_factory = \Drupal::configFactory();
  if (!isset($sandbox['count'])) {
    $sandbox['ids'] = $config_factory
      ->listAll('core.entity_view_display.');
    $sandbox['count'] = count($sandbox['ids']);
  }
  $ids = array_splice($sandbox['ids'], 0, 50);
  foreach ($ids as $id) {
    $display = $config_factory
      ->getEditable($id);
    if ($display
      ->get('third_party_settings.layout_builder_restrictions')) {
      $settings = $display
        ->get('third_party_settings.layout_builder_restrictions');
      $display
        ->clear('third_party_settings.layout_builder_restrictions')
        ->set('third_party_settings.layout_builder_restrictions.entity_view_mode_restriction', $settings)
        ->save();
    }
  }
  $sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
}

/**
 * Relocate all block_content configuration settings under "Custom blocks".
 */
function layout_builder_restrictions_update_8202(&$sandbox) {

  // Per #3091631, Layout Builder Restrictions will use the block_content
  // provider match, rather than the mutable "Custom" category definition.
  // Accordingly, any block_content restrictions that were previously stored
  // in other categories (e.g., an arbitrarily named "Reusable content")
  // should move to the "Custom blocks" configuration
  // category.
  $config_factory = \Drupal::configFactory();
  if (!isset($sandbox['count'])) {
    $sandbox['ids'] = $config_factory
      ->listAll('core.entity_view_display.');
    $sandbox['count'] = count($sandbox['ids']);
  }
  $ids = array_splice($sandbox['ids'], 0, 50);
  foreach ($ids as $id) {
    $display = $config_factory
      ->getEditable($id);
    if ($display
      ->get('third_party_settings.layout_builder_restrictions')) {
      $allowed_blocks_by_category = $display
        ->get('third_party_settings.layout_builder_restrictions.entity_view_mode_restriction.allowed_blocks');
      foreach ($allowed_blocks_by_category as $category => $block_ids) {
        if ($category == 'Custom blocks') {
          continue;
        }
        foreach ($block_ids as $id) {
          if (strpos($id, 'block_content:') === 0) {
            $allowed_blocks_by_category['Custom blocks'][] = $id;

            // Remove this block_content from its previous category so
            // that it is defined only in one place.
            unset($allowed_blocks_by_category[$category][$id]);
          }
        }
      }
      $display
        ->set('third_party_settings.layout_builder_restrictions.entity_view_mode_restriction.allowed_blocks', $allowed_blocks_by_category)
        ->save();
    }
  }
  $sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
}

/**
 * Notes on update for multilingual sites.
 */
function layout_builder_restrictions_update_8210() {
  $moduleHandler = \Drupal::service('module_handler');
  if ($moduleHandler
    ->moduleExists('locale')) {
    $message = t("Please note: since your site uses the Locale module, you will likely need to manually resave each Layout Builder Restriction entity configuration, due to new code that makes Layout Builder Restrictions' configuration multilingual compatible. For more information, see <a href='https://www.drupal.org/project/layout_builder_restrictions/releases/8.x-2.6'>Layout Builder Restrictions version 2.6 release notes</a>.");
    \Drupal::logger('layout_builder_restrictions')
      ->warning($message);
    return $message;
  }
}

/**
 * Add new configuration defaults for default block & layout settings.
 */
function layout_builder_restrictions_update_8211(&$sandbox) {

  // When upgrading to 2.6, new configuration keys are added to control
  // whether newly available block categories & layouts are restricted.
  // Set the defaults to "allowed" for both, to match previous site behavior.
  // Sites may then explictly restrict new blocks & layouts going forward.
  $config_factory = \Drupal::configFactory();
  if (!isset($sandbox['count'])) {
    $sandbox['ids'] = $config_factory
      ->listAll('core.entity_view_display.');
    $sandbox['count'] = count($sandbox['ids']);
  }
  $ids = array_splice($sandbox['ids'], 0, 50);
  foreach ($ids as $id) {
    $display = $config_factory
      ->getEditable($id);
    if ($display
      ->get('third_party_settings.layout_builder_restrictions')) {
      $settings = $display
        ->get('third_party_settings.layout_builder_restrictions');
      if (!isset($settings['allowed_block_categories'])) {

        // The empty array means "newly available block categories are allowed
        // by default," which maintains how restrictions behaved prior to the
        // new ability to set the default for newly available categories.
        $display
          ->set('third_party_settings.layout_builder_restrictions.allowed_block_categories', [])
          ->save();
      }
    }
  }
  $sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
}

/**
 * Relocate 'allowed_blocks' to 'whitelisted_blocks'.
 */
function layout_builder_restrictions_update_8212(&$sandbox) {

  // When upgrading to 2.6, configuration for 'allowed_blocks'
  // is relocated to 'whitelisted_blocks' to distinguish from newly available
  // 'blacklisted_blocks' (which will default to an empty array for existing
  // sites).
  $config_factory = \Drupal::configFactory();
  if (!isset($sandbox['count'])) {
    $sandbox['ids'] = $config_factory
      ->listAll('core.entity_view_display.');
    $sandbox['count'] = count($sandbox['ids']);
  }
  $ids = array_splice($sandbox['ids'], 0, 50);
  foreach ($ids as $id) {
    $display = $config_factory
      ->getEditable($id);
    if ($display
      ->get('third_party_settings.layout_builder_restrictions')) {
      $allowed_blocks_by_category = $display
        ->get('third_party_settings.layout_builder_restrictions.entity_view_mode_restriction.allowed_blocks');
      $display
        ->clear('third_party_settings.layout_builder_restrictions.entity_view_mode_restriction.allowed_blocks')
        ->set('third_party_settings.layout_builder_restrictions.entity_view_mode_restriction.whitelisted_blocks', $allowed_blocks_by_category)
        ->set('third_party_settings.layout_builder_restrictions.entity_view_mode_restriction.blacklisted_blocks', [])
        ->save();
    }
  }
  $sandbox['#finished'] = empty($sandbox['ids']) ? 1 : ($sandbox['count'] - count($sandbox['ids'])) / $sandbox['count'];
}

Functions

Namesort descending Description
layout_builder_restrictions_update_8201 Move third-party settings.
layout_builder_restrictions_update_8202 Relocate all block_content configuration settings under "Custom blocks".
layout_builder_restrictions_update_8210 Notes on update for multilingual sites.
layout_builder_restrictions_update_8211 Add new configuration defaults for default block & layout settings.
layout_builder_restrictions_update_8212 Relocate 'allowed_blocks' to 'whitelisted_blocks'.