Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
transform_api
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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
transform_api
Commits
7b3ea1a0
Commit
7b3ea1a0
authored
Jan 8, 2024
by
Erik Petra
Committed by
Martin Giessing
Jan 8, 2024
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3412075
by erik_petra: File size for Media transform
parent
8cdb7940
No related branches found
No related tags found
1 merge request
!3
Add Media file URL and size transform
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/Transform/Field/FileUrlSizeTransform.php
+68
-0
68 additions, 0 deletions
src/Plugin/Transform/Field/FileUrlSizeTransform.php
with
68 additions
and
0 deletions
src/Plugin/Transform/Field/FileUrlSizeTransform.php
0 → 100644
+
68
−
0
View file @
7b3ea1a0
<?php
namespace
Drupal\transform_api\Plugin\Transform\Field
;
use
Drupal\Core\Field\FieldDefinitionInterface
;
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\Core\File\FileUrlGenerator
;
use
Drupal\file\Entity\File
;
use
Drupal\transform_api
\FieldTransformBase
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Transform field plugin for file field types as Urls.
*
* @FieldTransform(
* id = "file_url_size",
* label = @Translation("File URL and Size"),
* field_types = {
* "file"
* }
* )
*/
class
FileUrlSizeTransform
extends
FieldTransformBase
{
/**
* File url generator.
*
* @var \Drupal\Core\File\FileUrlGenerator
*/
protected
$fileUrlGenerator
;
public
function
__construct
(
$plugin_id
,
$plugin_definition
,
FieldDefinitionInterface
$field_definition
,
array
$settings
,
$label
,
$transform_mode
,
array
$third_party_settings
,
FileUrlGenerator
$file_url_generator
)
{
parent
::
__construct
(
$plugin_id
,
$plugin_definition
,
$field_definition
,
$settings
,
$label
,
$transform_mode
,
$third_party_settings
);
$this
->
fileUrlGenerator
=
$file_url_generator
;
}
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
return
new
static
(
$plugin_id
,
$plugin_definition
,
$configuration
[
'field_definition'
],
$configuration
[
'settings'
],
$configuration
[
'label'
],
$configuration
[
'transform_mode'
],
$configuration
[
'third_party_settings'
],
$container
->
get
(
'file_url_generator'
));
}
/**
* {@inheritdoc}
*/
public
function
transformElements
(
FieldItemListInterface
$items
,
$langcode
)
{
$values
=
[];
/** @var \Drupal\Core\Field\FieldItemInterface $item */
foreach
(
$items
as
$item
)
{
if
(
!
empty
(
$item
->
getValue
()[
'target_id'
]))
{
// File ID.
$fid
=
$item
->
getValue
()[
'target_id'
];
// Load file.
$file
=
File
::
load
(
$fid
);
$file_uri
=
$file
->
getFileUri
();
$values
[]
=
[
'url'
=>
$this
->
fileUrlGenerator
->
generateString
(
$file_uri
),
'filesize'
=>
$file
->
getSize
(),
];
}
else
{
$values
[]
=
NULL
;
}
}
return
$values
;
}
}
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