Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bg_img_field
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
bg_img_field
Commits
33ba830f
Commit
33ba830f
authored
4 months ago
by
John Franklin
Browse files
Options
Downloads
Patches
Plain Diff
Adapt the rest of the function to use the list returned by getEntitiesToView().
parent
83b1ff01
No related branches found
Branches containing commit
Tags
8.x-2.2
Tags containing commit
1 merge request
!8
Update BgImgFieldFormatter to use getElementsToView()
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
+7
-42
7 additions, 42 deletions
src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
with
7 additions
and
42 deletions
src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
+
7
−
42
View file @
33ba830f
...
...
@@ -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.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment