I have a question about printing fields out in views templates.

beckyjohnson - March 12, 2009 - 18:16

I watched the latest Lullabot video on CCK and Views and I have a theming question as a result. I need to theme a views and print fields out on the views template page I am using. In the CCK Video they showed the format of code for printing out a cck field like such:
?php print check_plain($node-> title) ?> or <?php print $node-> field_job [0] [‘view’] ?>

Is this also how I could print out variables on my views template page?

Also, I was looking at an example of a templated views page recently and the variables looked a lot different. They looked like this:

<?php print $certification; ?> or <?php print $list_price; ?>

Where do you think the programmer generated the code from above?

Which style should I use?

Thanks,
Becky

This was pretty helpful in

beckyjohnson - March 12, 2009 - 18:58

This was pretty helpful in terms of giving me more knowledge but I tried their technique and it didn't work.

<?php
 
//print '<pre>' . htmlentities(print_r($rows, 1)) . '</pre>';
  //If you have devel.module installed, comment the line above and uncomment the line below
 
dsm(array_keys($fields));
?>

I put this in my template file views-view--technical-documents.tpl.php, the name of my high level view but i just get a blank page.
And yes, I have the devel module installed and enabled.

please help! why would i go white?

Becky

try this, or could be memory

justageek - March 12, 2009 - 20:47

Could be php out of memory, that's typically the cause of the white screen.

Try this to rule of devel module:

<?php
print '<pre>';
print_r($fields);

// or their suggestion
print '<pre>' . htmlentities(print_r($rows, 1)) . '</pre>';
?>

I checked my php memory and

beckyjohnson - March 12, 2009 - 20:58

I checked my php memory and it wasn't giving me an error on the status page.

Used the snippet you have up there and it made my page go blank. It loaded but there was nothing on the page other than the views title.

Becky

me too

justageek - March 12, 2009 - 21:00

I can't get dsm() to work.

At the level you are working, you cannot, I do not think, get to individual fields. You need to overide a lower level template like the row style template. What are your Style and Row Style settings?

I have another template for

beckyjohnson - March 12, 2009 - 21:04

I have another template for my fields view but the instructions on that article you said, said to use a higher template than a field template. Also, to clarify when I put this on my page i get a white screen:

<?php
 
print '<pre>' . htmlentities(print_r($rows, 1)) . '</pre>';
 
?>
as well. But If I put this on my page:
<?php
  print '<pre>' . htmlentities(print_r($rows, 1)) . '</pre>';
I don't get a white screen but I don't get anything helpful. Seems like the closing ?> is doing something.

Anyway, my row settings: Field which title inline. Not sure it that makes a difference.
style settings : unformatted, grouped by taxonomy term.

Becky

not sure about article yet

justageek - March 12, 2009 - 21:13

the overall view template does not appear to provide a $fields variable for me when I choose row style = fields. The $rows variable is there, but it is the complete, formatted output.

I know what that article says, but I don't think it is clear. If you want control of each field, you are going to work inside your fields template, not the overall view template.

Ok I grabbed <?phpprint

beckyjohnson - March 12, 2009 - 21:14

Ok I grabbed

<?php
print '<pre>';
print_r(array_keys($fields));
?>
off your last post there and now it works. It printed out the $field in a plain text way.

hmm....

justageek - March 12, 2009 - 21:17

Here is what I get in my overall view template for defined variables, when I have a row style of fields, I don't see a 'fields' variable at all.

<?php
Array
(
    [
0] => template_file
   
[1] => variables
   
[2] => template_files
   
[3] => view
   
[4] => zebra
   
[5] => id
   
[6] => directory
   
[7] => is_admin
   
[8] => is_front
   
[9] => logged_in
   
[10] => db_is_active
   
[11] => user
   
[12] => rows
   
[13] => css_name
   
[14] => name
   
[15] => display_id
   
[16] => empty
    [
17] => exposed
   
[18] => header
   
[19] => footer
   
[20] => more
   
[21] => feed_icon
   
[22] => attachment_before
   
[23] => attachment_after
   
[24] => pager
   
[25] => admin_links_raw
   
[26] => admin_links
   
[27] => dom_id
)
    
