You are here

public function PageManager::saveInstances in Bootstrap Layouts 8.4

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

Saves layout instances.

Parameters

\Drupal\bootstrap_layouts\BootstrapLayout[] $layouts: An associative array of BootstrapLayout instances, keyed by identifier.

Throws

\Drupal\Core\Entity\EntityStorageException In case of failures an exception is thrown.

Overrides BootstrapLayoutsHandlerInterface::saveInstances

File

src/Plugin/BootstrapLayouts/PageManager.php, line 52

Class

PageManager
Handles Display Suite specific layout implementations.

Namespace

Drupal\bootstrap_layouts\Plugin\BootstrapLayouts

Code

public function saveInstances(array $layouts = []) {

  /** @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager */
  $entity_type_manager = $this->container
    ->get('entity_type.manager');

  /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface[] $config_entities */
  $config_entities = $entity_type_manager
    ->getStorage('page_variant')
    ->loadByProperties([
    'variant' => 'panels_variant',
    'id' => array_keys($layouts),
  ]);

  /** @var \Drupal\bootstrap_layouts\BootstrapLayout[] $layouts */
  foreach ($layouts as $entity_id => $layout) {
    $config_entity = $config_entities[$entity_id];
    $info = $config_entity
      ->get('variant_settings');
    $info['layout'] = $layout
      ->getId();

    // The region is stored inside the block array. To effectively change
    // a region for a block, the variant's blocks must be iterated over and
    // changed manually based on the associative region key provided by the
    // BootstrapLayout instance.
    $info['blocks'] = [];
    foreach ($layout
      ->getRegions() as $region => $blocks) {
      foreach ($blocks as $uuid => $block) {
        $block['region'] = $region;
        $info['blocks'][$uuid] = $block;
      }
    }
    $info['layout_settings'] = $layout
      ->getSettings();
    $config_entity
      ->set('variant_settings', $info)
      ->save();
  }
}