Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ui_patterns
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
ui_patterns
Commits
ddf9710a
Commit
ddf9710a
authored
7 months ago
by
Sviatoslav Smovdyr
Committed by
Pierre Dureau
7 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3477419
by smovs, just_like_good_vibes, pdureau: New source plugin for attributes
parent
58bb95db
No related branches found
No related tags found
1 merge request
!232
#3477419 - Added a new source plugin for class attribute.
Pipeline
#309438
passed with warnings
7 months ago
Stage: build
Stage: validate
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Plugin/UiPatterns/PropType/AttributesPropType.php
+1
-1
1 addition, 1 deletion
src/Plugin/UiPatterns/PropType/AttributesPropType.php
src/Plugin/UiPatterns/Source/ClassAttributeWidget.php
+74
-0
74 additions, 0 deletions
src/Plugin/UiPatterns/Source/ClassAttributeWidget.php
with
75 additions
and
1 deletion
src/Plugin/UiPatterns/PropType/AttributesPropType.php
+
1
−
1
View file @
ddf9710a
...
...
@@ -16,7 +16,7 @@ use Drupal\ui_patterns\PropTypePluginBase;
id
:
'attributes'
,
label
:
new
TranslatableMarkup
(
'Attributes'
),
description
:
new
TranslatableMarkup
(
'HTML attributes as a a mapping.'
),
default_source
:
'attribute
s
'
,
default_source
:
'
class_
attribute'
,
schema
:
[
'type'
=>
'object'
,
'patternProperties'
=>
[
...
...
This diff is collapsed.
Click to expand it.
src/Plugin/UiPatterns/Source/ClassAttributeWidget.php
0 → 100644
+
74
−
0
View file @
ddf9710a
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\ui_patterns\Plugin\UiPatterns\Source
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\StringTranslation\TranslatableMarkup
;
use
Drupal\ui_patterns
\Attribute\Source
;
use
Drupal\ui_patterns
\SourcePluginPropValue
;
/**
* Plugin implementation of the source.
*/
#
[
Source
(
id
:
'class_attribute'
,
label
:
new
TranslatableMarkup
(
'Class attribute'
),
description
:
new
TranslatableMarkup
(
'A space-separated list of HTML classes.'
),
prop_types
:
[
'attributes'
]
)]
class
ClassAttributeWidget
extends
SourcePluginPropValue
{
/**
* {@inheritdoc}
*/
public
function
getPropValue
():
mixed
{
// In UI Patterns Settings, we built the Attribute object here. It is not
// possible anymore because SDC will not validate it against the prop
// type schema.
$value
=
parent
::
getPropValue
();
return
is_string
(
$value
)
?
$this
->
convertStringToAttributesMapping
(
$value
)
:
[];
}
/**
* {@inheritdoc}
*/
public
function
settingsForm
(
array
$form
,
FormStateInterface
$form_state
):
array
{
$form
=
parent
::
settingsForm
(
$form
,
$form_state
);
$form
[
'value'
]
=
[
'#type'
=>
'textfield'
,
'#default_value'
=>
$this
->
getSetting
(
'value'
),
];
$form
[
'value'
][
'#pattern'
]
=
$this
->
buildRegexPattern
();
// To allow form errors to be displayed correctly.
$form
[
'value'
][
'#title'
]
=
''
;
$form
[
'value'
][
'#placeholder'
]
=
'foo bar baz'
;
$form
[
'value'
][
'#description'
]
=
$this
->
t
(
"A space-separated list of HTML classes"
);
$this
->
addRequired
(
$form
[
'value'
]);
return
$form
;
}
/**
* Build regular expression pattern.
*/
protected
function
buildRegexPattern
():
string
{
// Each classname cannot start with a hyphen followed by a digit or a digit.
$class_name
=
"(?!(?:
\\
d|[-]
\\
d))[
\\
S]+"
;
// Return the pattern for valid CSS class names.
return
"^
\\
s*("
.
$class_name
.
"(
\\
s+"
.
$class_name
.
")*)?
\\
s*$"
;
}
/**
* Convert a string to an attribute mapping.
*/
protected
function
convertStringToAttributesMapping
(
string
$value
):
array
{
// Set a filtered array or an empty value if the array is empty.
return
[
'class'
=>
array_filter
(
explode
(
' '
,
$value
),
function
(
$raw_value
)
{
return
!
empty
(
$raw_value
);
}),
];
}
}
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