How do I display individual fields in a custom block? - drupal

A have a view created with Drupal's Views module, and have given it a Block display. It has the following fields:
first_name
last_name
professional_title
I understand that I can create a "[viewname]-block.tpl.php" file to provide a custom theme for this particular block, overriding the generic block.tpl.php template file. Within this [viewname]-block.tpl.php file, how can I pull out individual fields (eg, First name) so that I may theme them individually, giving them different classes? I've taken a look at $block, which yields $block-content, containing all fields, but this is as granular as I have managed to go so far.
Any help is appreciated.

Go into the Views UI and navigate to the block view you're trying to theme.
Under basic settings (at the bottom of it in fact) you'll see a Theme Information link. Click it.
I'm copying and pasting the official description of what that does:
"This section lists all possible templates for the display plugin and for the style plugins, ordered roughly from the least specific to the most specific. The active template for each plugin -- which is the most specific template found on the system -- is highlighted in bold."
So find the template for the field you want to theme and click the link for it - you'll get code to copy and paste. The code will be really generic, but there are notes in the generated tpl file about how to pull more specific object data.
Hope that helps

Related

Drupal page content

I'm new to drupal and reading through docs, but hoping to get an explanation for something.
I have a page called page--type-home-page.tpl.php.
Part of this page prints render($page['content'])). I want to remove something that is rendered as part of the page content from the page, but don't understand where this comes from and where/how to look.
Thanks!
Assuming you're talking about Drupal-7
Well the $page['content'] contains a string, which is a rendered version of what's injected into the content region of your theme.
By default, the only block in this region is the "Main page content" block that is generated by the Drupal core. Many things can generate this content but it always pass through the menu API. For instance, if you're viewing a node, the URL used is: node/12. The node module declares a menu entry for node/%node, this menu entry contains a callback function that will render whatever the module wants to render. The module, then, may use different strategy to render it's content from a simple function to a complex imbrication of templates.
The key to alter what's in the box, sorry, what's in the $page['content'], is to know what is rendered and to understand how it's rendered.
If it's a node, first you want to look if you can achieve your goal through the display settings of the content type. admin/structure/types/manage/page/display: And this is true for all entities (users, comments, taxonomy term etc.) Because this is the first thing the module of these entities will put together when they'll try to render your content.
If this is not enough to achieve your goal, you can look into the module that renders the path to see if it hasn't a .tpl.php. You'll be able to re-use it in your theme. You'll want to copy/paste the file in your theme and edit it.
If the module do not have a tpl file to override, try a template suggestion: here's a list from Drupal.org
Ex: node--type.tpl.php
If all this doesn't satisfy your need, you'll have to dig into preprocess functions; Those functions allow you to modify what's in the variables passed to .tpl.php files. That's a little more advanced and I recommend you to read this previous stackoverflow question/answer
Simply, don't use that $page['content'] which prints all content, but place your custom template code instead and print separate field values where you need them like:
<?php print render($content['your_field_name']); ?>
https://drupal.stackexchange.com/questions/30063/how-to-print-fields-in-node-tpl-php
If you want to do just simple styling, like excluding some field you can use content type display options like mgadrat explained, but if you want to use some complex styling, with totally custom html this solution is easier.

Drupal 7: How to theme nodes based on region?

How do I control the output of a node rendered inside a specific region?
Inside Sidebar First, I can theme blocks by creating a block--sidebar-first.tpl.php template file. However, creating a node--sidebar-first.tpl.php does not work.
Unfortunately, Drupal doesn't have support to have node template based on region. You can check out this link.
If you want to achieve your goal you can use Display Suite module. Here are some screen-casts about how to control your content with Display Suite. Here's also a comment about this.
For theming a node first thing you need a custom node template file.
Inorder to do that copy your node.tpl.php and rename it as node-[ur_node_name].tpl.php .
Now you have created a custom node template. In case of blog entry my node name would be node-blog.tpl.php [Drupal-6] and it would be node--blog.tpl.php [Drupal-7].
NOTE:
Make sure the [ur_node_name] part is the machine readable name.Now in your new node template give your corresponding styles and print the contents of the node.This is how you theme a node.Hope this helps you... :)

Drupal Views2 Exposed Form

A while back you commented on a poster's question for how to alter the Drupal Views2 Exposed Form. In the post you mentioned:
The proper way of changing the theme would be to override the views-exposed-form.tpl file in your theme's folder. Bear in mind that this will apply to all exposed filter forms, to theme a specific one, you will need to use a different name for that filename, like:
views-exposed-form--TITLE--DISPLAY.tpl.php views-exposed-form--TITLE.tpl.php
and some others, you can check the Theme: Information section of your views for template naming conventions.
Can you expand on what the 'TITLE' and 'DISPLAY' are exactly? Are both template files needed or just one of them?
Thanks.
I am not the author of the post you are referring to, but I can answer this question.
The way Views theming works is that there is a base template for a specific part of the output and this base template can be made more specific by adding additional qualifiers to it.
If you copied /sites/all/modules/views/theme/views-exposed-form.tpl.php to your theme folder (or from wherever you have Views installed), made changes to the copy, and then edited your view and chose Theme: Information and clicked Rescan Template Files, Views would now use this template in your theme folder for rendering the exposed filters form. This is the base template name and so it will be used for every view's exposed filters form.
If the change you're making should be applied to every exposed filters form then you're done. However you probably want to limit this overriden template file to specific views or even specific displays of specific views. If you want to apply the template only to a view named testview then you could rename your theme's views-exposed-form.tpl.php to views-exposed-form--testview.tpl.php. Don't forget to use the Rescan Template Files button after each rename or new file. Now that template only applies to the view testview and all of its displays.
A view can have multiple displays - maybe a block, a page, a page for admins, etc. These are listed vertically down the left side of the edit page for a view. You can get more specific and target a particular display of a particular view. To do this, you might name this template file views-exposed-form--testview--page-1.tpl.php which would cause it only to be used for the testview view and only for the first page display of that view. You can find the display name by hovering over the tab - it will appear at the end of the URL your browser shows in the status (default, page_1, etc). Just remember to turn underscores to hyphens in the template filenames: use page-1 not page_1.
You can also target all pages for example: views-exposed-form--testview--page.tpl.php.
These naming conventions as well as a list of templates which can be overriden are available here: http://views.doc.logrus.com/ (click Views Template Files).
So, if you have a specific display or view where you want to override the default template then you only need a single file targeted for that combination. If you need to target additional views or display combinations, you will have more files.
You can try Views exposed form layout module for views exposed form theming.

Is it possible to add buttons (link) to a view in Drupal?

I am looking to add separate buttons that will be displayed at the top of the view that I am creating in Drupal.
So far all the fields that I have added appear inside of the table as they should. The only thing that shows outside of the table is the search filter above (as it should).
I would like to add 3 buttons(links) in a row that are positioned above the table and below the search area.
Is there any way to do this in Views? If so could someone describe the process? Thanks!
The most straightforward way to do this is through the theme layer. Edit the view, and go into the Header option under Basic settings. There, you can put in your custom HTML for the buttons.
However, as you might've noticed, this places the header above the exposed filters, whereas you want the header below them. To do this, go into the Theme information option under Basic settings, and find the Display output section.
There, you'll see a list of file name hints. Create a file within your theme using one of those filename hints. Then, click on Display output: you'll receive the default code you'll need to put within your newly created template file.
You'll notice in this new template of yours that the $header code block is before the $exposed code block. Just switch the positions and save your template. Back on the view configuration page, click on Rescan template files, save your view, and the header you've added will now be below the exposed filters.

Drupal theming a row in a view's display

I'm trying to theme a single "display" in a view. What I'm trying to achieve is to have some control over how the row is rendered (eg. print the fields in the row, and maybe add some text in between. along with some divs for styling purpose). I've looked for days for the template to edit. I've used even the Theme Developer and the View's Theme Information, but no luck. All
My view's name is Calendar, and there are a bunch of displays. Could anyone tell me what I should do in order to customize the rows in this particular display? Does drupal even provide this flexibility?
The Name and Title of my display in the Calendar view is:
Name: List view
Title: List
Here's the theme information for row style:
Row style output: views-view-fields.tpl.php, views-view-fields--calendar.tpl.php, views-view-fields--page.tpl.php, views-view-fields--calendar--page.tpl.php, views-view-fields--calendar.tpl.php, views-view-fields--page-1.tpl.php, views-view-fields--calendar--page-1.tpl.php
Could someone tell me how to
Edit:
(Some additional information the OP posted in an answer)
I'm using Drupal 6. I'm not sure what page-1 indicates here The following is the list of display I have in this view:
Defaults
Calendar page
Calendar block
Month view
Week view
Block view
Upcoming
List view
Ongoing Block Feed
As you see, none of which has the string "page-1". I edited views-view-list.tpl.php, one of the file listed for "style output". The only variable I have access to is rows, which is an array containing all individual rendered "row". Like I mentioned before, the rows are generated somewhere else, and already contain the rendered fields. How can I customize these rows?
You shouldn't have to add anything to template.php if you are using Drupal 6.
The list of possible theme files that you have there goes in order from least to most specific. For instance, the first one, views-view-fields.tpl.php, will apply to all fields of all views on your site. The last one, views-view-fields--calendar--page-1.tpl.php, will only apply to the "page 1" display of the "calendar" view, so I'm guessing that's the one you want to overwrite.
When you click on Theme Information in the view, the display provided ("page 1" or whatever) applies to the display you are currently on. So if you want to theme fields in a certain display (the "blocky block" display of the "calendar" view, for instance), then you would click the "blocky block" display in the vertical menu on the left, then click Theme Information for that display.
So once you figure out which one you want to use, make a new file in your theme directory with that name, then copy of the contents of the current theme file (click the name of the theme files section on the Theme Information page) into your new theme file, then edit away. Remember, if you overwrite a theme file, you have to click "Rescan template files" in the Theme Information page for the new file to take effect.
Hope that helps!
The displays have custom names on the surface only, but internally they are referenced by id. 'page_1' will be your first display of type 'page' (blocks will become 'block_[id]', feeds will become 'feed_[id]' and so on).
So if you posted your displays in order, 'page_1' would be the 'Calendar' display. You can check this by looking at the links under the display entries in the display listing on the right of the views config page. They will have anchors named '#views-tab-page_1', '#views-tab-block_1' and so on.
As for overriding the templates, be aware that if you add a new *.tpl.php file, you need to reset the theme registry for it to be 'picked up' by Drupal.
Have you added the custom code needed in the template.php file? I know in drupal 5 you need to put code in 3 files:
template.php
views-views-fields--[name].tpl.php
views-views-fields--[name].css
Also you might try:
http://drupal.org/node/128741
answering the second question: the style output is not the template you're looking for. row style is the template that edits how fields are arranged within the row. if you want to edit how a particular field outputs its content, then the specific field template should be edited.

Resources