Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
ui_patterns_settings
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
ui_patterns_settings
Commits
a0afa462
Commit
a0afa462
authored
4 months ago
by
Florent Torregrosa
Committed by
christian.wiedemann
4 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3512245
by grimreaper: Prop source to get background URL
parent
35f5ca48
Branches
Branches containing commit
No related tags found
1 merge request
!41
Issue #3512245 by grimreaper: Prop source to get background URL
Pipeline
#468025
passed
4 months ago
Stage: build
Stage: validate
Stage: test
Changes
2
Pipelines
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
config/schema/ui_patterns_settings.sources.schema.yml
+11
-0
11 additions, 0 deletions
config/schema/ui_patterns_settings.sources.schema.yml
src/Plugin/UiPatterns/Source/MediaImageUrl.php
+219
-0
219 additions, 0 deletions
src/Plugin/UiPatterns/Source/MediaImageUrl.php
with
230 additions
and
0 deletions
config/schema/ui_patterns_settings.sources.schema.yml
+
11
−
0
View file @
a0afa462
...
...
@@ -8,3 +8,14 @@ ui_patterns_source.media:
mapping
:
media_library_selection
:
type
:
string
ui_patterns_source.media_image_url
:
type
:
mapping
label
:
'
Source:
Media
image
URL'
mapping
:
media
:
type
:
string
label
:
'
Media
ID'
image_style
:
type
:
string
label
:
'
Image
style'
This diff is collapsed.
Click to expand it.
src/Plugin/UiPatterns/Source/MediaImageUrl.php
0 → 100644
+
219
−
0
View file @
a0afa462
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\ui_patterns_settings\Plugin\UiPatterns\Source
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\File\FileUrlGeneratorInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\StringTranslation\TranslatableMarkup
;
use
Drupal\file\FileInterface
;
use
Drupal\image\ImageStyleInterface
;
use
Drupal\media\MediaInterface
;
use
Drupal\ui_patterns
\Attribute\Source
;
use
Drupal\ui_patterns
\SourcePluginBase
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Plugin implementation of the source.
*/
#
[
Source
(
id
:
'media_image_url'
,
label
:
new
TranslatableMarkup
(
'Media: image URL'
),
description
:
new
TranslatableMarkup
(
'Get an image URL from a media'
),
prop_types
:
[
'url'
]
)]
class
MediaImageUrl
extends
SourcePluginBase
{
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected
EntityTypeManagerInterface
$entityTypeManager
;
/**
* The file URL generator.
*
* @var \Drupal\Core\File\FileUrlGeneratorInterface
*/
protected
FileUrlGeneratorInterface
$fileUrlGenerator
;
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
):
static
{
$plugin
=
parent
::
create
(
$container
,
$configuration
,
$plugin_id
,
$plugin_definition
);
$plugin
->
entityTypeManager
=
$container
->
get
(
'entity_type.manager'
);
$plugin
->
fileUrlGenerator
=
$container
->
get
(
'file_url_generator'
);
return
$plugin
;
}
/**
* {@inheritdoc}
*/
public
function
defaultSettings
():
array
{
return
[
'media'
=>
NULL
,
'image_style'
=>
''
,
];
}
/**
* {@inheritdoc}
*/
public
function
getPropValue
():
string
{
if
(
!
$this
->
moduleHandler
->
moduleExists
(
'media'
))
{
return
''
;
}
$mediaId
=
$this
->
getMediaId
();
if
(
!
$mediaId
)
{
return
''
;
}
/** @var \Drupal\media\MediaStorage $mediaStorage */
$mediaStorage
=
$this
->
entityTypeManager
->
getStorage
(
'media'
);
$media
=
$mediaStorage
->
load
(
$mediaId
);
if
(
!
(
$media
instanceof
MediaInterface
))
{
return
''
;
}
$fileUri
=
$this
->
getFileUri
(
$media
);
if
(
!
$fileUri
)
{
return
''
;
}
$imageStyle
=
$this
->
getSetting
(
'image_style'
)
??
''
;
$style
=
NULL
;
if
(
$imageStyle
!==
''
)
{
$style
=
$this
->
entityTypeManager
->
getStorage
(
'image_style'
)
->
load
(
$imageStyle
);
}
if
(
$style
instanceof
ImageStyleInterface
)
{
return
$this
->
fileUrlGenerator
->
transformRelative
(
$style
->
buildUrl
(
$fileUri
));
}
else
{
return
$this
->
fileUrlGenerator
->
transformRelative
(
$this
->
fileUrlGenerator
->
generateString
(
$fileUri
));
}
}
/**
* {@inheritdoc}
*/
public
function
settingsForm
(
array
$form
,
FormStateInterface
$form_state
):
array
{
$form
=
parent
::
settingsForm
(
$form
,
$form_state
);
if
(
!
$this
->
moduleHandler
->
moduleExists
(
'media_library_form_element'
))
{
$form
[
'warning'
]
=
[
'#theme'
=>
'status_messages'
,
'#message_list'
=>
[
'error'
=>
[
$this
->
t
(
'You need to enable the Media Library Form Element module to use this source.'
),
],
],
];
return
$form
;
}
$bundles
=
$this
->
getAllowedMediaBundles
();
if
(
empty
(
$bundles
))
{
return
$form
;
}
$form
[
'media'
]
=
[
'#type'
=>
'media_library'
,
'#allowed_bundles'
=>
$bundles
,
'#default_value'
=>
$this
->
getSetting
(
'media'
)
??
NULL
,
];
$this
->
addRequired
(
$form
[
'media'
]);
$imageStyles
=
$this
->
getAllowedImageStyles
();
if
(
empty
(
$imageStyles
))
{
$form
[
'image_style'
]
=
[
'#type'
=>
'value'
,
'#value'
=>
''
,
];
}
else
{
$form
[
'image_style'
]
=
[
'#type'
=>
'select'
,
'#title'
=>
$this
->
t
(
'Image style'
),
'#options'
=>
$imageStyles
,
'#default_value'
=>
$this
->
getSetting
(
'image_style'
)
??
NULL
,
'#empty_value'
=>
''
,
'#empty_option'
=>
$this
->
t
(
'None'
),
];
}
return
$form
;
}
/**
* Get the file URI of the background image.
*
* @param \Drupal\media\MediaInterface $media
* The referenced media.
*
* @return string
* The file URI or empty string of not found.
*/
protected
function
getFileUri
(
MediaInterface
$media
):
string
{
foreach
(
$media
->
referencedEntities
()
as
$file
)
{
if
(
$file
instanceof
FileInterface
)
{
$fileUri
=
$file
->
getFileUri
();
if
(
is_string
(
$fileUri
))
{
return
$fileUri
;
}
}
}
return
''
;
}
/**
* Get the allowed media bundles.
*
* @return array
* The media bundles.
*/
protected
function
getAllowedMediaBundles
():
array
{
$bundles
=
[];
foreach
(
$this
->
entityTypeManager
->
getStorage
(
'media_type'
)
->
loadMultiple
()
as
$mediaType
)
{
$bundles
[]
=
$mediaType
->
id
();
}
return
$bundles
;
}
/**
* Get the allowed image styles.
*
* @return array
* The image styles.
*/
protected
function
getAllowedImageStyles
():
array
{
$imageStyles
=
[];
foreach
(
$this
->
entityTypeManager
->
getStorage
(
'image_style'
)
->
loadMultiple
()
as
$style
)
{
$imageStyles
[
$style
->
id
()]
=
$style
->
label
();
}
return
$imageStyles
;
}
/**
* The referenced media ID.
*
* @return string
* The media ID. Empty string if not found.
*/
protected
function
getMediaId
():
string
{
$mediaId
=
$this
->
getSetting
(
'media'
)
??
''
;
if
(
isset
(
$mediaId
[
'media_library_selection'
])
&&
!
empty
(
$mediaId
[
'media_library_selection'
]))
{
$mediaId
=
$mediaId
[
'media_library_selection'
];
}
elseif
(
!
is_string
(
$mediaId
))
{
$mediaId
=
''
;
}
return
$mediaId
;
}
}
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