id() == "programmes"){ $rows = $variables['rows']; foreach ($rows as $id => $row) { $variables['rows'][$id]['attributes'] = new Attribute(); $c = $variables['rows'][$id]['content']["#row"]->nid; $variables['rows'][$id]['attributes']->addClass("programme-".$c); } } }; /* implements template_preprocess_region() */ function popsu_preprocess_region(&$variables) { if (isset($variables['region'])) { $variables['attributes']['class'][] = $variables['region'].'_container'; } } /** * Implements hook_preprocess_page(). */ function popsu_preprocess_views_view(&$variables){ $vars = $variables; if(isset($variables['css_class']) && $variables['css_class'] == 'last-news'){ $rows =& $variables['rows']; foreach ($rows as $i => &$row ){ foreach ($row['#rows'] as $j => &$elem) { $entity = $elem["#row"]->_entity; if ($entity->hasField('field_programme')) { $prog_id = $entity->get('field_programme')->getString(); $class = "programme-".$prog_id; $attributes = array( "class" => $class ); } // $elem['id'] = new Attribute("socks"); //$elem['attributes']->addClass($class); } } } } function popsu_preprocess_views_view_grid(&$variables){ // we pass all variables by reference // https://www.php.net/manual/en/language.references.php $items =& $variables['items']; // loop through grid items (row and cols) not through views\rows foreach ($items as $r => &$row) { foreach ($row['content'] as $c => &$col) { // get the entity $entity = $col['content']["#row"]->_entity; // build generique classe $classes = array( $entity->getEntityTypeId(), $entity->getEntityTypeId().'-'.$entity->bundle(), $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id(), ); // if field_programme exists, add a class if ($entity->hasField('field_programme')) { $prog_id = $entity->get('field_programme')->getString(); $classes[] = "programme-".$prog_id; } // add the classes to the column // Drupal\Core\Template\Attribute // https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Template%21Attribute.php/class/Attribute/8.2.x $col['attributes']->addClass($classes); } } } function popsu_preprocess_views_view_field(&$variables){ // check if link to entoty option is true $settings = isset($variables['field']->options['settings']) ? $variables['field']->options['settings'] : false; if ($settings && isset($settings['link_to_entity']) && $settings['link_to_entity']) { // get the entity and build the classes $entity = $variables['row']->_entity; // build new classes $new_classes = array( $entity->getEntityTypeId(), $entity->getEntityTypeId().'-'.$entity->bundle(), $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id(), ); // get the entity link and url $link = $entity->toLink(); $url = $entity->toLink()->getUrl(); // set the active_class option to the url $url->setOption('set_active_class', true); // add new classes to the url (without overwriting existing once) $options = $url->getOptions(); if(isset($options['attributes']) && $attributes = $options['attributes']){ if(isset($attributes['class']) && $classes = $attributes['class']){ $attributes['class'] += $new_classes; }else{ $attributes['class'] = $new_classes; } }else{ $attributes = array( "class" => $new_classes ); } $url->setOption('attributes', $attributes); // update the url of the link $link->setUrl($url); // overwrite the output $variables['output'] = $link->toRenderable(); } }