?
Solved

How do I theme a view?

Posted on 2010-04-06
17
Medium Priority
?
250 Views
Last Modified: 2012-05-09
I created a custom content type named projects.  I'm using the node title and node body and created three fields for status, coordinator, and client.  I tried creating a tpl file so I can loop through the nodes in the view and style them accordingly.  This may be more of a php question I'm not sure but my code is below.  I tried at a minimum to render the title and the body with no success.  It keeps giving me the following error:
warning: Invalid argument supplied for foreach() line 11
Any help will be appreciated.
<?php foreach ($nodes as $node) 
	{
	print '<strong>'.$node->node_title.'</strong><br />';
	print $node->node_body.'<br /><br />';
	}
?>

Open in new window

0
Comment
Question by:xamian
  • 9
  • 6
  • 2
17 Comments
 
LVL 3

Expert Comment

by:Enclavet
ID: 29938010
Are you sure $nodes is an array?
0
 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 29938208
Here you go:
<?php 
if (!empty($nodes)) {
foreach ($nodes as $node) 
        {
        print '<strong>'.$node->node_title.'</strong><br />';
        print $node->node_body.'<br /><br />';
        }
}
?>

Open in new window

0
 
LVL 1

Author Comment

by:xamian
ID: 30014133
OK...I'm a total Drupal/PHP newb so I'm not sure what $nodes is.  I thought that all views were always returned as an array but maybe it's an object.  Regardless I thought I could do a ForEach loop on both arrays and objects but maybe I can't.

Rok's code at least got rid of the error message but the page is blank.

I also tried entering the following code just so i could see how big the array was:
<?php print count($nodes); ?>
The resulting page renders a 0, so maybe $nodes isn't an array.

Any ideas on what I'm doing wrong?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 12

Expert Comment

by:Rok-Kralj
ID: 30021956
var_dump($nodes);

will print $nodes content and type. Sorry, I'm not into Drupal neither.
0
 
LVL 1

Author Comment

by:xamian
ID: 30026675
OK, that rendered "NULL", so I'm guessing that means it doesn't exist?  What am I missing?  I have two versions of this view, one standard, just to make sure it's working, (which it is) and another I'm experimenting with creating a custom TPL file for (which is the one rendering NULL).  The only difference between the two is their URL.
Any ideas?
0
 
LVL 3

Expert Comment

by:Enclavet
ID: 30031007
Can you give a little bit more information about what you are trying to do?

So for the custom content type projects
You want to theme a view that will display a list of nodes with that custom content type?

0
 
LVL 1

Author Comment

by:xamian
ID: 30037637
I'll try my best...
I used CCK to create a custom content type named PROJECTS. In the SUBMISSION FORM SETTINGS for this content type I chose to make use of the TITLE and BODY fileds which I labeled as NAME (aka TITLE) and DESCRIPTION (aka BODY). I also created three custom fields which I will display below along with their "machine name";
CLIENT NAME: field_itg_project_client
CONTACT PERSON: field_itg_project_contact
PROJECT STATUS: field_itg_project_status

I then created a view to render the list of projects (see View 1) which works well (See View Published) but I want to change the styling a bit.  It was my understanding that I could create a custom TPL file just for that view so I created a TPL file named "views-view--ITMIS Projects.tpl.php", which is what I'm having trouble with now.  I need to understand how to read and output the data coming from the view using a custom TPL file.
As I mentioned before at least the code Rok gave me didnt render a PHP error message, but I'm still not seeing anything.
Any ideas?

view1.jpg
view-published.jpg
0
 
LVL 3

Expert Comment

by:Enclavet
ID: 30062274
Ok I think I know what your trying to do:

First:
Goto this page in Drupal:
Home  ¿ Administer ¿ Site building ¿ Views
Edit view ITMIS_Projects
At least for my tutorial make sure Row Style is set to Fields
Under Basic settings theres a field saying Theme: Information
Click on Information.

Then go to drupalroot/sites/all/modules/views/theme
There should be a file views-view-fields.tpl.php:

<?php
// $Id: views-view-fields.tpl.php,v 1.6 2008/09/24 22:48:21 merlinofchaos Exp $
/**
 * @file views-view-fields.tpl.php
 * Default simple view template to all the fields as a row.
 *
 * - $view: The view in use.
 * - $fields: an array of $field objects. Each one contains:
 *   - $field->content: The output of the field.
 *   - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
 *   - $field->class: The safe class id to use.
 *   - $field->handler: The Views field handler object controlling this field. Do not use
 *     var_export to dump this object, as it can't handle the recursion.
 *   - $field->inline: Whether or not the field should be inline.
 *   - $field->inline_html: either div or span based on the above flag.
 *   - $field->separator: an optional separator that may appear before a field.
 * - $row: The raw result object from the query, with all data it fetched.
 *
 * @ingroup views_templates
 */
?>
<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>
  <?php echo "TEST"; ?>
  <<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>">
    <?php if ($field->label): ?>
      <label class="views-label-<?php print $field->class; ?>">
        <?php print $field->label; ?>:
      </label>
    <?php endif; ?>
      <?php
      // $field->element_type is either SPAN or DIV depending upon whether or not
      // the field is a 'block' element type or 'inline' element type.
      ?>
      <<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_ty$
  </<?php print $field->inline_html;?>>
<?php endforeach; ?>

The above is how you want to make your template. So take this and copy it to another tpl file and edit that.
If you have any more questions let me know.
0
 
LVL 3

Expert Comment

by:Enclavet
ID: 30062620
Also depending on what theme you are using, you can copy views-view--ITMIS_Projects.tpl.php to your theme directory. Then in the Home  ¿ Administer ¿ Site building ¿ Views : Under Basic settings theres a field saying Theme: Information

Hit Rescan Templates and your  views-view--ITMIS_Projects.tpl.php will be highlighted indicating it is using that one.
0
 
LVL 1

Author Comment

by:xamian
ID: 30111649
Actually that is exactly what I'm trying to do, which is placing the custom theme for that particular view into my custom theme directory.
Unfortunately it didn't work, here's what I did...
1.) I made a copy of my original view called "ITMIS Projects2" (See View 1) so I can still have the original that kinda works.
2.) I made sure the row style was set to "Fields".
3.) I copied the file as you instructed and renamed it "views-view--ITMIS-Projects2.tpl.php"
4.) I placed the file in my custom theme directory
5.) I then went to SITE_CONFIGURATION -> PERFORMANCE to clear the cache
6.) I loaded the page and got an invalid argument error.(See View Error)
view1.jpg
view-error.jpg
0
 
LVL 3

Expert Comment

by:Enclavet
ID: 30116131
Can you rename the tpl file to views-view-fields--ITMIS-Projects2.tpl.php and paste the following in:
<?php
// $Id: views-view-fields.tpl.php,v 1.6 2008/09/24 22:48:21 merlinofchaos Exp $
/**
 * @file views-view-fields.tpl.php
 * Default simple view template to all the fields as a row.
 *
 * - $view: The view in use.
 * - $fields: an array of $field objects. Each one contains:
 *   - $field->content: The output of the field.
 *   - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
 *   - $field->class: The safe class id to use.
 *   - $field->handler: The Views field handler object controlling this field. Do not use
 *     var_export to dump this object, as it can't handle the recursion.
 *   - $field->inline: Whether or not the field should be inline.
 *   - $field->inline_html: either div or span based on the above flag.
 *   - $field->separator: an optional separator that may appear before a field.
 * - $row: The raw result object from the query, with all data it fetched.
 *
 * @ingroup views_templates
 */
?>
<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>
  <<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>">
    <?php if ($field->label): ?>
      <label class="views-label-<?php print $field->class; ?>">
        <?php print $field->label; ?>:
      </label>
    <?php endif; ?>
      <?php
      // $field->element_type is either SPAN or DIV depending upon whether or not
      // the field is a 'block' element type or 'inline' element type.
      ?>
      <<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>>
  </<?php print $field->inline_html;?>>
