Entity view (Content)

Views preprocessors in module files

By rllabore
Oct. 22, 2009

We've all used pre-processors to modify data before it reaches the template file to be printed. These pre-processors are usually defined in template.php with a corresponding template file within the themes folder. The views module is quite helpful because it allows others to use their own themeing files to design their own views. I was working on a custom node module and wanted to implement views hooks, so that I can expose my tables data to views, and to create default views. Exposing the extra data from my table was relatively simple as I simply implemented the hook: hook_views_data. With this hook, I was able to setup my fields, filters, arguments, and sorting. So now, with my exposed data ready to be used, I was ready to implement hook_views_default_views. I initially created the views through the user interface and then pasted the exported code in my hook_views_default_views implementation. After creating the views, I wanted to be able to theme them. In order for me to theme them, I had to override the template files in my themes folder. This is where I thought to myself, what if someone else wanted to use my module? what if it’s not themed as they wanted it to be? This is when I began searching the web for solutions. I stumbled upon the views documentation. This is where I found the ability to be able to create views pre-processors in my module file. Here's a snippet that will allow for you to create templates and pre-processors for views in your module:

/**  * Implementation of hook_theme  */ function MODULE_NAME_theme($existing, $type, $theme, $path) {   $themes['views_view_field__field_blog_blogger_nid_nid'] =     array(     'arguments' => array('view' => NULL, 'field' => NULL, 'row' => NULL),     'template' => 'views-view-field--field-blog-blogger-nid-nid',     'path' => $path .'/templates',     'original hook' => 'views_view_field',     'preprocess functions' => array(       'MODULE_NAME_preprocess_views_view_field__field_blog_blogger_nid_nid',     ),   );   return $themes; }

Post Tags: