How do I theme a view?

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

LVL 1
xamianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EnclavetCommented:
Are you sure $nodes is an array?
0
Rok-KraljCommented:
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
xamianAuthor Commented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Rok-KraljCommented:
var_dump($nodes);

will print $nodes content and type. Sorry, I'm not into Drupal neither.
0
xamianAuthor Commented:
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
EnclavetCommented:
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
xamianAuthor Commented:
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
EnclavetCommented:
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
EnclavetCommented:
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
xamianAuthor Commented:
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
EnclavetCommented:
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
xamianAuthor Commented:
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
EnclavetCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
xamianAuthor Commented:
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
xamianAuthor Commented:
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
xamianAuthor Commented:
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
xamianAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Drupal

From novice to tech pro — start learning today.