From 1223d8d5949ffd61257d27c9e25e345b3ef55904 Mon Sep 17 00:00:00 2001
From: Arthur Lorenz <arthur@utor.at>
Date: Mon, 20 Mar 2023 08:47:36 +0100
Subject: [PATCH 1/2] Add new method to render custom elements.

---
 src/CustomElementsRenderer.php                | 59 ++++++++++++++-----
 src/CustomElementsRendererTrait.php           | 43 ++++++++++++++
 .../CustomElementsViewSubscriber.php          |  4 +-
 3 files changed, 88 insertions(+), 18 deletions(-)
 create mode 100644 src/CustomElementsRendererTrait.php

diff --git a/src/CustomElementsRenderer.php b/src/CustomElementsRenderer.php
index a7a35d8..674ca66 100644
--- a/src/CustomElementsRenderer.php
+++ b/src/CustomElementsRenderer.php
@@ -8,7 +8,6 @@ use Drupal\Core\Access\AccessResult;
 use Drupal\Core\Cache\CacheableJsonResponse;
 use Drupal\Core\Cache\CacheableResponse;
 use Drupal\Core\Controller\TitleResolverInterface;
-use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Extension\ModuleHandlerInterface;
 use Drupal\Core\Messenger\MessengerInterface;
 use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
