You are here

simple_block_layout_builder.module in Simple Block 8

Hook implementations for 'Simple Block + Layout Builder' module.

File

modules/simple_block_layout_builder/simple_block_layout_builder.module
View source
<?php

/**
 * @file
 * Hook implementations for 'Simple Block + Layout Builder' module.
 */
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\simple_block_layout_builder\Form\EditSimpleBlockInLayoutBuilderForm;

/**
 * Implements hook_entity_type_alter().
 */
function simple_block_layout_builder_entity_type_alter(array &$entity_types) : void {

  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  if (isset($entity_types['simple_block'])) {
    $entity_types['simple_block']
      ->setFormClass('layout_builder', EditSimpleBlockInLayoutBuilderForm::class);
  }
}

/**
 * Implements hook_contextual_links_alter().
 */
function simple_block_layout_builder_contextual_links_alter(array &$links, $group, array $route_parameters) : void {
  if ($group !== 'layout_builder_block' || !isset($links['layout_builder_block_update'])) {
    return;
  }
  $params =& $links['layout_builder_block_update']['route_parameters'];
  if (!($display = \Drupal::entityTypeManager()
    ->getStorage('entity_view_display')
    ->load($params['section_storage']))) {

    // @todo Support for other kind of layout builders, such as lb_everywhere?
    return;
  }

  /** @var \Drupal\layout_builder\SectionStorage\SectionStorageManager $section_manager */
  $section_manager = \Drupal::service('plugin.manager.layout_builder.section_storage');
  $section_storage = $section_manager
    ->load($params['section_storage_type'], [
    'display' => EntityContext::fromEntity($display),
  ]);

  /** @var \Drupal\layout_builder\SectionStorageInterface $temp_storage */
  $temp_storage = \Drupal::service('layout_builder.tempstore_repository')
    ->get($section_storage);
  $section = $temp_storage
    ->getSection($params['delta']);
  $components = $section
    ->getComponents();
  if (!isset($components[$params['uuid']])) {
    return;
  }
  $plugin = $components[$params['uuid']]
    ->getPlugin();
  if ($plugin
    ->getBaseId() === 'simple_block') {
    $links['layout_builder_block_update']['route_name'] = 'simple_block_layout_builder.edit_block';
    $params['simple_block'] = $plugin
      ->getDerivativeId();
  }
}