<?php endforeach; ?>

Then rescan the templates in Defaults: Theming information and ensure that views-view-fields--ITMIS-Projects2.tpl.php is bold.
0
 
LVL 1

Author Comment

by:xamian
ID: 30119231
OK that's a lot better, it's rendering all of my project data, but it looks exactly like my original view. I was under the impression that I would be able isolate and mark-up each piece of data. To put it more simply I want each project to look kinda like this:

Project Title in Bold
The project description goes underneath the title.  These first two fields don't need labels.  The client, status, and contact would go beneath the description with labels.
Client: John Doe
Contact: Bill Smith
Status: Open
Is there another template that I can copy from the theme information samples that would have a better example of outputting each field?
0
 
LVL 3

Accepted Solution

by:
Enclavet earned 2000 total points
ID: 30122627
Change your Row Style to Node.
Then create a file node-view-ITMIS-Projects2.tpl.php in your theme directory.
You can theme each node like this:

<?php
echo $title."<br>";
echo $body."<br>";
echo $field_test[0]['value']."<br>";
echo $field_test2[0]['value']."<br>";
?>

Change $field_test to $field_nameoffield and it should return the value of a node.

Play around with that.

Note the $body might give you the description, client, contact, status all in one so try it with just the body first.
0
 
LVL 1

Author Comment

by:xamian
ID: 30124089
Almost there.  Regardless if I do this:

<?php foreach ($fields as $id => $field): ?>
      <?php
    echo $title."<br>";
    echo $body."<br>";
    echo $field_itg_project_client_value[0]['value']."<br>";
    echo $field_itg_project_contact_value[0]['value']."<br>";
    echo $field_itg_project_status_value[0]['value']."<br>";
    ?>
<?php endforeach; ?>

or this

<?php foreach ($fields as $id => $field): ?>
      <?php
    echo $title."<br>";
    echo $body."<br>";
    ?>
<?php endforeach; ?>

It renders every piece of data associated with the full node; Taxonomy, Submitted By, even some start and end dates that I had added to the "Project" content-type.  Check out the result.



result.jpg
0
 
LVL 1

Author Comment

by:xamian
ID: 30124279
Also, in the filename of the tpl file, are you sure I don't need two hyphens between
"node-vew" and "ITMIS-Projects2.tpl.php"
Does it make a difference?  Which is correct?
This:
node-view-ITMIS-Projects2.tpl.php
or this:
node-view--ITMIS-Projects2.tpl.php
0
 
LVL 1

Author Comment

by:xamian
ID: 30125925
OK, so I went back to using Row Style as fields and the TPL file to:
views-view-fields--ITMIS-Projects2.tpl.php
and implimented the following code and got almost exactly what I wanted (see final result)
I didn't want that space between the description and the client name but I can live with it.
I need to spend some more time on my PHP then I'll be able to make it a little cleaner.
Thanks for all your help.


<?php foreach ($fields as $id => $field): ?>
	<?php if ($id==title): ?>
        <?php print "<strong>".$field->content."</strong>"; ?>
    <?php endif; ?>
    
    <?php if ($id==body): ?>
        <?php print $field->content; ?>
    <?php endif; ?>
    
    <?php if ($id==field_itg_project_client_value): ?>
        <?php print "<strong>Client:</strong> ".$field->content."<br />"; ?>
    <?php endif; ?>
    
    <?php if ($id==field_itg_project_contact_value): ?>
        <?php print "<strong>Contact:</strong> ".$field->content."<br />"; ?>
    <?php endif; ?>
    
    <?php if ($id==field_itg_project_status_value): ?>
        <?php print "<strong>Status:</strong> ".$field->content."<br /><br />"; ?>
    <?php endif; ?>
<?php endforeach; ?>

Open in new window

final-result.jpg
0
 
LVL 1

Author Closing Comment

by:xamian
ID: 31711487
Blessed is he who in the name of charity and good will shepherds the weak through the valley of Drupal, for he is truly his brother's keeper and the finder of lost children.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question