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

Adapt the rest of the function to use the list returned by getEntitiesToView().

parent 83b1ff01
No related branches found
Tags 8.x-2.2
1 merge request!8Update BgImgFieldFormatter to use getElementsToView()
......@@ -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.
......
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