Skip to content
Snippets Groups Projects
Select Git revision
  • 8.9.x
  • 11.x default protected
  • 11.2.x protected
  • 10.5.x protected
  • 10.6.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.2 protected
  • 11.2.3 protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
40 results

ManageOptions.php

Blame
  • 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;
      }
    
    }