You are here

public function BootstrapLayoutsUpdate8401::processExistingLayout in Bootstrap Layouts 8.4

Same name and namespace in other branches
  1. 8.5 src/Plugin/BootstrapLayouts/Updates/BootstrapLayoutsUpdate8401.php \Drupal\bootstrap_layouts\Plugin\BootstrapLayouts\Updates\BootstrapLayoutsUpdate8401::processExistingLayout()

Provide an update for an existing layout.

Note: this process any existing layout and is not specific to just "Bootstrap Layouts" based layouts. If implementing this update, you should check the $layout->getId() before performing any tasks.

Parameters

\Drupal\bootstrap_layouts\BootstrapLayout $layout: The existing BootstrapLayout instance that is being processed.

array $data: Any static YAML data found for the update.

bool $display_messages: Flag determining whether a message will be displayed indicating whether the layout was processed successfully or not.

Overrides BootstrapLayoutsUpdateBase::processExistingLayout

See also

\Drupal\bootstrap_layouts\BootstrapLayoutsManager::update()

File

src/Plugin/BootstrapLayouts/Updates/BootstrapLayoutsUpdate8401.php, line 23

Class

BootstrapLayoutsUpdate8401
Bootstrap Layouts Update 8401

Namespace

Drupal\bootstrap_layouts\Plugin\BootstrapLayouts\Updates

Code

public function processExistingLayout(BootstrapLayout $layout, array $data = [], $display_messages = TRUE) {

  // Fix any typos and replace hyphens with underscores.
  $id = preg_replace('/\\-+/', '_', preg_replace('/^booststrap/', 'bootstrap', $layout
    ->getId()));

  // Immediately return if existing layout identifier doesn't match
  // one of the old "bootstrap_layouts" layouts.
  if (!isset($data['bootstrap_layouts_update_map'][$id])) {
    return;
  }
  $layout_map = $data['bootstrap_layouts_update_map'][$id];

  // Set the new layout identifier.
  $layout
    ->setId($layout_map['id']);

  // Only update the path if it's actually set.
  $path = $layout
    ->getPath();
  if (isset($path)) {
    $layout
      ->setPath($this
      ->getPath() . '/templates/3.0.0');
  }

  // Set default layout wrapper, attributes and classes.
  $layout
    ->setSetting('layout.wrapper', 'div');
  $layout
    ->setSetting('layout.classes', [
    'row',
    'clearfix',
  ]);
  $layout
    ->setSetting('layout.attributes', '');

  // Rename existing region and set region wrapper, attributes and classes.
  foreach ($layout_map['regions'] as $old_region => $new_region) {
    if ($old_region !== $new_region && ($region_data = $layout
      ->getRegion($old_region))) {
      $layout
        ->setRegion($new_region, $region_data);
      $layout
        ->unsetRegion($old_region);
    }
    $layout
      ->setSetting("regions.{$new_region}.wrapper", 'div');
    $layout
      ->setSetting("regions.{$new_region}.classes", $layout_map['classes'][$new_region]);
    $layout
      ->setSetting("regions.{$new_region}.attributes", '');
  }
}