Assigning existing display template to Drupal view - drupal

How can I assign an existing template file (template file of an another view) to Drupal view.
I already have a template views-view--search-issue.tpl.php for search_issue view. Is there any way to use the same template for another view archive_issue? Or is it necessary that I have to create a new template for that one?

Then you might want to implement some preprocess hook that lets you add some suggestions. Something like this (it's not tested and maybe you'll need a different hook but to get an idea):
function phptemplate_preprocess_views_view (&$vars) {
$view = $vars['view'];
if ($view->name == 'archive_issue') {
$vars['template_files'] = 'views-view--search-issue';
}
}
But like Aniruddhsinh said, the easiest way is to just copy paste the code you need in the appropriate template. Maybe you're feeling that you're violating the DRY manta (Don't Repeat Yourself), but in this case it's better than to break the pattern for views templates. Just go with Aniruddhsinh solution.
Source: Suggested template files for views

Go to your archive_issue view and select the template file name. Create a template file with the same name from archive_issue and copy the content from views-view--search-issue.tpl.php which is for your search_issue.Paste it into this archive's template file. Clear the cache because of template changes and you will get the same template as it is in search_issue.

Related

Specialized template for subpages?

If I have a page named 'events' (of which the specialized template will be named 'page-events.php'), is there any way to have a specialized template of a subpage 'events/event-sub-page'?
I can't seem to find how to do it in the documentation here: codex.wordpress.org/Page_Templates.
You would just create a new template called events-subpage.php and build it accordingly. Then when making your page in wordpress, select the template events-subpage.
You can just create the "event-sub-page" page in WordPress, set its parent to the "events" page, and make your custom template file "page-event-sub-page.php" (no need to specify the "events" part in the actual php filename)
Or in my case I wanted "../login" for a specialized login form and "../login/recovery" for password recovery. The template page is simply "page-recovery.php"

Drupal7: Trying to theme a specific page using a preprocess function, but...I get a blank screen instead

I've just discovered that if you want to alter a specific page (or group of pages) all you need is to add templates file to the core templates. For instance, I need to theme my /helloword page using a page--helloworld.tpl.php and node--helloworld.tpl.php template files.
Now all I get is a blank screen so I tried to write a preprocess function that adds support for custom theme files like:
<?php
/**
* Adding or modifying variables before page render.
*/
function phptemplate_preprocess_page(&$vars) {
// Page change based on node->type
// Add a new page-TYPE template to the list of templates used
if (isset($vars['node'])) {
// Add template naming suggestion. It should alway use doublehyphens in Drupal7.
$vars['template_files'][] = 'page--'. str_replace('_', '-', $vars['node']->type);
}
}
?>
I see no syntax error but I still get a blank screen. Still no luck
Is someone able to figure out what's wrong in the code/routine?
Drupal7 + Omega Sub-Theme
Kind Regards
I think there's a tiny bit of confusion here: a template file named node--type.tpl.php will automatically be called for any node which has the type type...you don't need to add the template suggestions in yourself.
There is one caveat to this, you have to copy the original node.tpl.php to your theme folder and clear your caches otherwise Drupal won't pick it up.
Also you don't want to use the phptemplate_ prefix...rather you want your function to be called MYTHEMENAME_preprocess_page.
Your code to add the page template based on the node type looks spot on, see if you still have the problem after you change your function name and clear the caches.
Hope that helps :)

Get the view name in the page.tpl.php file

How can I get the current view name inside the page.tpl.php file?
Details:
If I want to apply special code inside page.tpl.php for a certain node I can use the node number
if($node->nid == 35){
//do something special for this node id
}
But The pages generated by the views module don't have a nid, They have a view name, How can I get this node name using php, I need to get something like this.
if(//node-name == "view1")
//do something special for this page generated by views module
I use a work around for now, I use current_path() but I need a more reliable solution because the path may change!
Thanks
When you edit the view, there is a part called 'Theme Information', there shows a list of possible template files for theming the view or its components.
I found the answer to my question (Drupal 7)..
In order to get the current view name in the page.tpl use:
$page["#views_contextual_links_info"]["views_ui"]["view_name"]
In order to get the name of the page inside the current view use:
$page["#views_contextual_links_info"]["views_ui"]["view_display_id"]

Rewrite default Drupal view programmatically

Say we have a defaul view (i.e hardcoded), provided by Views module , for example "taxonomy/term/%"
Now, I'd like to make some modification to that view programmatically, through an installation profile
Normally I use Features module for such work, but Features does not support default views.
Please advise how to do that.
Thanks!
Use hook_views_default_views_alter
function MODULE_views_default_views_alter(&$views) {
if (isset($views['taxonomy_term'])) {
$views['taxonomy_term']->set_display('default');
$views['taxonomy_term']->display_handler->set_option('title', 'Categories');
}
}
You should use the Views theme information. There is a link you can use to find out what you should name your views (Its called "Theme Information") copy the name of the particular part of the view you would like to hardcode and paste it as a new file in your template's directory. You can use a folder (I usually name it views) to separate these files from others in the template. You'll need to refresh your cache to see the changes once you've created the new template file(s).
Yes, use hook_views_default_views_alter()
Here's a good example:
enter link description here

How do you modify the fields output in Drupal's RSS Feeds

Trying to modify the RSS feeds created by Views module in Drupal.
Since there are no 'theme_' hooks for the RSS feeds (rightfully as XML is theme-less), I need an alternate way to modify the fields that are output into the RSS, preferably using template.php if possible.
http://api.drupal.org/api/function/format_rss_item/6 looks promising as that is where each row is created, but it doesn't
node_feed() is what collects the nodes, creates the additional fields, and then calls format_rss_item().
Specifically, we need to remove the dc:creator element from the $extra array created in node_feed()
If you go into a specific content type, you can set the RSS display fields as per this post:
http://redfinsolutions.com/redfin-blog/show-hide-fields-views-generated-drupal-rss-feed
I am adding another answer, as I have recently had to do this and managed to do it without modifying data in the theme layer.
You can add a preprocess function to your view. It is a bit of work though.
There are some guidelines here, but they are a bit confusing. Here is my summary of how to do this.
First make sure your modules weight is > views
Secondly copy the template you would like to add a preprocessor to to your modules directory. Rename it to be something in the list of templates in theming information.
Then edit hook theme like this (but change to use the existing view that you need to override.
function mymodule_theme($existing, $type, $theme, $path) {
// Copy the exsisting views theme from the existing array.
$override = $existing['views_view_row_rss'];
// Add my preprocessor to the list of preprocess functions
$override['preprocess functions'][] = 'mymodule_myfunction';
// Point to a template so that this view will be used.
$override['template'] = 'my_more_specific_template_name';
$override['path'] = drupal_get_path('module', 'path');
unset($override['file']);
// Return your theme handler.
return array('my_more_specific_template_name' => $override);
}
You should then be able to write your preprocess code in the function mymodule_myfunction.
In the view, if you click on "style information" this will show you the template files used to create the feed. You can copy the template so that it is overridden for your view and remove dc:creator from the $item_elements array.
This is not particularly nice as you are modifying data in the theme layer, but it will do what you want.
I'd suggest using the Views Node Feed module to do this. It will let you completely write the XML that is output by Views for feeds.

Resources