4

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',
),

4 Answers 4

3

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

1
  • thanks wiifm, however I want to do this within my module as it needs to be portable across site themes. I can theme a view field within my module, but just having trouble with the table version.
    – lordg
    Commented Mar 14, 2011 at 9:04
0

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:

0

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.

0

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.

4
  • 1
    Please don't use code ticks for things that are not code.
    – Mołot
    Commented Dec 18, 2013 at 10:46
  • Please don't note formatting issues in comments. It makes it look like I screwed that up, it was a subsequent edit by someone else. Thanks.
    – stockli
    Commented Dec 19, 2013 at 14:18
  • Sorry, but comments are the only way to contact person who edits. There was numerous requests for alternatives at Meta, but so far none is status-completed.
    – Mołot
    Commented Dec 19, 2013 at 14:30
  • Duly noted, I just wanted to make it clear... Since the only comment on this post (which I believe answers the original question) was a neg it felt less than awesome on my first post on the Drupal SE.
    – stockli
    Commented Dec 19, 2013 at 19:43

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.