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
974b7aa0
Commit
974b7aa0
authored
4 months ago
by
Nicholas Stees
Committed by
John Franklin
4 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3484950
by nicholass: Error: Call to a member function load() on null BgImgFieldFormatter
parent
5bb20a8a
No related branches found
No related tags found
1 merge request
!5
Fix Background Image Field Formatter Dependencies and Image Loading
Pipeline
#331523
passed with warnings
4 months ago
Stage: build
Stage: validate
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
+85
-55
85 additions, 55 deletions
src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
src/Plugin/Field/FieldType/BgImgItem.php
+1
-1
1 addition, 1 deletion
src/Plugin/Field/FieldType/BgImgItem.php
with
86 additions
and
56 deletions
src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
+
85
−
55
View file @
974b7aa0
...
...
@@ -2,22 +2,23 @@
namespace
Drupal\bg_img_field\Plugin\Field\FieldFormatter
;
use
Drupal\
Core\Entity\EntityInterface
;
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
;
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\Core\File\FileSystemInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\responsive_image
\Plugin\Field\FieldFormatter\ResponsiveImageFormatter
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\bg_img_field
\Component\Render\CSSSnippet
;
use
Drupal\Core\Image\ImageFactory
;
use
Drupal\Core\Logger\LoggerChannelTrait
;
use
Drupal\Core\Field\FieldDefinitionInterface
;
use
Drupal\Core\Entity\EntityStorageInterface
;
use
Drupal\Core\Utility\LinkGeneratorInterface
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\Core\Session\AccountInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Drupal\Core\Url
;
use
Drupal\Core\File\FileSystemInterface
;
use
Drupal\Core\Image\ImageFactory
;
use
Drupal\Core\Utility\LinkGeneratorInterface
;
use
Drupal\responsive_image
\Plugin\Field\FieldFormatter\ResponsiveImageFormatter
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Plugin implementation of the 'image' formatter.
...
...
@@ -55,6 +56,13 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
*/
protected
$imageFactory
;
/**
* The entityTypeManager service.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected
$entityTypeManager
;
/**
* Constructor for the Background Image Formatter.
*
...
...
@@ -85,6 +93,8 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
* The file system.
* @param \Drupal\Core\Image\ImageFactory $imageFactory
* The image factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
*/
public
function
__construct
(
$plugin_id
,
...
...
@@ -98,6 +108,9 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
EntityStorageInterface
$image_style_storage
,
LinkGeneratorInterface
$link_generator
,
AccountInterface
$current_user
,
FileSystemInterface
$fileSystem
,
ImageFactory
$imageFactory
,
EntityTypeManagerInterface
$entityTypeManager
)
{
parent
::
__construct
(
$plugin_id
,
...
...
@@ -114,6 +127,7 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
);
$this
->
logger
=
$this
->
getLogger
(
'bg_img_field'
);
$this
->
entityTypeManager
=
$entityTypeManager
;
$this
->
fileSystem
=
$fileSystem
;
$this
->
imageFactory
=
$imageFactory
;
}
...
...
@@ -122,12 +136,23 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
$container
=
parent
::
create
(
$container
,
$configuration
,
$plugin_id
,
$plugin_definition
);
return
$container
;
}
return
new
static
(
$plugin_id
,
$plugin_definition
,
$configuration
[
'field_definition'
],
$configuration
[
'settings'
],
$configuration
[
'label'
],
$configuration
[
'view_mode'
],
$configuration
[
'third_party_settings'
],
$container
->
get
(
'entity_type.manager'
)
->
getStorage
(
'responsive_image_style'
),
$container
->
get
(
'entity_type.manager'
)
->
getStorage
(
'image_style'
),
$container
->
get
(
'link_generator'
),
$container
->
get
(
'current_user'
),
$container
->
get
(
'file_system'
),
$container
->
get
(
'image.factory'
),
$container
->
get
(
'entity_type.manager'
)
);
}
/**
* {@inheritdoc}
...
...
@@ -180,12 +205,13 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
$elements
=
[];
$entity
=
$items
->
getEntity
();
$file_storage
=
$this
->
entityTypeManager
->
getStorage
(
'file'
);
// Load the files to render.
$files
=
[];
foreach
(
$items
->
getValue
()
as
$item
)
{
$files
[]
=
[
'file'
=>
$
this
->
fileSystem
->
load
(
$item
[
'target_id'
]),
'file'
=>
$
file_storage
->
load
(
$item
[
'target_id'
]),
'item'
=>
$item
,
];
}
...
...
@@ -295,9 +321,13 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
* @return string
* Generated background image CSS.
*/
protected
function
generateBackgroundCss
(
$
imag
e
,
$responsive_image_style
,
$selector
,
array
$options
)
{
protected
function
generateBackgroundCss
(
$
fil
e
,
$responsive_image_style
,
$selector
,
array
$options
)
{
$css
=
""
;
if
(
!
$file
)
{
return
$css
;
}
$css
.
=
$selector
.
'{'
;
$css
.
=
"background-repeat: "
.
$options
[
'css_repeat'
]
.
";"
;
$css
.
=
"background-size: "
.
$options
[
'css_background_size'
]
.
";"
;
...
...
@@ -323,45 +353,45 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
else
{
$breakpoints
=
\Drupal
::
service
(
'breakpoint.manager'
)
->
getBreakpointsByGroup
(
$responsive_image_style
->
getBreakpointGroup
());
foreach
(
array_reverse
(
$responsive_image_style
->
getKeyedImageStyleMappings
())
as
$breakpoint_id
=>
$multipliers
)
{
if
(
isset
(
$breakpoints
[
$breakpoint_id
]))
{
$multipliers
=
array_reverse
(
$multipliers
);
$query
=
$breakpoints
[
$breakpoint_id
]
->
getMediaQuery
();
if
(
$query
!=
""
)
{
$css
.
=
' @media '
.
$query
.
' {'
;
if
(
isset
(
$breakpoints
[
$breakpoint_id
]))
{
$multipliers
=
array_reverse
(
$multipliers
);
$query
=
$breakpoints
[
$breakpoint_id
]
->
getMediaQuery
();
if
(
$query
!=
""
)
{
$css
.
=
' @media '
.
$query
.
' {'
;
}
foreach
(
$multipliers
as
$multiplier
=>
$mapping
)
{
$multiplier
=
rtrim
(
$multiplier
,
"x"
);
if
(
$mapping
[
'image_mapping_type'
]
!=
'image_style'
)
{
continue
;
}
if
(
$mapping
[
'image_mapping'
]
==
"_original image_"
)
{
$url
=
\Drupal
::
service
(
'file_url_generator'
)
->
generateAbsoluteString
(
$file
->
getFileUri
());
}
else
{
// Use image style URL generation instead of ImageFactory->load()
$image_style
=
$this
->
entityTypeManager
->
getStorage
(
'image_style'
)
->
load
(
$mapping
[
'image_mapping'
]);
$url
=
$image_style
->
buildUrl
(
$file
->
getFileUri
());
}
if
(
$multiplier
!=
1
)
{
$css
.
=
' @media (-webkit-min-device-pixel-ratio: '
.
$multiplier
.
'), (min-resolution: '
.
$multiplier
*
96
.
'dpi), (min-resolution: '
.
$multiplier
.
'dppx) {'
;
}
$css
.
=
$selector
.
' {background-image: url('
.
$url
.
');}'
;
if
(
$multiplier
!=
1
)
{
$css
.
=
'}'
;
}
}
if
(
$query
!=
""
)
{
$css
.
=
'}'
;
}
}
foreach
(
$multipliers
as
$multiplier
=>
$mapping
)
{
$multiplier
=
rtrim
(
$multiplier
,
"x"
);
if
(
$mapping
[
'image_mapping_type'
]
!=
'image_style'
)
{
continue
;
}
if
(
$mapping
[
'image_mapping'
]
==
"_original image_"
)
{
$url
=
\Drupal
::
service
(
'file_url_generator'
)
->
generateAbsoluteString
(
$image
->
getFileUri
());
}
else
{
$url
=
$this
->
imageFactory
->
load
(
$mapping
[
'image_mapping'
])
->
buildUrl
(
$image
->
getFileUri
());
}
if
(
$multiplier
!=
1
)
{
$css
.
=
' @media (-webkit-min-device-pixel-ratio: '
.
$multiplier
.
'), (min-resolution: '
.
$multiplier
*
96
.
'dpi), (min-resolution: '
.
$multiplier
.
'dppx) {'
;
}
$css
.
=
$selector
.
' {background-image: url('
.
$url
.
');}'
;
if
(
$multiplier
!=
1
)
{
$css
.
=
'}'
;
}
}
if
(
$query
!=
""
)
{
$css
.
=
'}'
;
}
}
}
}
}
return
$css
;
}
...
...
This diff is collapsed.
Click to expand it.
src/Plugin/Field/FieldType/BgImgItem.php
+
1
−
1
View file @
974b7aa0
...
...
@@ -245,6 +245,6 @@ class BgImgItem extends ImageItem {
// Handle the custom file extensions setting before saving.
$extensions
=
preg_split
(
'/[\s,]+/'
,
$this
->
getSetting
(
'file_extensions'
));
$this
->
setSetting
(
'
file_extensions
'
,
implode
(
' '
,
array_filter
(
$extensions
))
)
;
$this
->
file_extensions
=
implode
(
' '
,
array_filter
(
$extensions
));
}
}
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