From ddb57fd049e23d503b0249c5fdadacac9799767c Mon Sep 17 00:00:00 2001 From: Jan Para Date: Thu, 28 Sep 2017 11:28:23 +0200 Subject: [PATCH] Add initial module implementation. --- README.md | 9 +++ entity_class_formatter.info.yml | 7 +++ entity_class_formatter.module | 59 +++++++++++++++++++ .../FieldFormatter/EntityClassFormatter.php | 32 ++++++++++ 4 files changed, 107 insertions(+) create mode 100644 entity_class_formatter.info.yml create mode 100644 entity_class_formatter.module create mode 100644 src/Plugin/Field/FieldFormatter/EntityClassFormatter.php diff --git a/README.md b/README.md index 283637f..f5e9b88 100644 --- a/README.md +++ b/README.md @@ -1 +1,10 @@ # 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/). diff --git a/entity_class_formatter.info.yml b/entity_class_formatter.info.yml new file mode 100644 index 0000000..897c021 --- /dev/null +++ b/entity_class_formatter.info.yml @@ -0,0 +1,7 @@ +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 diff --git a/entity_class_formatter.module b/entity_class_formatter.module new file mode 100644 index 0000000..81aa4a7 --- /dev/null +++ b/entity_class_formatter.module @@ -0,0 +1,59 @@ +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']); + } + } + } + } + } +} diff --git a/src/Plugin/Field/FieldFormatter/EntityClassFormatter.php b/src/Plugin/Field/FieldFormatter/EntityClassFormatter.php new file mode 100644 index 0000000..c698d4c --- /dev/null +++ b/src/Plugin/Field/FieldFormatter/EntityClassFormatter.php @@ -0,0 +1,32 @@ +