Ability to limit templates by entity/bundle

Created on 8 June 2024, 6 months ago
Updated 24 July 2024, 4 months ago

Is it possible to limit the availability of particular templates by content type? I don't see anything, but I just want to confirm. I see there's an event subscriber for the LayoutParagraphsAllowedTypesEvent::EVENT_NAME event that I could potentially use to accomplish this, but it's not clear how to use it for this purpose. When I dump out the variables there, I don't seem to have the context of the node I'm inside of.

✨ Feature request
Status

Closed: works as designed

Version

2.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States zengenuity

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @zengenuity
  • πŸ‡ΊπŸ‡ΈUnited States zengenuity

    For anyone else looking for this, I figured out how to do it with that event. Here's the important part of my subscriber.

    <?php
    /**
       * {@inheritDoc}
       */
      public static function getSubscribedEvents() {
        // Run this with low priority so that other subscribers can add their templates first.
        return [
          LayoutParagraphsAllowedTypesEvent::EVENT_NAME => ['filterTemplates', -100],
        ];
      }
    
      /**
       * Filter templates to the allowed types.
       *
       * @param \Drupal\layout_paragraphs\Event\LayoutParagraphsAllowedTypesEvent $event
       *   The allowed types event.
       */
      public function filterTemplates(LayoutParagraphsAllowedTypesEvent $event) {
        $template_types = $event->getTypes();
    
        if (($layout = $event->getLayout()) && ($entity = $layout->getEntity())) {
          $entity_type = $entity->getEntityTypeId();
          $bundle = $entity->bundle();
    
          // Your logic here to remove items from $template_types
    
          $event->setTypes($template_types);
        }
      }
    ?>
  • Status changed to Closed: works as designed 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States zengenuity
Production build 0.71.5 2024