Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
E
entity_class_formatter
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
drupal.org
entity_class_formatter
Commits
ddb57fd0
Commit
ddb57fd0
authored
Sep 28, 2017
by
Jan Para
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add initial module implementation.
parent
707bcb38
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
107 additions
and
0 deletions
+107
-0
README.md
README.md
+9
-0
entity_class_formatter.info.yml
entity_class_formatter.info.yml
+7
-0
entity_class_formatter.module
entity_class_formatter.module
+59
-0
src/Plugin/Field/FieldFormatter/EntityClassFormatter.php
src/Plugin/Field/FieldFormatter/EntityClassFormatter.php
+32
-0
No files found.
README.md
View file @
ddb57fd0
# Entity Class Formatter
## Overview
Apply class on the entity if it's field is using this formatter.
## Maintainers
This module is maintained by developers at Morpht. For more information on
the company and our offerings, see
[
morpht.com
](
https://morpht.com/
)
.
entity_class_formatter.info.yml
0 → 100644
View file @
ddb57fd0
name
:
Entity Class Formatter
type
:
module
description
:
Apply class on the entity if it's field is using this formatter.
core
:
8.x
package
:
Field types
dependencies
:
-
drupal:field
entity_class_formatter.module
0 → 100644
View file @
ddb57fd0
<?php
/**
* @file
* Entity Class Formatter module.
*/
use
Drupal\Component\Utility\Html
;
use
Drupal\Core\Entity\Display\EntityViewDisplayInterface
;
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\Core\Entity\FieldableEntityInterface
;
use
Drupal\Core\Field\EntityReferenceFieldItemListInterface
;
/**
* Implements hook_entity_view_alter().
*/
function
entity_class_formatter_entity_view_alter
(
array
&
$build
,
EntityInterface
$entity
,
EntityViewDisplayInterface
$display
)
{
// Skip entities which are not holding fields.
if
(
!
(
$entity
instanceof
FieldableEntityInterface
))
{
return
;
}
// Process all fields.
foreach
(
$display
->
getComponents
()
as
$name
=>
$component
)
{
// Only if entity_class_formatter is used.
if
(
isset
(
$component
[
'type'
])
&&
$component
[
'type'
]
===
'entity_class_formatter'
)
{
// Get field object.
$field
=
$entity
->
get
(
$name
);
// Only for entity reference field type.
if
(
$field
instanceof
EntityReferenceFieldItemListInterface
)
{
// Process all entities referenced by current field.
foreach
(
$field
->
referencedEntities
()
as
$entity
)
{
$title
=
$entity
->
label
();
// Fill title as a class into the entity if is not empty.
if
(
!
empty
(
$title
))
{
$build
[
'#attributes'
][
'class'
][]
=
Html
::
getClass
(
$title
);
}
}
}
// Process simple fields.
else
{
// Process all values of current field.
foreach
(
$entity
->
get
(
$name
)
->
getValue
()
as
$value
)
{
// Fill value as a class into the entity if is not empty.
if
(
!
empty
(
$value
[
'value'
]))
{
$build
[
'#attributes'
][
'class'
][]
=
Html
::
getClass
(
$value
[
'value'
]);
}
}
}
}
}
}
src/Plugin/Field/FieldFormatter/EntityClassFormatter.php
0 → 100644
View file @
ddb57fd0
<?php
namespace
Drupal\entity_class_formatter\Plugin\Field\FieldFormatter
;
use
Drupal\Core\Field\FormatterBase
;
use
Drupal\Core\Field\FieldItemListInterface
;
/**
* Plugin implementation of the 'entity_class_formatter'.
*
* @FieldFormatter(
* id = "entity_class_formatter",
* label = @Translation("Entity Class"),
* field_types = {
* "string",
* "list_string",
* "entity_reference"
* }
* )
*/
class
EntityClassFormatter
extends
FormatterBase
{
/**
* {@inheritdoc}
*/
public
function
viewElements
(
FieldItemListInterface
$items
,
$langcode
)
{
// Instead of outputting the value on the page
// we are inserting it as a class into the markup.
return
[];
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment