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']); } } } } } }