Question

I'm trying to override a view table from within my module. I can't locate what the arguments are suppose to be and in what order (for my hook_theme func). I copied the theme file from views/themes and did no modifications. Does anyone know what is going wrong, and what should the arguments value be below?

My theme configuration is currently:

'views_view_table__opportunities_mentions' => array(
  'arguments' => array(
    'view' => NULL, 
    'title' => NULL, 
    'header' => NULL, 
    'fields' => null, 
    'class' => null, 
    'row_classes' => null, 
    'rows' => null
  ),
  'template' => 'views-view-table--opportunities-mentions',
  'original hook' => 'views_view_table',
  'path' => drupal_get_path('module', 'smd') . '/theme',
),
Was it helpful?

Solution

The easiest way to theme views is to edit the particular view in question and scroll down to find the link 'theme information'. This screen will tell you exactly what views theming templates that it is currently using and what templates you can make in your theme to override this output.

That essentially all views theming is - overriding the default markup with something to suit you designs.

@see http://www.group42.ca/theming_views_2_the_basics for an excellent tutorial on views theming

EDIT

If you want full control over the markup produced, and for this to be portable across themes, the only option you have is to create a custom module. This custom module could also have themeable components, and could even use a view to perform any heavy SQL (or you could just hand-write the SQL)

Take a look at a similiar module to get you started, and have a read through hook_theme

OTHER TIPS

The theme function you want to override, is a templated theme function. This means that a preprocess function is called, and them the variables are passed to the template. Preprocess functions are a bit different, in that only a single variable is passed to it: an array containing all variables, so the order of the variables are irrelevant.

See:

It's way better to preprocess the view.

If you want to override just a particular display, you need to be specific. You must first create a tpl.php file for the view. You can figure out which one you want by looking at the theme information for your particular view. Here is an example:

enter image description here You then want to find the template suggestions of row style, which is currently 'Table':

enter image description here

views-view-table.tpl.php will override every view that has a table style. If you want to be specific to just this view, you're going to want (in this case) views-view-table--frontpage.tpl.php - replace 'frontpage' with whatever suggestions your view is giving you.

You actually need to create this file in your theme directory. But what do you put in this file? Well, just click that 'Style output' link, and you will be presented with code that you can simply copy and paste right into that file.

After saving this file, open up your template.php and create a preprocessor for it. Note that preprocessors do not work without the file being present in D6. Here is some stub code, keeping with our example:

function [theme-name]_preprocess_views_view_table__frontpage(&$vars) {
  // manipulate the $vars here
  ...
}

$vars is passed by reference so you only have to manipulate the appropriate keys to do what you want.

It actually looks mostly correct to me, I used the same code in my module. I wanted to package my template file with my view in the module. As the site uses the regular bartik theme for admin and I didn't want to edit that theme to add my CSS.

What I believe is wrong is the following:

'views_view_table__opportunities_mentions' => array(
  'arguments' => array(
  'view' => NULL, 
  'title' => NULL, 
  'header' => NULL, 
  'fields' => null, 
  'class' => null, 
  'row_classes' => null, 
  'rows' => null
),
'template' => 'views-view-table--opportunities-mentions',
'base hook' => 'views_view_table',
'path' => drupal_get_path('module', 'smd') . '/theme',
),

Note that instead of original hook, it should be base hook. This is required that view has own preprocess functions to be correctly wired to your tpl. If you don't set that correctly it will pick up your custom tpl anyway, but you'll get all kinds of errors about missing or null variables in the tpl because the preprocess function didn't set any of the variables that the tpl uses.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top