?>

I think you are right. I

beckyjohnson - March 12, 2009 - 21:17

I think you are right. I added the code you showed me to my fields template and it printed the variables out.

<?php
print '<pre>';
print_r(array_keys($fields));
?>

I got this:

Array
(
    [0] => title
    [1] => body
    [2] => field_stages_value
    [3] => field_dl_number_value
    [4] => field_dl_upload_fid
    [5] => field_dl_filesize_value
    [6] => field_marketing_dl_format_value
)

which are the names of my fields.

Now one more question, how do I put these fields on my actual template. Can you give me an example?

from listening to the CCK video on printing out variables, I thought i would have a number and word or something to print out, like this <?php print $node-> field_job [0] [‘view’] ?> and it made sense at the time becuase they were using the dsm code and looking at it through Krumo but I'm not sure what to do with this particular page......

Thanks for all your help on this, btw. I really appreciate it.
Becky

this should help

justageek - March 12, 2009 - 21:28

this is from the core fields template in views, in the video was it a node style view?

<?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
*/
?>

I think it was probably a

beckyjohnson - March 12, 2009 - 21:32

I think it was probably a node style view they were using but in the styling views section, they didn't get very deep; they just talked about how you could generate theme files using views 2 now.

http://beta.wimaxforum.org/technical_release1 this is actually a link to my views if you want to see it so far. I'm trying to mix js and views to get a result like this : http://www.wimaxforum.org/training/courses. Someone else did that page and I'm trying to replicate it.

It's proving rather hard.

Becky

another option I like better

justageek - March 12, 2009 - 21:34

Based on your web site sample:

If you set up a view that is row style 'node', then what you get in your view is a complete list of nodes, with all the data for each node. Then, you can create your own node template for your content type. So, lets say you create a content type called 'news'. You can create a template called node-news.tpl.php. This will then handle the output for your 'news' nodes. And, in this template, you can actually have a section of code to handle output for a list of nodes, and a section of code to handle viewing one single node. I would recommend locating your node.tpl.php file for the theme you are using then just making a copy, naming it according to your content type.

Inside this node template, there is a variable called $page. If this value is 0, it means you are NOT viewing a single page for a node, you are on a list type page, so you can do this

<?php
if($page == 0){
// your php / html goes here to style the output of each node in your list
} else {

// your php  / html for viewing a single node goes here

}
?>

This is really interesting.

beckyjohnson - March 12, 2009 - 21:53

This is really interesting. If I did it the way you suggest above, could I still use the js effect that is illustrated on the page: http://www.wimaxforum.org/training/courses ? That is proving to be difficult too btw. I was trying to go off of what the other person did in their js but it's not working yet.

For example

<div class="document-controls">
  <a href="javascript:;" class="document-details-control document-details-control-nid-<?php print $nid; ?>">View Details</a>
  <a href="javascript:;" style="display:none;" class="document-details-control course-details-control-nid-<?php print $nid; ?>">Hide Details</a>
   |
  <?php if ($dl_upload_fid) { ?>
   |
    <?php print $dl_upload_fid ?>
  <?php } ?>

This part: is supposed to show a download link, so people can download the file, but it's not working

