diff --git a/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php b/src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php index c4825c96868a2b93f5a06b43ca5eab96380f5114..9d113aaf34c68c4a15dca53f32e9862412872630 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.