@@ -158,20 +157,7 @@ class CustomElementsRenderer {
    */
   public function renderResponse(CustomElement $custom_element, $format = 'markup', $select = NULL) {
     $custom_element->addCacheContexts($this->rendererConfig['required_cache_contexts']);
-    $bubbleable_metadata = BubbleableMetadata::createFromObject($custom_element);
-
-    if ($format == static::CONTENT_FORMAT_MARKUP) {
-      $build = $custom_element->toRenderArray();
-      $content = $this->renderer->renderRoot($build);
-      $bubbleable_metadata = $bubbleable_metadata
-        ->merge(BubbleableMetadata::createFromRenderArray($build));
-    }
-    elseif ($format == static::CONTENT_FORMAT_JSON) {
-      $content = $this->getCustomElementNormalizer()->normalize($custom_element, NULL, ['cache_metadata' => $bubbleable_metadata]);
-    }
-    else {
-      throw new \LogicException('Unsupported content format given.');
-    }
+    [$content, $bubbleable_metadata] = $this->renderCustomElement($custom_element, $format);
 
     if ($select == 'content') {
       if ($format == static::CONTENT_FORMAT_JSON) {
@@ -216,6 +202,49 @@ class CustomElementsRenderer {
     return $response;
   }
 
+  /**
+   * Gets content format from request parameter.
+   *
+   * @param \Symfony\Component\HttpFoundation\Request $request
+   *   The request.
+   *
+   * @return string
+   *   The content format.
+   */
+  public function getContentFormatFromRequest(Request $request): string {
+    $content_format_settings = Settings::get('lupus_ce_renderer_default_format', CustomElementsRenderer::CONTENT_FORMAT_MARKUP);
+    $default_content_format = $request->attributes->get('lupus_ce_renderer.content_format', $content_format_settings);
+    return $request->query->get('_content_format', $default_content_format);
+  }
+
+  /**
+   * Renders a custom element.
+   *
+   * @param \Drupal\custom_elements\CustomElement $custom_element
+   *   The custom element to render.
+   * @param string $format
+   *   The content format.
+   *
+   * @return array
+   *   Array with content as first element and bubbleable metadata as second.
+   */
+  public function renderCustomElement(CustomElement $custom_element, string $format): array {
+    $bubbleable_metadata = BubbleableMetadata::createFromObject($custom_element);
+    if ($format == static::CONTENT_FORMAT_MARKUP) {
+      $build = $custom_element->toRenderArray();
+      $content = $this->renderer->renderRoot($build);
+      $bubbleable_metadata = $bubbleable_metadata
+        ->merge(BubbleableMetadata::createFromRenderArray($build));
+    }
+    elseif ($format == static::CONTENT_FORMAT_JSON) {
+      $content = $this->getCustomElementNormalizer()->normalize($custom_element, NULL, ['cache_metadata' => $bubbleable_metadata]);
+    }
+    else {
+      throw new \LogicException('Unsupported content format given.');
+    }
+    return [$content, $bubbleable_metadata];
+  }
+
   /**
    * Get additional dynamic content.
    *
diff --git a/src/CustomElementsRendererTrait.php b/src/CustomElementsRendererTrait.php
new file mode 100644
index 0000000..4d1c76d
--- /dev/null
+++ b/src/CustomElementsRendererTrait.php
@@ -0,0 +1,43 @@
+<?php
+
+namespace Drupal\lupus_ce_renderer;
+
+/**
+ * Allows setter injection and simple usage of the service.
+ */
+trait CustomElementsRendererTrait {
+
+  /**
+   * CE Renderer.
+   *
+   * @var \Drupal\lupus_ce_renderer\CustomElementsRenderer
+   */
+  protected CustomElementsRenderer $ceRenderer;
+
+  /**
+   * Sets the CE renderer.
+   *
+   * @param \Drupal\lupus_ce_renderer\CustomElementsRenderer $ce_renderer
+   *   Custom Element Renderer.
+   *
+   * @return $this
+   */
+  public function setCeRenderer(CustomElementsRenderer $ce_renderer) : self {
+    $this->ceRenderer = $ce_renderer;
+    return $this;
+  }
+
+  /**
+   * Gets the CE renderer.
+   *
+   * @return \Drupal\lupus_ce_renderer\CustomElementsMetatagsGenerator
+   *   Custom elements renderer.
+   */
+  public function getCeRenderer() : CustomElementsRenderer {
+    if (empty($this->ceRenderer)) {
+      $this->ceRenderer = \Drupal::service('lupus_ce_renderer.custom_elements_renderer');
+    }
+    return $this->ceRenderer;
+  }
+
+}
diff --git a/src/EventSubscriber/CustomElementsViewSubscriber.php b/src/EventSubscriber/CustomElementsViewSubscriber.php
index 7a22e91..20a5c87 100644
--- a/src/EventSubscriber/CustomElementsViewSubscriber.php
+++ b/src/EventSubscriber/CustomElementsViewSubscriber.php
@@ -66,9 +66,7 @@ class CustomElementsViewSubscriber implements EventSubscriberInterface {
 
     // Render the controller result into a response if it's a render array.
     if ($result instanceof CustomElement) {
-      $content_format_settings = Settings::get('lupus_ce_renderer_default_format', CustomElementsRenderer::CONTENT_FORMAT_MARKUP);
-      $default_content_format = $request->attributes->get('lupus_ce_renderer.content_format', $content_format_settings);
-      $content_format = $request->query->get('_content_format', $default_content_format);
+      $content_format = $this->customElementRenderer->getContentFormatFromRequest($request);
       $wrapper_format = $request->query->get('_select');
       $response = $this->customElementRenderer->renderResponse($result, $content_format, $wrapper_format);
 
-- 
GitLab


From 6533386237c7abbb31dca9cc1b454d5cd0195ff8 Mon Sep 17 00:00:00 2001
From: Arthur Lorenz <arthur@utor.at>
Date: Mon, 20 Mar 2023 17:51:52 +0100
Subject: [PATCH 2/2] Improve naming and code documentation.

---
 src/CustomElementsRenderer.php      | 7 +++++--
 src/CustomElementsRendererTrait.php | 6 +++---
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/CustomElementsRenderer.php b/src/CustomElementsRenderer.php
index 674ca66..42eef02 100644
--- a/src/CustomElementsRenderer.php
+++ b/src/CustomElementsRenderer.php
@@ -218,15 +218,18 @@ class CustomElementsRenderer {
   }
 
   /**
-   * Renders a custom element.
+   * Renders a custom element into the given content format.
    *
    * @param \Drupal\custom_elements\CustomElement $custom_element
    *   The custom element to render.
    * @param string $format
-   *   The content format.
+   *   The content format, markup or json.
    *
    * @return array
    *   Array with content as first element and bubbleable metadata as second.
+   *
+   * @throws \LogicException
+   *   Thrown when an unsupported format was given.
    */
   public function renderCustomElement(CustomElement $custom_element, string $format): array {
     $bubbleable_metadata = BubbleableMetadata::createFromObject($custom_element);
diff --git a/src/CustomElementsRendererTrait.php b/src/CustomElementsRendererTrait.php
index 4d1c76d..dca7cef 100644
--- a/src/CustomElementsRendererTrait.php
+++ b/src/CustomElementsRendererTrait.php
@@ -22,7 +22,7 @@ trait CustomElementsRendererTrait {
    *
    * @return $this
    */
-  public function setCeRenderer(CustomElementsRenderer $ce_renderer) : self {
+  public function setCustomElementsRenderer(CustomElementsRenderer $ce_renderer) : self {
     $this->ceRenderer = $ce_renderer;
     return $this;
   }
@@ -30,10 +30,10 @@ trait CustomElementsRendererTrait {
   /**
    * Gets the CE renderer.
    *
-   * @return \Drupal\lupus_ce_renderer\CustomElementsMetatagsGenerator
+   * @return \Drupal\lupus_ce_renderer\CustomElementsRenderer
    *   Custom elements renderer.
    */
-  public function getCeRenderer() : CustomElementsRenderer {
+  public function getCustomElementsRenderer() : CustomElementsRenderer {
     if (empty($this->ceRenderer)) {
       $this->ceRenderer = \Drupal::service('lupus_ce_renderer.custom_elements_renderer');
     }
-- 
GitLab

