Skip to content
Snippets Groups Projects
Commit eba0efaa authored by John Franklin's avatar John Franklin
Browse files

Merge branch '3490943-bgimgfieldformatter-wrong-files' into '8.x-2.x'

Update BgImgFieldFormatter to use getElementsToView()

See merge request !8
parents 974b7aa0 33ba830f
No related branches found
No related tags found
No related merge requests found
Pipeline #355720 passed with warnings
......@@ -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;
......@@ -204,43 +203,14 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
public function viewElements(FieldItemListInterface $items, $langcode) {
$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 = [];
......@@ -264,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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment