How to quickly theme a view? - drupal

I've defined a view with the CCK and View 2 modules. I would like to quickly define a template specific to this view. Is there any tutorial or information on this? What are the files I need to modify?
Here are my findings: (Edited)
In fact, there are two ways to theme a view: the "field" way and the "node" way. In "edit View", you can choose "Row style: Node", or "Row style: Fields".
with the "Node" way, you can create a node-contentname.tpl.php which will be called for each node in the view. You'll have access to your cck field values with $field_name[0]['value']. (edit2) You can use node-view-viewname.tpl.php which will be only called for each node displayed from this view.
with the "Field" way, you add a views-view-field--viewname--field-name-value.tpl.php for each field you want to theme individually.
Thanks to previous responses, I've used the following tools :
In the 'Basic Settings' block, the 'Theme: Information' to see all the different templates you can modify.
The Devel module's "Theme developer" to quickly find the field variable names.
View 2 documentation, especially the "Using Theme" page.

In fact there are two ways to theme a view : the "field" way and the "node" way. In "edit View", you can choose "Row style: Node", or "Row style: Fields".
with the "Node" way, you can create a node-contentname.tpl.php wich will be called for each node in the view. You'll have access to your cck field values with $field_name[0]['value']
with the "Field" way, you add a views-view-field--viewname--field-name-value.tpl.php for each field you want to theme individually.
Thanks to previous responses, I've used the following tools :
In the 'Basic Settings' block, the 'Theme: Information' to see all the different templates you can modify.
The Devel module's "Theme developer" to quickly find the field variable names.
View 2 documentation, especially the "Using Theme" page.

A quick way to find the template files you can create and modify for a view in Views 2.0 is to:
Edit the view
Select the style (e.g. page, block, default)
In the 'Basic Settings' block click on 'Theme: Information' to see all the different templates you can modify.

The Devel module's "Theme developer" feature is handy for seeing what template files Drupal is looking for when it goes to theme something. See the screenshot on that page for an example.

You should also check out Semantic Views. For simple Views theming, it is really handy.

One tip:
You'll likely have a number of views which require similar formatting. Creating templates for each of these views and copying them creates a nightmare of code branching - if you're asked to change the whole look and feel of the site (implying changing the display of each of these views formatted in this particular way), you have to go back and edit each of these separately.
Instead of using the views interface to select new templates for views, I sometimes simply insert some code branching into a single views file. E.g. for one site in views-view-fields.tpl.php I have:
if($view->name == 'articleList' || $view->name == 'frontList'
|| $view->name == 'archiveList') {
/* field formatting code */
} else {
/* the default code running here */
}
This then modifies the fields in the way I want only for this family of Views = articleList, frontList and archiveList - and for other views using this template runs the code one normally finds in this template. If the client asks, "Hey, could you make those pages showing the archives & that list on the front page to look more like ( ... )", it's simply a matter of my opening & editing this one file, instead of three different files. Maintenance becomes much more quick & friendly.

for me block-views-myViewName-myBlockId.tpl.php works

My shortcut option.
Go to theme.inc file in YOUR_MODULE_DIR/views/theme/ folder.
In the _views_theme_functions function print the $themes variable or put a breakpoint on the last line of the function to see the content of the variable.
Just convert views_view to views-view and __ to -- and add your template extension to get desired file name.
For example if an element of the $themes array is views_view__test_view__block (where test_view is the name of your view) then the name of the template file would be views-view--test_view--block.tpl.php.

In my opinion the simplest way to decide which template file to use for theming the views is :
1) Click on admin/build/views/edit/ViewName -> Basic Settings -> Theme
Clicking this would list all the possible template files. Highlighted (File names in Bold) files indicate which template file is being used to do theme what part of the view. After incorporating the required changes in the relevant view template file RESCAN .. now you should be able to see the changed template file highlighted .

If you want to do quick Drupal development with a lot of drag-and-drop, the Display Suite module def. is a something you should use: http://drupal.org/project/ds

According to me there are two ways to do it:
Programatic Way:
Go to edit view.
Select page/block style.
Go to 'Basic Settings' and click on 'Theme: Information' to see all the different templates you can modify.
Add the html you want to theme and print the variables of the view wherever needed
Configuration Update: The Display suite provides us an option to place your labels inline or above and add even to hide them. Custom classes to each of the view's elements can be added too.
Advanced options include:
Exportables
Add your own custom fields in the backend or in your code
Add custom layouts in your theme (D7 only)
Change labels, add styles or override field settings (semantic fields).
Full integration with Views and Panels
Extend the power of your layouts by installing Field Group
Optimal performance with Object cache (D6) or Entity cache (D7) integration

Related

Placing block inside a node (positioning block between specific elements in node's content)

Basically I created a webform and enabled it as a block, now I want to put that block inside a specific node. I can do that by placing it in a 'content' region and defining the specific node BUT it displays at the end of the content. Now how can I move it between specific elements inside the content?
The node is using a page-type....tpl.php which is used by 5 other nodes as well so I cannot change the code.
To visualize it looks like:
[ content ]
-description text-
-list of videos-
[ end of content ]
and I need to put my webform between the text and the video list. Is there a way?
There are many roads you could take, but since you said you're considering the template file: Why not use a node-specific template, since page is a node type?
Say you're on node/123, then you could use a template named node--123.tpl.php
(see Drupal 7 Template (Theme Hook) Suggestions) and embed your block right there.
Alternatively, you could provide a reusable token in a custom module via hook_token_info and combine it with the commonly used token_filter module. But that might be over the top, if it's just one node you need to touch.
For Drupal 7 a bit of a hacky way to display the contents of a block in content would be to enable the PHP Filter module. Then edit your node and switch to the PHP code Text format and add this code
<?php
$block = module_invoke('block', 'block_view', '1');
print render($block['content']);
?>
where '1' is the block id found in the URL when you edit the block and be sure to include the PHP tags.
Also see this page https://www.drupal.org/node/26502 for more information on placing blocks.
You can use the EVA module to add the webforms in the node as a field.
You basically create a view and choose the "eva field" option then you make sure that this view selects only the webforms you want to have and relates it to the node (the EVA module documentation has much better examples than I can provide).
After you have added it as a field you can place it anywhere in the node.
There is also the Block reference module that could help you.

How to customise HTML structure in Drupal view block

I am currently developing a website in Drupal. I have a load of news articles which are of content type 'news'. I want to create a widget to show at the bottom of several pages listing the latest articles. I have managed to achieve thus far by creating the view with the fields I want and then making the view available as a block.
The problem is the format in which the articles are being listed in the view. Ideally I would like to customise the HTML. I understand that I can create a custom template to target views/blocks but I haven't quite got a full understanding of it yet, and find the Drupal documentation to be quite dry and therefore hard to find what I need from.
Any pointers would be helpful.
Go to your view edit page and select "table" as your view mode.
Go to "Theme information" and look for the desired (style output) template name.
Copy this name and create a new file in your theme folder using this name. I.e.: views-view-table--view-name-block.tpl.php.
In this file you can use the following structure (as an example) to get the values of the fields:
<?php foreach($rows as $row):?>
<div>
<h2><?php print $row['title'];?></h2>
<p><?php print $row['field_custom'];?></p>
</div>
<?php endforeach;?>
Don't forget press rescan theme files after you saved the file in the view theme section.
You can get as specific as you want. Create a block view next to the default or page view and you will see specific file names in the "Theme information" section.

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"

Creating a link field in a custom content type

I have several custom content types and in one specific one I need to offer two fields, one for the href for a link, and one for the text of a link this way I can build and style it with minimal user involvement of the HTML/CSS. I also have a custom node.tpl for this content type. My problem is that drupal throws divs around each field I create that aren't in my template file for this content type (node-custom.tpl) and I can't put the href for a link with divs around it inside google.co.uk</div>"> See my problem. Maybe I'm doing this all wrong so any other ideas are welcome.
Please note I'm trying to create this site with minimum involvement of HTML/CSS access for the user. I'm aware I could hand code the link in the field.
The easiest way to do this would be to use a preprocess function in your template.php file and build the link up manually:
function mytheme_preprocess_node(&$vars) {
$node = $vars['node'];
if ($node->type = 'my_type') {
$uri = $node->field_name_of_link_field[LANGUAGE_NONE][0]['value'];
$text = $node->field_name_of_display_text_field[LANGUAGE_NONE][0]['value'];
$vars['my_link'] = l($text, $uri); // Using Drupal's l() function to render a link
}
}
Then in your node template file you'll have access to the variable $my_link which you can output anywhere, and will contain the correct HTML for the link. Finally, go to the "Manage Display" page for your content type and set the display of the two fields you no longer need to output to 'Hidden'.
There are other ways so if that's no good let me know
EDIT
Just to add, I think the easiest way to do this would actually be to install the Link module and use the provided field type instead of the two other fields you're currently using.

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

Resources