You are here

public function LayoutParagraphsAllowedTypesSubscriber::typeRestrictions in Layout Paragraphs 2.0.x

Restricts available types based on settings in layout.

Parameters

\Drupal\layout_paragraphs\Event\LayoutParagraphsAllowedTypesEvent $event: The allowed types event.

Return value

bool

File

src/EventSubscriber/LayoutParagraphsAllowedTypesSubscriber.php, line 24

Class

LayoutParagraphsAllowedTypesSubscriber

Namespace

Drupal\layout_paragraphs\EventSubscriber

Code

public function typeRestrictions(LayoutParagraphsAllowedTypesEvent $event) {
  $parent_uuid = $event
    ->getParentUuid();
  $types = $event
    ->getTypes();
  $layout = $event
    ->getLayout();
  $settings = $layout
    ->getSettings();
  if ($settings['require_layouts'] && !$parent_uuid) {
    $event
      ->setTypes(array_filter($types, function ($type) {
      return $type['is_section'] === TRUE;
    }));
  }
  $depth = 0;
  while ($parent = $layout
    ->getComponentByUuid($parent_uuid)) {
    $depth++;
    $parent_uuid = $parent
      ->getParentUuid();
  }
  if ($depth > $settings['nesting_depth']) {
    $event
      ->setTypes(array_filter($types, function ($type) {
      return $type['is_section'] === FALSE;
    }));
  }
}