entity_class_formatter.module 1.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
<?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']);
          }
        }
      }
    }
  }
}