You are here

class MoveBlockController in Layout Builder Restrictions 8.2

Defines a controller to move a block.

@internal Controller classes are internal.

Hierarchy

Expanded class hierarchy of MoveBlockController

File

src/Controller/MoveBlockController.php, line 17

Namespace

Drupal\layout_builder_restrictions\Controller
View source
class MoveBlockController extends MoveBlockControllerCore {

  /**
   * Called if a block move fails layout_builder_restriction validation.
   *
   * @see \Drupal\layout_builder\Controller\MoveBlockController::move()
   */
  public function restrictLayout(SectionStorageInterface $section_storage, $error) {
    $response = new AjaxResponse();
    $layout = [
      '#type' => 'layout_builder',
      '#section_storage' => $section_storage,
    ];

    // Revert layout and present error dialog.
    $response
      ->addCommand(new ReplaceCommand('#layout-builder', $layout));
    $response
      ->addCommand(new OpenDialogCommand("#layout-builder-restrictions-error", "Content cannot be placed.", $error));
    return $response;
  }

  /**
   * {@inheritdoc}
   */
  public function build(SectionStorageInterface $section_storage, $delta_from, $delta_to, $region_to, $block_uuid, $preceding_block_uuid = NULL) {

    // Retrieve defined Layout Builder Restrictions plugins.
    $layout_builder_restrictions_manager = \Drupal::service('plugin.manager.layout_builder_restriction');
    $restriction_plugins = $layout_builder_restrictions_manager
      ->getSortedPlugins();
    foreach (array_keys($restriction_plugins) as $id) {
      $plugin = $layout_builder_restrictions_manager
        ->createInstance($id);
      $block_status = $plugin
        ->blockAllowedinContext($section_storage, $delta_from, $delta_to, $region_to, $block_uuid, $preceding_block_uuid);
      if ($block_status !== TRUE) {
        return $this
          ->restrictLayout($section_storage, $block_status);
      }
    }
    return parent::build($section_storage, $delta_from, $delta_to, $region_to, $block_uuid, $preceding_block_uuid);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LayoutRebuildTrait::rebuildAndClose protected function Rebuilds the layout.
LayoutRebuildTrait::rebuildLayout protected function Rebuilds the layout.
MoveBlockController::$layoutTempstoreRepository protected property The layout tempstore repository.
MoveBlockController::build public function Moves a block to another region. Overrides MoveBlockController::build
MoveBlockController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
MoveBlockController::restrictLayout public function Called if a block move fails layout_builder_restriction validation.
MoveBlockController::__construct public function LayoutController constructor.