Same template file for multiple view pages
You don't need to use any preprocess function to call the same template file for multiple view pages.
Just edit the view, click on edit view name/description
and add the View tag
Now, create template file views-view--my-custom-view.tpl.php
and mark any view with the same tag, so all of them will use the same template file.
Same template file for multiple view pages and displays
If you have multiple views with the number of displays attached to them, and you need to define one template file for any view or display, you can achieve it with the following approaches:
Non-coding solution
You can create a separate view for each view display, and mark the view with the appropriate tag, for example:
view_custom_pages
View tag: custom-pages
displays: Page1, Page2, Page3 ...
view_custom_blocks
View tag: custom-blocks
displays: Block1, Block2, Block3 ...
Each page from view_custom_pages
will use views-view--custom-pages.tpl.php
Each block from view_custom_blocks
will use views-view--custom-blocks.tpl.php
Coding solution
If the above mentioned solution does not satisfy your needs, then you can use template_preprocess_views_view() to define theme suggestion for any view display
/**
* Implements template_preprocess_views_view().
*/
function YOURTHEME_OR_MODULE_preprocess_views_view(&$vars) {
$view = $vars['view'];
// an array with views and displays which should use only ONE template file.
$shared_themes = array(
'SOME_VIEW_NAME' => array(
array(
'displays' => array(
'page',
),
'theme_suggestion' => 'views_view__custom_pages'
),
array(
'displays' => array(
'block',
'block_1'
),
'theme_suggestion' => 'views_view__custom_blocks'
),
),
'ANOTHER_VIEW_NAME' => array(
array(
'displays' => array(
'page',
),
'theme_suggestion' => 'views_view__another_custom_pages'
),
array(
'displays' => array(
'block',
'block_1',
'block_2'
),
'theme_suggestion' => 'views_view__another_custom_blocks'
),
)
);
// use ONE template file for multiple views and displays.
if (in_array($view->name, array_keys($shared_themes))) {
foreach ($shared_themes[$view->name] as $key => $data) {
if (in_array($view->current_display, $data['displays'])) {
$vars['theme_hook_suggestion'] = $shared_themes[$view->name][$key]['theme_suggestion'];
}
}
}
}
Override or insert variables into the view template
Create common variables for your custom template using template_preprocess_views_view()
function YOURTHEME_OR_MODULE_preprocess_views_view(&$vars) {
$view = $vars['view'];
switch ($view->name) {
case 'SOME_VIEW_NAME' :
// here get some field value from view and create a variable
$vars['some_variable'] = 'some value';
break;
case 'ANOTHER_VIEW_NAME' :
// here get some field value from view and create a variable
// use the same names in order to use it by one template file.
$vars['some_variable'] = 'some value for another view';
break;
}
}
Notice: It is the same hook as mentioned in the coding solution
.