Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
element_class_formatter
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
element_class_formatter
Commits
ed698f70
Commit
ed698f70
authored
Oct 23, 2020
by
Michael Caldwell
Committed by
rikki_iki
Oct 23, 2020
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3113746
by justcaldwell: Support List (text) fields
parent
21b56fb9
Branches
Branches containing commit
Tags
8.x-1.3
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/Field/FieldFormatter/ListStringListClassFormatter.php
+51
-0
51 additions, 0 deletions
...gin/Field/FieldFormatter/ListStringListClassFormatter.php
with
51 additions
and
0 deletions
src/Plugin/Field/FieldFormatter/ListStringListClassFormatter.php
0 → 100644
+
51
−
0
View file @
ed698f70
<?php
namespace
Drupal\element_class_formatter\Plugin\Field\FieldFormatter
;
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\Core\Field\FormatterBase
;
use
Drupal\Core\Template\Attribute
;
/**
* Formatter for displaying list (text) in an HTML list.
*
* @FieldFormatter(
* id="list_string_list_class",
* label="List (with class)",
* field_types={
* "list_string",
* }
* )
*/
class
ListStringListClassFormatter
extends
FormatterBase
{
use
ElementListClassTrait
;
/**
* {@inheritdoc}
*/
public
function
viewElements
(
FieldItemListInterface
$items
,
$langcode
)
{
$elements
=
[];
$attributes
=
new
Attribute
();
$class
=
$this
->
getSetting
(
'class'
);
if
(
!
empty
(
$class
))
{
$attributes
->
addClass
(
$class
);
}
$field_storage
=
$items
->
getFieldDefinition
()
->
getFieldStorageDefinition
();
$options
=
options_allowed_values
(
$field_storage
);
foreach
(
$items
as
$delta
=>
$item
)
{
$elements
[
$delta
]
=
$options
[
$item
->
getValue
()[
'value'
]];
}
return
[
[
'#theme'
=>
'item_list'
,
'#items'
=>
$elements
,
'#list_type'
=>
$this
->
getSetting
(
'list_type'
),
'#attributes'
=>
$attributes
->
toArray
(),
],
];
}
}
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