From 33ba830fa90106c835c5330a948126b2f8fcd779 Mon Sep 17 00:00:00 2001
From: John Franklin <john.franklin@bixal.com>
Date: Fri, 8 Nov 2024 00:46:20 -0500
Subject: [PATCH] Adapt the rest of the function to use the list returned by
 getEntitiesToView().

---
 .../FieldFormatter/BgImgFieldFormatter.php    | 49 +++----------------
 1 file changed, 7 insertions(+), 42 deletions(-)

diff --git a/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php b/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
index c4825c9..9d113aa 100644
--- a/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
+++ b/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
@@ -4,7 +4,6 @@ namespace Drupal\bg_img_field\Plugin\Field\FieldFormatter;
 
 use Drupal\bg_img_field\Component\Render\CSSSnippet;
 use Drupal\Core\Cache\Cache;
-use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityStorageInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Field\FieldDefinitionInterface;
@@ -203,50 +202,15 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
    */
   public function viewElements(FieldItemListInterface $items, $langcode) {
     $elements = [];
-    $files = $this->getEntitiesToView($items, $langcode);
-
-    // Early opt-out if the field is empty.
-    if (empty($files)) {
-      return $elements;
-    }
 
+    $files = $this->getEntitiesToView($items, $langcode);
     $entity = $items->getEntity();
-    $file_storage = $this->entityTypeManager->getStorage('file');
-
-    // Load the files to render.
-    $files = [];
-    foreach ($items->getValue() as $item) {
-      $files[] = [
-        'file' => $file_storage->load($item['target_id']),
-        'item' => $item,
-      ];
-    }
+
     // Early opt-out if the field is empty.
     if (empty($files)) {
       return $elements;
     }
 
-    return $this->buildElement($files, $entity);
-  }
-
-  /**
-   * Build the inline css style based on a set of files and a selector.
-   *
-   * @param array $files
-   *   An array of image files.
-   * @param \Drupal\Core\Entity\EntityInterface $entity
-   *   The parent entity the field belongs to. Used for token replacement in the
-   *   selector.
-   *
-   * @return array
-   *   Returns the built image with the prepared css in the html_head of
-   *   render array
-   */
-  protected function buildElement(array $files,
-                                  EntityInterface $entity) {
-    $elements = [];
-    $css = "";
-
     $image_link_setting = $this->getSetting('image_link');
 
     $cache_contexts = [];
@@ -270,13 +234,14 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
 
     // Process the files to get the css markup.
     foreach ($files as $file) {
-      $selector = $file['item']['css_selector'];
+      $item = $file->_referringItem;
+      $selector = $item->css_selector;
       $selector = \Drupal::token()->replace($selector, [$entity->getEntityTypeId() => $entity], ['clear' => TRUE]);
-      $css .= $this->generateBackgroundCss(
-        $file['file'],
+      $css = $this->generateBackgroundCss(
+        $file,
         $responsive_image_style,
         $selector,
-        $file['item']
+        $item->toArray()
       );
 
       // Attach to head on element to create style tag in the html head.
-- 
GitLab