Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
seeds_page
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
seeds_page
Commits
7ec4fa0d
Commit
7ec4fa0d
authored
9 months ago
by
Ahmad Alyasaki
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3443542
: Add Heading field formatter
parent
0426d1f9
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/Field/FieldFormatter/Heading.php
+84
-0
84 additions, 0 deletions
src/Plugin/Field/FieldFormatter/Heading.php
with
84 additions
and
0 deletions
src/Plugin/Field/FieldFormatter/Heading.php
0 → 100755
+
84
−
0
View file @
7ec4fa0d
<?php
namespace
Drupal\seeds_page\Plugin\Field\FieldFormatter
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Field\FieldItemListInterface
;
use
Drupal\Core\Field\FormatterBase
;
/**
* Plugin implementation of the 'heading' formatter.
*
* @FieldFormatter(
* id = "heading",
* label = @Translation("Heading"),
* field_types = {
* "string",
* "textfield"
* }
* )
*/
class
Heading
extends
FormatterBase
{
/**
* {@inheritdoc}
*/
public
function
settingsSummary
()
{
$summary
=
[];
$summary
[]
=
$this
->
t
(
'Displays as a heading.'
);
return
$summary
;
}
/**
*
*/
public
static
function
defaultSettings
()
{
return
[
'tag'
=>
'h2'
,
'class'
=>
''
,
]
+
parent
::
defaultSettings
();
}
/**
*
*/
public
function
settingsForm
(
array
$form
,
FormStateInterface
$form_state
)
{
$element
=
parent
::
settingsForm
(
$form
,
$form_state
);
$element
[
'tag'
]
=
[
'#type'
=>
'select'
,
'#title'
=>
"Tag"
,
'#options'
=>
[
'h1'
=>
'H1'
,
'h2'
=>
'H2'
,
'h3'
=>
'H3'
],
'#default_value'
=>
$this
->
getSetting
(
"tag"
),
];
$element
[
'class'
]
=
[
'#type'
=>
'textfield'
,
'#title'
=>
"Class"
,
'#default_value'
=>
$this
->
getSetting
(
"class"
),
];
return
$element
;
}
/**
* {@inheritdoc}
*/
public
function
viewElements
(
FieldItemListInterface
$items
,
$langcode
)
{
$element
=
[];
foreach
(
$items
as
$delta
=>
$item
)
{
$value
=
$item
->
value
;
$tag
=
$this
->
getSetting
(
"tag"
);
$class
=
$this
->
getSetting
(
"class"
);
$element
[
$delta
]
=
[
'#type'
=>
'html_tag'
,
'#tag'
=>
$tag
,
'#attributes'
=>
[
'class'
=>
$class
,
],
'#value'
=>
$value
,
];
}
return
$element
;
}
}
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