function classy_paragraphs_entity_view_alter in Classy paragraphs 8
Implements hook_entity_view_alter().
File
- ./
classy_paragraphs.module, line 38 - Contains classy_paragraphs.module.
Code
function classy_paragraphs_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
// Find fields which are referencing classy_paragraphs.
$class_fields = [];
$fields = $entity
->getFieldDefinitions();
foreach ($fields as $field) {
if ($field instanceof FieldConfigInterface) {
/** @var Drupal\field\Entity\FieldStorageConfig $field_storage */
$field_storage = $field
->getFieldStorageDefinition();
$target = $field_storage
->getSetting('target_type');
if ($target == 'classy_paragraphs_style') {
$class_fields[] = $field;
}
}
}
foreach ($class_fields as $class_field) {
$field_name = $class_field
->get('field_name');
if (!$entity
->get($field_name)
->isEmpty()) {
$field = $entity
->get($field_name);
$classes = $field
->referencedEntities();
foreach ($classes as $class) {
$raw_classes = $class
->getClasses();
$array_classes = explode("\r\n", $raw_classes);
if (!empty($array_classes)) {
if (!empty($build['#attributes']['class'])) {
$current_classes = $build['#attributes']['class'];
$build['#attributes']['class'] = array_merge($current_classes, $array_classes);
}
else {
$build['#attributes']['class'] = $array_classes;
}
}
}
}
}
}