You are here

class ChooseBlockController in Layout Builder Restrictions 8.2

Defines a controller to choose a new block.

@internal Controller classes are internal.

Hierarchy

Expanded class hierarchy of ChooseBlockController

File

src/Controller/ChooseBlockController.php, line 14

Namespace

Drupal\layout_builder_restrictions\Controller
View source
class ChooseBlockController extends ChooseBlockControllerCore {

  /**
   * {@inheritdoc}
   */
  public function build(SectionStorageInterface $section_storage, $delta, $region) {
    $build = parent::build($section_storage, $delta, $region);

    // 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);
      $allowed_inline_blocks = $plugin
        ->inlineBlocksAllowedinContext($section_storage, $delta, $region);

      // If no inline blocks are allowed, remove the "Create custom block" link.
      if (empty($allowed_inline_blocks)) {
        unset($build['add_block']);
      }
    }
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function inlineBlockList(SectionStorageInterface $section_storage, $delta, $region) {
    $build = parent::inlineBlockList($section_storage, $delta, $region);

    // 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);
      $allowed_inline_blocks = $plugin
        ->inlineBlocksAllowedinContext($section_storage, $delta, $region);

      // Loop through links and remove those for disallowed inline block types.
      foreach ($build['links']['#links'] as $key => $link) {
        $route_parameters = $link['url']
          ->getRouteParameters();
        if (!in_array($route_parameters['plugin_id'], $allowed_inline_blocks)) {
          unset($build['links']['#links'][$key]);
        }
      }
    }
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AjaxHelperTrait::getRequestWrapperFormat protected function Gets the wrapper format of the current request.
AjaxHelperTrait::isAjax protected function Determines if the current request is via AJAX.
ChooseBlockController::$blockManager protected property The block manager.
ChooseBlockController::$currentUser protected property The current user.
ChooseBlockController::$entityTypeManager protected property The entity type manager.
ChooseBlockController::build public function Provides the UI for choosing a new block. Overrides ChooseBlockController::build
ChooseBlockController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ChooseBlockController::getAjaxAttributes protected function Get dialog attributes if an ajax request.
ChooseBlockController::getBlockLinks protected function Gets a render array of block links.
ChooseBlockController::inlineBlockList public function Provides the UI for choosing a new inline block. Overrides ChooseBlockController::inlineBlockList
ChooseBlockController::__construct public function ChooseBlockController constructor.
LayoutBuilderContextTrait::$contextRepository protected property The context repository.
LayoutBuilderContextTrait::contextRepository protected function Gets the context repository service.
LayoutBuilderContextTrait::getAvailableContexts protected function Provides all available contexts, both global and section_storage-specific.
LayoutBuilderHighlightTrait::blockAddHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutBuilderHighlightTrait::blockUpdateHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutBuilderHighlightTrait::sectionAddHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutBuilderHighlightTrait::sectionUpdateHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.