Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tiny_slider
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
tiny_slider
Commits
3d8ffbe8
Commit
3d8ffbe8
authored
1 year ago
by
Geoffrey Roberts
Committed by
Nicolas Borda
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3375397
by geoffreyr, ipwa: Field formatter should support Media fields
parent
6e8c1bc9
No related branches found
No related tags found
1 merge request
!11
3375397: Add support for media entities
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/Field/FieldFormatter/TinySliderFieldFormatter.php
+71
-9
71 additions, 9 deletions
src/Plugin/Field/FieldFormatter/TinySliderFieldFormatter.php
with
71 additions
and
9 deletions
src/Plugin/Field/FieldFormatter/TinySliderFieldFormatter.php
+
71
−
9
View file @
3d8ffbe8
...
...
@@ -5,6 +5,7 @@ namespace Drupal\tiny_slider\Plugin\Field\FieldFormatter;
use
Drupal\Component\Utility\Html
;
use
Drupal\Component\Utility\Random
;
use
Drupal\Core\Cache\Cache
;
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\Core\Entity\EntityStorageInterface
;
use
Drupal\Core\Field\FieldDefinitionInterface
;
use
Drupal\Core\Field\FieldItemInterface
;
...
...
@@ -25,7 +26,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* id = "tiny_slider_field_formatter",
* label = @Translation("Tiny Slider Carousel"),
* field_types = {
* "image"
* "image",
* "entity_reference"
* }
* )
*/
...
...
@@ -493,21 +495,17 @@ class TinySliderFieldFormatter extends EntityReferenceFormatterBase implements C
foreach
(
$files
as
$delta
=>
$file
)
{
if
(
isset
(
$link_file
))
{
$image_uri
=
$
file
->
get
FileUri
(
);
$image_uri
=
$
this
->
get
EntityFileUrl
(
$file
);
$url
=
\Drupal
::
service
(
'file_url_generator'
)
->
generate
(
$image_uri
);
}
$cache_tags
=
Cache
::
mergeTags
(
$cache_tags
,
$file
->
getCacheTags
());
// Extract field item attributes for the theme function, and unset them
// from the $item so that the field template does not re-render them.
$item
=
$file
->
_referringItem
;
$item_attributes
=
$item
->
_attributes
;
unset
(
$item
->
_attributes
);
$item_info
=
$this
->
getEntityItem
(
$file
);
$elements
[
$delta
]
=
[
'#theme'
=>
'image_formatter'
,
'#item'
=>
$item
,
'#item_attributes'
=>
$item_attributes
,
'#item'
=>
$item
_info
[
'item'
]
,
'#item_attributes'
=>
$item_
info
[
'
attributes
'
]
,
'#image_style'
=>
$image_style_setting
,
'#url'
=>
$url
,
'#cache'
=>
[
...
...
@@ -533,6 +531,70 @@ class TinySliderFieldFormatter extends EntityReferenceFormatterBase implements C
}
/**
* Get the FieldItem
*
* @param EntityInterface $entity
* The file or media entity.
*
* @return mixed[]
* 'item' is a \Drupal\Core\Field\FieldItemInterface.
* 'attributes' is an array of attributes.
*/
protected
function
getEntityItem
(
EntityInterface
$entity
)
{
// Extract field item attributes for the theme function, and unset them
// from the $item so that the field template does not re-render them.
$item
=
$entity
->
_referringItem
;
$item_attributes
=
$item
->
_attributes
;
unset
(
$item
->
_attributes
);
if
(
$entity
->
getEntityTypeId
()
===
'media'
)
{
$file_id
=
$entity
->
getSource
()
->
getSourceFieldValue
(
$entity
);
$file
=
\Drupal
::
entityTypeManager
()
->
getStorage
(
'file'
)
->
load
(
$file_id
);
// @see BlazyFormatterBase::getEntitiesToView().
$source_field
=
$entity
->
getSource
()
->
getConfiguration
()[
'source_field'
];
$file_meta
=
$entity
->
get
(
$source_field
)
->
getValue
()[
0
]
??
[];
$item
=
(
object
)
[
'target_id'
=>
$file
->
id
(),
'alt'
=>
$file_meta
[
'alt'
]
??
''
,
'title'
=>
$file_meta
[
'title'
]
??
''
,
'width'
=>
intval
(
$file_meta
[
'width'
]
??
'0'
),
'height'
=>
intval
(
$file_meta
[
'height'
]
??
'0'
),
'entity'
=>
$file
,
'_loaded'
=>
TRUE
,
'_is_default'
=>
TRUE
,
];
}
return
[
'item'
=>
$item
,
'attributes'
=>
$item_attributes
,
];
}
/**
* Fetch the URL of the file attached to this entity.
*
* Works with file or media entities.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* @return \Drupal\Core\Url|null
* The file URL if available.
*/
protected
function
getEntityFileUrl
(
EntityInterface
$entity
):
?Url
{
if
(
$entity
->
getEntityTypeId
()
===
'file'
)
{
return
$entity
->
getFileUri
();
}
if
(
$entity
->
getEntityTypeId
()
===
'media'
)
{
$file_id
=
$entity
->
getSource
()
->
getSourceFieldValue
(
$entity
);
$file
=
\Drupal
::
entityTypeManager
()
->
getStorage
(
'file'
)
->
load
(
$file_id
);
return
$file
->
getFileUri
();
}
return
NULL
;
}
/**
* Generate the output appropriate for one field item.
*
...
...
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