<?php if ($dl_upload_fid) { ?>
   |
    <?php print $dl_upload_fid ?>

dl_upload_fid is the variable for my upload widget that i made in CCK but this isn't showing up on the page.

Anyway, I'm going to give it a shot your way. Thanks for the advice!

Becky

well, they have a jquery affect applied

justageek - March 12, 2009 - 21:56

When their page loads jquery locates all their specific links and applies some custom show / hide functionality. I don't know exactly how they did their pages, probably something with either a custom module or a custom node template. So, the short answer is, yes you can do it, the long answer is, it might not be simple unless you know some javascript / jquery or have a js person handy.

He didn't use a module, it

beckyjohnson - March 12, 2009 - 21:59

He didn't use a module, it was custom code. For example in the css there is this:

/*training*/
/**********************/
.course-details {
  display:none;
}
/**********************/
div.course-controls{
font-size: 12px;
}

and in the js there is this:

Drupal.behaviors.crscourses = function() {
  $(".course-details-control").click( function() {
    $(".course-details",this.parentNode.parentNode).toggle("fast");
    $(".course-details-control",this.parentNode).toggle();
  }); 
  $(".course-schedule-control").click( function() {
    $(".course-schedule",this.parentNode.parentNode).toggle("fast");
    $(".course-schedule-control",this.parentNode).toggle();
  }); 
}

In the view there is this:

<div class="course-controls">
  <a href="javascript:;" class="course-details-control course-details-control-nid-<?php print $nid; ?>">View Details</a>
  <a href="javascript:;" style="display:none;" class="course-details-control course-details-control-nid-<?php print $nid; ?>">Hide Details</a>
   |
  <a href="javascript:;" class="course-schedule-control course-schedule-control-nid-<?php print $nid; ?>">View Schedule</a>
  <a href="javascript:;" style="display:none;" class="course-schedule-control course-schedule-control-nid-<?php print $nid; ?>">Hide Schedule</a>
 
  <?php if ($download_brochure_link) { ?>
   |
    <?php print $download_brochure_link; ?>
  <?php } ?>
  <?php
 
global $user;
  if(
$user->uid) {
   
$node->nid = $nid;
   
$node->type = 'course';
    if(
node_access('edit', $node)) {
      print
' | ' . l('Edit course details','node/'.$nid.'/edit',array('query' => 'destination='.$_GET['q']));
    }
  }
 
?>

 
  <?php
 
if(user_access('create course_instance content')) {
    print
' | ' . l('Add an instance of this course','node/add/course-instance',array('query' => 'parent='.$nid.'destination='.$_GET['q']));
  }
 
?>

  
</div>

<div class="course-details course-details-nid-<?php print $nid; ?>">
  <div class="field-item">
    <span class="label">Certification: </span>
    <span class="value"><?php print $certification; ?></span>
  </div>
 
  <div class="field-item">
    <span class="label">Duration: </span>
    <span class="value"><?php print $duration; ?></span>
  </div>
 
  <div class="field-item">
    <span class="label">List Price: </span>
    <span class="value"><?php print $list_price; ?></span>
  </div>
 
  <div class="field-item">
    <span class="label">Course Description: </span>
    <span class="value"><?php print $long_description; ?></span>
  </div>
</div>

Either way, cannibalizing this code is not working.....

Becky

I don't think this is going

beckyjohnson - March 12, 2009 - 23:32

I don't think this is going to work. I am going to try it but, one reason is that I needed everything to group by taxonomy term, under style: unformatted and I don't seem to be able to do this when I'm doing row style as node.... unless you know of an alternative way to achieve this?

Becky

you can still get to the fields in unformatted / field view

justageek - March 13, 2009 - 14:38

You can still get to individual fields by making it Field rather than Node. If you look at the core view for the unformatted row style, you will see that the data is in $fields, and the raw data is in an array called $rows.

Concering the javascript, I see his javascript and CSS, but somewhere there must be a line of code (unless maybe drupal just does this for you) to add his custom "click handler" code to all those link tags.

Anyway, so change to unformatted row style and it will let you choose your grouping field. Then use your original template override for row style to output your individual fields. You can basically use $fields like this:

<?php
print $fields['my_field_name']->content;
?>

this is formatted content, meaning if any of your fields contain html, Drupal will have processed it based on your selected input format, made sure it is safe, etc. If you need unformatted data, it is there somewhere, I'd have to hunt to find it.

I'm going to look again, because I think there is a way to do a "group by" with a node view, too.

this might help

justageek - March 13, 2009 - 14:41

Thanks, I'll try these out

beckyjohnson - March 16, 2009 - 15:52

Thanks, I'll try these out today.

??

karl2011 - August 29, 2009 - 23:44

??

 
 

Drupal is a registered trademark of Dries Buytaert.