Search code examples
drupaldrupal-9

How do I add CSS classes in "Content type > Manage Display >Table of files"?


In Drupal 9, I can create a new field called file in some content type, e.g. in Article.

Then for this new field in Manage Display I can select Format Settings: File Table.

Finally, I can add a new page (Article type) and attach some files. After saving, I have a page with a table.

My question: how do I add a class to this table? I know I can add styles in css stylesheets, but maybe there is a way to add a class directly to the table in a node? I mean node not View. It just so happens that for View I already know how to do it. But I don't know how to do this for Node

Thank you

ok


Solution

  • The hooks are there for that. If you want to update the node renderable array, you can use the preprocess hook :

    /**
      * Implements hook_preprocess_HOOK().
      */
    function my_theme_preprocess_node(&$variables) {
      /** @var \Drupal\node\Entity\Node $node */
      $node = $variables['node'];
    
      if ($node->bundle() == 'article'
        && $variables['view_mode'] == 'full') {
        // $variables['content']['files']['#attributes']['class'][] = 'your-class';
      }
    }
    

    In your case, you want to update the field, it might be cleaner to edit the field directly :

    /**
      * Implements hook_preprocess_field().
      */
    function your_theme_preprocess_field(&$variables, $hook) {
      $element = $variables['element'];
    
    }