2

As I understand, I need a module like this: https://www.drupal.org/project/field_formatter_css_class but for Drupal 8

For example, I want to highlight one node in the views using CSS-class, how to do this?

PS

Finally I got necessary result:

/**
 * Preprocess function for views-view-grid.tpl.php.
 */
function themename_theme_preprocess_views_view_grid(&$vars) {
   foreach ($vars['items'] as $row) {
        foreach ($row['content'] as $item) {
             $subclass=$item['content']['#row']->_entity->field_fieldname;
             if(!empty($subclass[0])){
                $item['attributes']['class'][] = 'col-'. $subclass[0]->value;
             }
        }
   }
}
2
/**
 * Implements hook_preprocess_html().
 */
function themename_preprocess_html(&$variables) {
$node = load_your_node// Load you node here and add proper condition and type for following code
    $variables['attributes']['class'][] = 'page-node-' . $node->custom_textfield();
}
  • It looks promising, but I have no idea where I can insert this code – Crantisz Apr 12 '16 at 20:50
  • Hi @Crantisz You can use above code in template.php of you theme. – visabhishek Apr 13 '16 at 5:27
  • As I understand, this code adds class to the <body> tag, but I don't need this -- i want to highlight one node in the list of nodes in "views" module – Crantisz Apr 13 '16 at 18:29
  • 1
    Finally I got necessary result /** * Preprocess function for views-view-grid.tpl.php. */ function adminimal_theme_preprocess_views_view_grid(&$vars) { foreach ($vars['items'] as $row) { foreach ($row['content'] as $item) { $subclass=$item['content']['#row']->_entity->field_subclass; if(!empty($subclass[0])){ $item['attributes']['class'][] = 'col-'. $subclass[0]->value; } } } } – Crantisz Apr 13 '16 at 20:38
0

I had to add '#' before 'attributes'.

This is an exemple in a Listbuilder class for those which are doing this.

class SubscribeMeMailListBuilder extends EntityListBuilder {

  /*
   * {@inheritdoc}
   */
  public function render() {
    $render = parent::render();
    $render['table']['#attributes']['class'][] = 'subscribeme_table';
    return $render;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.