Skip to content
Snippets Groups Projects
Select Git revision
  • 2.0.x
  • 8.x-1.x
  • 3519543-bug--sample-entity
  • 2.0.8
  • 2.0.7
  • 2.0.6
  • 2.0.5
  • 8.x-1.13
  • 8.x-1.12
  • 8.x-1.11
  • previous/3525646-2.1.0-copy-variant/2025-05-27
  • previous/3525775-computed-entity-ref/2025-05-27
  • 2.0.4
  • previous/3525646-2.1.0-copy-variant/2025-05-21
  • previous/3525646-2.1.0-copy-variant/2025-05-20
  • 2.0.3
  • 2.0.2
  • 2.0.1
  • 2.0.0
  • 2.0.0-rc2
  • 2.0.0-rc1
  • 2.0.0-beta6
  • 2.0.0-beta5
23 results

ComponentElementAlter.php

Blame
  • christian.wiedemann's avatar
    Issue #3490872 by christian.wiedemann, pdureau, just_like_good_vibes: Remove...
    christian.wiedemann authored and Pierre Dureau committed
    Issue #3490872 by christian.wiedemann, pdureau, just_like_good_vibes: Remove temporary compatibility layers
    7962d952
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ComponentElementAlter.php 2.08 KiB
    <?php
    
    declare(strict_types=1);
    
    namespace Drupal\ui_patterns_library\Element;
    
    use Drupal\Core\Security\TrustedCallbackInterface;
    use Drupal\Core\Theme\ComponentPluginManager;
    use Drupal\ui_patterns_library\StoriesSyntaxConverter;
    use Drupal\ui_patterns_library\StoryPluginManager;
    
    /**
     * Renders a component story.
     */
    class ComponentElementAlter implements TrustedCallbackInterface {
    
      /**
       * Constructs a ComponentElementAlter.
       *
       * @param \Drupal\Core\Theme\ComponentPluginManager $componentPluginManager
       *   The component plugin manager.
       * @param \Drupal\ui_patterns_library\StoryPluginManager $storyPluginManager
       *   The story plugin manager.
       * @param \Drupal\ui_patterns_library\StoriesSyntaxConverter $storiesConverter
       *   The stories syntax converter.
       */
      public function __construct(
        protected ComponentPluginManager $componentPluginManager,
        protected StoryPluginManager $storyPluginManager,
        protected StoriesSyntaxConverter $storiesConverter,
      ) {
      }
    
      /**
       * {@inheritdoc}
       */
      public static function trustedCallbacks() {
        return ['alter'];
      }
    
      /**
       * Alter SDC component element.
       */
      public function alter(array $element): array {
        $element = $this->loadStory($element);
        return $element;
      }
    
      /**
       * Load story from component definition.
       */
      protected function loadStory(array $element): array {
        if (!isset($element["#story"])) {
          return $element;
        }
        $story_id = $element["#story"];
        $component = $this->componentPluginManager->getDefinition($element["#component"]);
        $component["stories"] = $this->storyPluginManager->getComponentStories($element["#component"]);
        if (!isset($component["stories"])) {
          return $element;
        }
        if (!isset($component["stories"][$story_id])) {
          return $element;
        }
        $story = $component["stories"][$story_id];
        $slots = array_merge($story["slots"] ?? [], $element["#slots"] ?? []);
        $element["#slots"] = $this->storiesConverter->convertSlots($slots);
        $element["#props"] = array_merge($story["props"] ?? [], $element["#props"] ?? []);
        return $element;
      }
    
    }