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
935cc085
Commit
935cc085
authored
6 years ago
by
cchoe1
Browse files
Options
Downloads
Patches
Plain Diff
add logger, check if responsive_image style is set
parent
05e8008f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
+105
-27
105 additions, 27 deletions
src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
with
105 additions
and
27 deletions
src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php
+
105
−
27
View file @
935cc085
...
...
@@ -10,6 +10,13 @@ use Drupal\image\Entity\ImageStyle;
use
Drupal\responsive_image
\Plugin\Field\FieldFormatter\ResponsiveImageFormatter
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\bg_img_field
\Component\Render\CSSSnippet
;
use
Drupal\Core\Logger\LoggerChannelTrait
;
use
Drupal\Core\Field\FieldDefinitionInterface
;
use
Drupal\Core\Entity\EntityStorageInterface
;
use
Drupal\Core\Utility\LinkGeneratorInterface
;
use
Drupal\Core\Session\AccountInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Drupal\Core\Url
;
/**
* Plugin implementation of the 'image' formatter.
...
...
@@ -27,6 +34,49 @@ use Drupal\bg_img_field\Component\Render\CSSSnippet;
*/
class
BgImgFieldFormatter
extends
ResponsiveImageFormatter
implements
ContainerFactoryPluginInterface
{
/**
* @var Drupal\Core\Logger\LoggerChannelTrait
*/
use
LoggerChannelTrait
;
public
function
__construct
(
$plugin_id
,
$plugin_definition
,
FieldDefinitionInterface
$field_definition
,
array
$settings
,
$label
,
$view_mode
,
array
$third_party_settings
,
EntityStorageInterface
$responsive_image_style_storage
,
EntityStorageInterface
$image_style_storage
,
LinkGeneratorInterface
$link_generator
,
AccountInterface
$current_user
)
{
parent
::
__construct
(
$plugin_id
,
$plugin_definition
,
$field_definition
,
$settings
,
$label
,
$view_mode
,
$third_party_settings
,
$responsive_image_style_storage
,
$image_style_storage
,
$link_generator
,
$current_user
);
$this
->
logger
=
$this
->
getLogger
(
'bg_img_field'
);
}
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
$container
=
parent
::
create
(
$container
,
$configuration
,
$plugin_id
,
$plugin_definition
);
return
$container
;
}
/**
* {@inheritdoc}
*/
...
...
@@ -179,47 +229,75 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
$css
.
=
"background-position: "
.
$options
[
'css_background_position'
]
.
";"
;
$css
.
=
'}'
;
$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
]))
{
// $responsive_image_style holds the configuration from the responsive_image module for a given responsive style
// We need to check that this exists or else we get a WSOD
if
(
!
$responsive_image_style
)
{
$field_definition
=
$this
->
fieldDefinition
->
getFieldStorageDefinition
();
$multipliers
=
array_reverse
(
$multipliers
);
$query
=
$breakpoints
[
$breakpoint_id
]
->
getMediaQuery
();
if
(
$query
!=
""
)
{
$css
.
=
' @media '
.
$query
.
' {'
;
}
$this
->
logger
->
error
(
'
There is no responsive image style set for the {field_name} field on the {entity_type} entity. Please ensure
that the responsive image style is configured at <a href="{link}">{link}</a>. Then set the correct style on the
formatter for the entity display.
'
,
[
'field_name'
=>
$field_definition
->
get
(
'field_name'
),
'entity_type'
=>
$field_definition
->
get
(
'entity_type'
),
'link'
=>
Url
::
fromRoute
(
'entity.responsive_image_style.collection'
)
->
toString
()
]);
}
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
]))
{
foreach
(
$multipliers
as
$multiplier
=>
$mapping
)
{
$multiplier
=
rtrim
(
$multiplier
,
"x"
);
$multipliers
=
array_reverse
(
$multipliers
);
if
(
$mapping
[
'image_mapping_type'
]
!=
'image_style'
)
{
continue
;
$query
=
$breakpoints
[
$breakpoint_id
]
->
getMediaQuery
();
if
(
$query
!=
""
)
{
$css
.
=
' @media '
.
$query
.
' {'
;
}
if
(
$mapping
[
'image_mapping'
]
==
"_original image_"
)
{
$
url
=
file_create_url
(
$image
->
getFileUri
()
);
}
else
{
$url
=
ImageStyle
::
load
(
$mapping
[
'image_mapping'
])
->
buildUrl
(
$image
->
getFileUri
())
;
}
foreach
(
$multipliers
as
$multiplier
=>
$mapping
)
{
$
multiplier
=
rtrim
(
$multiplier
,
"x"
);
if
(
$mapping
[
'image_mapping_type'
]
!=
'image_style'
)
{
continue
;
}
if
(
$multiplier
!=
1
)
{
$css
.
=
' @media (-webkit-min-device-pixel-ratio: '
.
$multiplier
.
'), (min-resolution: '
.
$multiplier
*
96
.
'dpi), (min-resolution: '
.
$multiplier
.
'dppx) {'
;
if
(
$mapping
[
'image_mapping'
]
==
"_original image_"
)
{
$url
=
file_create_url
(
$image
->
getFileUri
());
}
else
{
$url
=
ImageStyle
::
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
.
=
'}'
;
}
}
$css
.
=
$selector
.
' {background-image: url('
.
$url
.
');}'
;
if
(
$
multipli
er
!=
1
)
{
if
(
$
qu
er
y
!=
""
)
{
$css
.
=
'}'
;
}
}
if
(
$query
!=
""
)
{
$css
.
=
'}'
;
}
}
}
return
$css
;
}
/**
* Implement abstract method
*
* @param $level
* @param $message
* @param array $context
*/
public
function
log
(
$level
,
$message
,
array
$context
=
[])
{
}
}
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