You are here

class LayoutParagraphsAllowedTypesSubscriber in Layout Paragraphs 2.0.x

Hierarchy

Expanded class hierarchy of LayoutParagraphsAllowedTypesSubscriber

1 string reference to 'LayoutParagraphsAllowedTypesSubscriber'
layout_paragraphs.services.yml in ./layout_paragraphs.services.yml
layout_paragraphs.services.yml
1 service uses LayoutParagraphsAllowedTypesSubscriber
layout_paragraphs_allowed_types_subscriber in ./layout_paragraphs.services.yml
\Drupal\layout_paragraphs\EventSubscriber\LayoutParagraphsAllowedTypesSubscriber

File

src/EventSubscriber/LayoutParagraphsAllowedTypesSubscriber.php, line 8

Namespace

Drupal\layout_paragraphs\EventSubscriber
View source
class LayoutParagraphsAllowedTypesSubscriber implements EventSubscriberInterface {
  public static function getSubscribedEvents() {
    return [
      LayoutParagraphsAllowedTypesEvent::EVENT_NAME => 'typeRestrictions',
    ];
  }

  /**
   * Restricts available types based on settings in layout.
   *
   * @param \Drupal\layout_paragraphs\Event\LayoutParagraphsAllowedTypesEvent $event
   *   The allowed types event.
   *
   * @return bool
   */
  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;
      }));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LayoutParagraphsAllowedTypesSubscriber::getSubscribedEvents public static function
LayoutParagraphsAllowedTypesSubscriber::typeRestrictions public function Restricts available types based on settings in layout.