1

I am looking for a D8 solution to theme the table view mode. To start, I would like to remove the main title (the "Legend" tag of the html fieldset).

Note: I know how to use hook_inline_entity_form_table_fields_alter to add/remove columns, I would like to theme what is outside the table.

After the remove of the title, I would like to remove the headers of the table ("Title", "Status", "Operations"...)

In fact, if someone have a solution, I would like to get rid of this table view mode.. My final goal is to have a very simple view mode showing only one field and the 2 action button..

Here is the solution I found to remove the header and the title: this is to preprocess fieldset and table hooks

/**
 * Implements hook_preprocess_fieldset().
 */
function my_module_preprocess_fieldset(&$vars) {
  //Remove the fieldset title/legend for field_myfield
  //as it is a multiple value field, I check only the
  //first part of the id to target field_myfield
  if (strpos($vars['attributes']['id'],'dit-field-myfield')>0){
    unset($vars['legend']);
    unset($vars['legend_span']); 
    $vars['attributes']['style'][]='padding:0';
  }
}

/**
 * Implements hook_preprocess_table().
 */
function my_module_preprocess_table(&$vars) {
  //In my use case, I need to target only table that are 
  //generated by ief module. I use the first part of the
  //id to target it
  if (strpos($vars['attributes']['id'], 'ef-entity-table')>0 ) {
    //remove column headers
    unset($vars['header']);
    //adpat layout;
    $vars['attributes']['style'][]='margin:0;border-collapse:separate';
  }
}

1 Answer 1

0

Most simple solution should be to override the table.html.twig template. You can simplify it to your needs in your theme's template directory. Use the core/themes/classy/templates/dataset/table.html.twig file as source to start your theming.

If you don't want to "re-theme" all tables, add "suggestions" for other templates to your tables and create template files accordingly.

3
  • Thank you, I am trying to add a suggestion with mytheme_theme_suggestions_table_alter(array &$suggestions, array $variables) but it is not called... do you have another idea?
    – Baud
    Commented Jul 11, 2016 at 9:20
  • No. If the API doesn't work as documented it's never a good idea to search for a workaround. Try to find out why your hook is not called. I bet your cache was not cleared! Commented Jul 11, 2016 at 9:36
  • mytheme_theme_suggestions_table_alter is not called but mymodule_theme_suggestions_table_alter is called... Anyway I found another way to solve my problem. I will set your answer as answered and I will add my own solution. Thanks for your help
    – Baud
    Commented Jul 11, 2016 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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