|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\patternkit\Element; |
| 4 | + |
| 5 | +use Drupal\Core\Render\BubbleableMetadata; |
| 6 | +use Drupal\Core\Render\Element\RenderElement; |
| 7 | +use Drupal\patternkit\Entity\PatternInterface; |
| 8 | +use Drupal\patternkit\PatternLibraryPluginInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * Provides a render element to display a pattern. |
| 12 | + * |
| 13 | + * Properties: |
| 14 | + * - #pattern: The loaded Pattern entity to be rendered. |
| 15 | + * - #config: Configuration to be passed to the pattern for rendering. |
| 16 | + * - #context: Context values for rendering the pattern. |
| 17 | + * |
| 18 | + * Usage Example: |
| 19 | + * @code |
| 20 | + * $build['example_pattern'] = [ |
| 21 | + * '#type' => 'pattern', |
| 22 | + * '#pattern' => 'example', |
| 23 | + * '#config' => [ |
| 24 | + * 'text' => '[node:title]', |
| 25 | + * 'formatted_text' => '<p><strong>My formatted text</strong></p>', |
| 26 | + * ], |
| 27 | + * '#context' => [ |
| 28 | + * 'node' => $node, |
| 29 | + * ], |
| 30 | + * ]; |
| 31 | + * @endcode |
| 32 | + * |
| 33 | + * @RenderElement("pattern") |
| 34 | + */ |
| 35 | +class Pattern extends RenderElement { |
| 36 | + |
| 37 | + /** |
| 38 | + * {@inheritdoc} |
| 39 | + */ |
| 40 | + public function getInfo(): array { |
| 41 | + return [ |
| 42 | + '#pre_render' => [ |
| 43 | + [get_class($this), 'preRenderPatternElement'], |
| 44 | + ], |
| 45 | + '#pattern' => NULL, |
| 46 | + '#config' => [], |
| 47 | + '#context' => [], |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Pattern element pre render callback. |
| 53 | + * |
| 54 | + * @param array $element |
| 55 | + * An associative array containing the properties of the pattern element. |
| 56 | + * |
| 57 | + * @return array |
| 58 | + * The modified element. |
| 59 | + * |
| 60 | + * @throws \Drupal\Component\Plugin\Exception\PluginException |
| 61 | + */ |
| 62 | + public static function preRenderPatternElement(array $element): array { |
| 63 | + /** @var \Drupal\patternkit\Entity\Pattern $pattern */ |
| 64 | + $pattern = $element['#pattern']; |
| 65 | + |
| 66 | + // Fail early if a pattern was unable to be loaded. |
| 67 | + if (is_null($pattern)) { |
| 68 | + $element = [ |
| 69 | + '#markup' => t('Pattern unavailable.'), |
| 70 | + ]; |
| 71 | + return $element; |
| 72 | + } |
| 73 | + |
| 74 | + $pattern->config = $element['#config']; |
| 75 | + $pattern->context = $element['#context']; |
| 76 | + |
| 77 | + $bubbleableMetadata = new BubbleableMetadata(); |
| 78 | + static::preprocessConfigValues($pattern, $pattern->config, $pattern->context, $bubbleableMetadata); |
| 79 | + |
| 80 | + $library_plugin = static::getPatternLibraryPlugin($pattern); |
| 81 | + $elements = $library_plugin->render([$pattern]); |
| 82 | + |
| 83 | + // Apply all bubbleable metadata from preprocessing. |
| 84 | + $bubbleableMetadata->applyTo($elements); |
| 85 | + |
| 86 | + return $elements; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Load the appropriate pattern library plugin for rendering the pattern. |
| 91 | + * |
| 92 | + * @param \Drupal\patternkit\Entity\PatternInterface $pattern |
| 93 | + * The pattern entity being prepared for rendering. |
| 94 | + * |
| 95 | + * @return \Drupal\patternkit\PatternLibraryPluginInterface |
| 96 | + * The pattern library plugin needed by the given pattern. |
| 97 | + */ |
| 98 | + protected static function getPatternLibraryPlugin(PatternInterface $pattern): PatternLibraryPluginInterface { |
| 99 | + /** @var \Drupal\patternkit\PatternLibraryPluginManager $pattern_plugin_manager */ |
| 100 | + $patternLibraryPluginManager = \Drupal::service('plugin.manager.library.pattern'); |
| 101 | + |
| 102 | + $pattern_plugin = $pattern->getLibraryPluginId(); |
| 103 | + $library_plugin_id = !empty($pattern_plugin) ? $pattern_plugin : 'twig'; |
| 104 | + |
| 105 | + return $patternLibraryPluginManager->createInstance($library_plugin_id); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Execute value processing on configuration values. |
| 110 | + * |
| 111 | + * @param \Drupal\patternkit\Entity\PatternInterface $pattern |
| 112 | + * The pattern being prepared and processed. |
| 113 | + * @param array $config |
| 114 | + * Configuration values for the pattern being prepared. |
| 115 | + * @param array $context |
| 116 | + * Context values configured for the pattern being prepared. |
| 117 | + * @param \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata |
| 118 | + * Bubbleable metadata to be tracked and updated during processing. |
| 119 | + * |
| 120 | + * @throws \Drupal\Component\Plugin\Exception\PluginException |
| 121 | + */ |
| 122 | + public static function preprocessConfigValues(PatternInterface $pattern, array &$config, array $context, BubbleableMetadata $bubbleable_metadata): void { |
| 123 | + /** @var \Drupal\patternkit\PatternFieldProcessorPluginManager $manager */ |
| 124 | + $manager = \Drupal::service('plugin.manager.pattern_field_processor'); |
| 125 | + $manager->processSchemaValues($pattern, $config, $context, $bubbleable_metadata); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments