1 system.install system_update_1025()

Convert block positioning and configuration to the default layout.

Related topics

File

core/modules/system/system.install, line 2284
Install, update and uninstall functions for the system module.

Code

function system_update_1025() {
  update_module_enable(array('layout'));

  // When upgrading from Drupal 7 disable legacy layouts initially.
  $layout_settings_config = config('layout.settings');
  $legacy = array('one_column', 'two_column', 'two_column_flipped', 'three_three_four_column');
  $layout_settings_config->set('excluded_templates', $legacy);
  $layout_settings_config->save();

  // Manually install schema, as update_module_enable() does not do this for us.
  if (!db_table_exists('cache_layout_path')) {
    $cache_table = backdrop_get_schema_unprocessed('system', 'cache');
    $cache_table['description'] = 'Cache table to store which layouts are active at a given path.';
    db_create_table('cache_layout_path', $cache_table);
  }

  $admin_layout = array(
    'path' => NULL,
    'name' => 'admin_default',
    'title' => 'Default Administrative Layout',
    'description' => NULL,
    'module' => 'layout',
    'weight' => 1,
    'layout_template' => 'boxton',
    'settings' => array(),
    'positions' => array(
      'content' => array(
        'default',
      ),
      'top' => array(
        '55f542fe-0763-453b-ba3e-62677ea559e5',
      ),
    ),
    'content' => array(
      '55f542fe-0763-453b-ba3e-62677ea559e5' => array(
        'plugin' => 'system:breadcrumb',
        'data' => array(
          'module' => 'system',
          'delta' => 'breadcrumb',
        ),
        'uuid' => '55f542fe-0763-453b-ba3e-62677ea559e5',
      ),
      'default' => array(
        'plugin' => 'system:main',
        'data' => array(
          'module' => 'system',
          'delta' => 'main',
        ),
        'uuid' => 'default',
      ),
    ),
  );
  $admin_config = config('layout.layout.admin_default');
  $admin_config->setData($admin_layout);
  $admin_config->save();

  $layout_name = state_get('upgrade_layout_name', 'simmons');

  $default_layout = array(
    'path' => NULL,
    'name' => 'default',
    'title' => 'Default Layout',
    'description' => NULL,
    'module' => 'layout',
    'weight' => 0,
    'layout_template' => $layout_name,
    'settings' => array(),
    'positions' => array(
      'header' => array(
        '84c43df4-1165-4faf-8700-c362a7153c0b',
        '13b2f325-af26-40bb-8c61-97fe1d93f19f',
      ),
      'content' => array(
        'default',
      ),
      'footer' => array(
        'cd4d033d-3f67-4191-8655-85ee758877c0',
      ),
      'top' => array(
        '09f11b4e-877e-45ab-9000-a4c6254faa04',
      ),
    ),
    'content' => array(
      '84c43df4-1165-4faf-8700-c362a7153c0b' => array(
        'plugin' => 'system:header',
        'data' => array(
          'module' => 'system',
          'delta' => 'header',
          'settings' => array(),
          'uuid' => '84c43df4-1165-4faf-8700-c362a7153c0b',
        ),
      ),
      '13b2f325-af26-40bb-8c61-97fe1d93f19f' => array(
        'plugin' => 'system:main-menu',
        'data' => array(
          'module' => 'system',
          'delta' => 'main-menu',
          'uuid' => '13b2f325-af26-40bb-8c61-97fe1d93f19f',
          'settings' => array(
            'title_display' => 'none',
          ),
        ),
      ),
      '09f11b4e-877e-45ab-9000-a4c6254faa04' => array(
        'plugin' => 'system:breadcrumb',
        'data' => array(
          'module' => 'system',
          'delta' => 'breadcrumb',
          'uuid' => '09f11b4e-877e-45ab-9000-a4c6254faa04',
        ),
      ),
      'default' => array(
        'plugin' => 'system:main',
        'data' => array(
          'module' => 'system',
          'delta' => 'main',
          'uuid' => 'default',
        ),
      ),
    ),
  );

  $current_theme = update_variable_get('theme_default', 'bartik');
  $layout_regions = state_get('upgrade_layout_regions', array(
    'header' => 'header',
    'top' => 'top',
    'content' => 'content',
    'sidebar_first' => 'sidebar',
    'sidebar_second' => 'sidebar2',
    'triptych_first' => 'third1',
    'triptych_middle' => 'third2',
    'triptych_last' => 'third3',
    'footer_firstcolumn' => 'quarter1',
    'footer_secondcolumn' => 'quarter2',
    'footer_thirdcolumn' => 'quarter3',
    'footer_fourthcolumn' => 'quarter4',
    'footer' => 'footer',
  ));
  $default_layout['layout_template'] = $layout_name;

  // Prepare a node-specific layout to be used if necessary.
  $node_layout = $default_layout;
  $node_layout['title'] = t('Node layout');
  $node_layout['name'] = 'node';
  $node_layout['path'] = 'node/%';
  unset($node_layout['module']);
  $node_layout_required = FALSE;

  $result = db_query("SELECT * FROM {block} WHERE theme = :theme AND status = 1 AND NOT (module = 'system' AND delta = 'main') ORDER BY region, weight", array(':theme' => $current_theme));
  foreach ($result as $block_row) {
    if (array_key_exists($block_row->region, $layout_regions)) {
      $region = $layout_regions[$block_row->region];
      $is_node_block = FALSE;

      // Create a new layout block object.
      $uuid = new Uuid();
      $block_uuid = $uuid->generate();
      $block_config = array(
        'plugin' => $block_row->module . ':' . $block_row->delta,
        'data' => array(
          'module' => $block_row->module,
          'delta' => $block_row->delta,
          'uuid' => $block_uuid,
          'conditions' => array(),
        ),
      );

      // Add any path restrictions, excluding PHP visibility rules.
      $pages = trim($block_row->pages);
      $visibility = (int) $block_row->visibility;
      if ($pages && $visibility !== 2) {
        $settings = array(
          'paths' => $pages,
          'visibility_setting' => $visibility,
        );
        $access_config = array(
          'plugin' => 'path',
          'data' => array(
            'settings' => $settings,
          ),
        );
        $block_config['data']['conditions'][] = $access_config;
      }

      // Add any role restrictions.
      if (db_table_exists('block_role')) {
        $role_result = db_query("SELECT * FROM {block_role} WHERE module = :module AND delta = :delta", array(':module' => $block_row->module, ':delta' => $block_row->delta));
        $roles = array();
        foreach ($role_result as $role_row) {
          switch ($role_row->rid) {
            case 1:
              $roles[] = 'anonymous';
              break;
            case 2:
              $roles[] = 'authenticated';
              break;
            default:
              $roles[] = $role_row->rid;
          }
        }
        if ($roles) {
          $settings = array('roles' => $roles);
          $access_config = array(
            'plugin' => 'user_role',
            'data' => array(
              'settings' => $settings,
            ),
          );
          $block_config['data']['conditions'][] = $access_config;
        }
      }

      // Add any node type restrictions.
      if (db_table_exists('block_node_type')) {
        $type_result = db_query("SELECT * FROM {block_node_type} WHERE module = :module AND delta = :delta", array(':module' => $block_row->module, ':delta' => $block_row->delta));
        $types = array();
        foreach ($type_result as $type_row) {
          $types[] = $type_row->type;
          $node_layout_required = TRUE;
          $is_node_block = TRUE;
        }
        if ($types) {
          $settings = array('bundles' => $types);
          $access_config = array(
            'plugin' => 'node:bundle',
            'data' => array(
              'settings' => $settings,
            ),
          );
          $block_config['data']['conditions'][] = $access_config;
        }
      }

      // Position the block in the default layout if not node-type specific.
      if (!$is_node_block) {
        $default_layout['positions'][$region][] = $block_uuid;
        $default_layout['content'][$block_uuid] = $block_config;
      }

      // Position the block in the node layout.
      $node_layout['positions'][$region][] = $block_uuid;
      $node_layout['content'][$block_uuid] = $block_config;
    }
  }

  // Save the layout with the migrated blocks in position.
  $layout_config = config('layout.layout.default');
  $layout_config->setData($default_layout);
  $layout_config->save();

  // If any node-specific blocks were found, save the node layout as well.
  if ($node_layout_required) {
    $layout_config = config('layout.layout.node');
    $layout_config->setData($node_layout);
    $layout_config->save();
  }

  // Set layout as the default route handler.
  config_set('system.core', 'menu_route_handler', 'layout_route_handler');
}