You are here

private function LayoutBuilderMigration::toSection in Panelizer 8.5

Converts a Panels display to a single Layout Builder section.

Parameters

array $configuration: The Panels display configuration.

string $entity_type_id: The entity type ID associated with the display.

string $bundle: The entity bundle associated with the display.

Return value

\Drupal\layout_builder\Section A layout section with the same layout and blocks as the Panels display.

2 calls to LayoutBuilderMigration::toSection()
LayoutBuilderMigration::doProcessDisplay in src/LayoutBuilderMigration.php
Migrates a layout-able entity view display to Layout Builder.
LayoutBuilderMigration::doProcessEntity in src/LayoutBuilderMigration.php
Migrates a custom entity-specific Panelizer layout to Layout Builder.

File

src/LayoutBuilderMigration.php, line 195

Class

LayoutBuilderMigration
Provides functionality to migrate Panelizer data to Layout Builder.

Namespace

Drupal\panelizer

Code

private function toSection(array &$configuration, $entity_type_id, $bundle) {
  if (isset($configuration['static_context'])) {
    $static_contexts = $configuration['static_context'];
  }
  else {
    $static_contexts = [];
  }
  $to_component = function (array $block) use ($entity_type_id, $bundle, $static_contexts) {

    // Convert ctools_block field blocks to use Layout Builder's field_block.
    if ($block['provider'] === 'ctools_block' && strpos($block['id'], 'entity_field:') === 0) {
      list(, , $field_name) = explode(':', $block['id']);
      $block['provider'] = 'layout_builder';
      $block['id'] = "field_block:{$entity_type_id}:{$bundle}:{$field_name}";

      // Remove configuration keys that are moved to component-level settings.
      unset($block['formatter']['region'], $block['formatter']['weight']);

      // If the entity being panelized is referenced in the context mapping,
      // use the Layout Builder version of that.
      if (isset($block['context_mapping']['entity']) && $block['context_mapping']['entity'] === '@panelizer.entity_context:entity') {
        $block['context_mapping']['entity'] = 'layout_builder.entity';
      }
    }
    $plugin_definition = $this->blockManager
      ->getDefinition($block['id']);

    // The required context values must be passed directly in the plugin
    // configuration, or the plugin will throw an exception as soon as it is
    // instantiated. Note that this is only supported as of Drupal 8.8.

    /** @var \Drupal\Component\Plugin\Context\ContextDefinitionInterface $context_definition */
    foreach ($plugin_definition['context_definitions'] as $context_name => $context_definition) {
      if ($context_definition
        ->isRequired() && array_key_exists($context_name, $static_contexts)) {
        $block['context'][$context_name] = $static_contexts[$context_name]['value'];
      }
    }
    $block['configuration'] = $block;

    // Remove keys which are not actually part of the block configuration.
    unset($block['configuration']['provider'], $block['configuration']['region'], $block['configuration']['uuid'], $block['configuration']['weight']);
    $block += [
      'additional' => [],
    ];
    return SectionComponent::fromArray($block);
  };
  $layout_id = panels_convert_plugin_ids_to_layout_discovery($configuration['layout']);
  if ($layout_id) {
    $configuration['layout'] = $layout_id;
  }
  return new Section($configuration['layout'], $configuration['layout_settings'], array_map($to_component, $configuration['blocks']));
}