node_view does not make ['view'] s for the cck fields in content profiles

YesCT - April 2, 2009 - 20:35
Project:Content Profile
Version:6.x-1.0-beta3
Component:Base module
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

does node_view not work with cck fields in content profiles?

When I use ['value'], the stuff gets printed, but ['value'] is the bad way.

When I use ['view'], nothing gets printed.

like:

<?php
<hr>
<?
php
 
if ( $node->field_districtadvisors[0]['uid'] == NULL) {
    echo
"<p> Currently, there are no DAs for this District. Contact the ACL.";
  } else {
    foreach ((array)
$node->field_districtadvisors as $leader_item) {
      echo
"<h2>District Advisor</h2>";
      echo
"<p>";
      print
$leader_item['view'];
      echo
"<br>";
     
$new_leader_node = node_view(content_profile_load('profile', $leader_item['uid']));
     
//$new_leader_node = content_profile_load('profile', $leader_item['uid']);
// print name
echo "first name: ";
      print
$new_leader_node->field_firstname[0]['view']; echo " ";
//      if (0 != strcmp($new_leader_node->field_firstname[0]['value'],"")) {
//        print $new_leader_node->field_firstname[0]['value']; echo " "; }
....
?>

What am I doing wrong?

Thanks.

#1

JamesAn - April 3, 2009 - 00:15

Hi Cathy,

node_view() returns the "HTML representation of the themed node", not an object structure that stores the values. In the API document, you'll notice that node_view() returns the output of theme('node').

If you want to access the individual field values, you shouldn't use node_view(). Try the line of code you commented out:
$new_leader_node = content_profile_load('profile', $leader_item['uid']);

#2

YesCT - April 3, 2009 - 02:41
Category:bug report» support request

JamesAn, Thanks so much. This does make more sense.

Is there another function that would theme a field for me?

So I could do something like my invented field_view below:

<?php
    $new_leader_node
= content_profile_load('profile', $leader_item['uid']);
// print name
echo "first name: ";
      print
field_view($new_leader_node->field_firstname[0]['view']); echo " ";
?>

I'm asking because someone insisted it was really bad to use the
$new_leader_node->field_firstname[0]['value']
['value'] because of security.
Also, I was hoping that something like field_view would know about the permissions user roles have to view certain cck fields, and not other cck fields.

Thanks, CT

#3

andreiashu - April 3, 2009 - 03:06

CCK has the content_view_field function. You can find its definition here http://api.audean.com/api/function/content_view_field/API+Reference.

BUT: I think that you should download the latest DEV snapshot and read the README file:

Theming: Easily use profile information in your templates!
-----------------------------------------------------------
Content Profile adds a new variable $content_profile to most templates related to users.
So this variable allows easy access to the data contained in the users' profiles.
Furthermore it does its job fast by lazy-loading and caching the needed content profile
nodes.

#4

JamesAn - April 3, 2009 - 06:53

You can use $new_leader_node->field_firstname[0]['value'], but you'll have to manually filter it with check_plain().

So, just use:
print check_plain($new_leader_node->field_firstname[0]['view']);.

#5

YesCT - April 3, 2009 - 14:38

Thank you so much. I will look into that. I just felt like I didnt know what to search for!

#6

YesCT - April 3, 2009 - 14:41

JamesAn (and others).

I had been using print check_plain($new_leader_node->field_firstname[0]['value']);

But someone told me that was "bad" that I would have to do my own permissions checking.

And it does seem like cck fields I restricted according to roles in the Admin were being displayed to anyone, even anonymous users. I thought that it was showing the cck fields to everyone because the "view" functions did permission checking, and I was not using the "view" ... so I was hoping that if I figured out the right way, the best practices way, well... that things would be better. :)

What do you think?

#7

kenorb - July 1, 2009 - 11:35

#8

JamesAn - July 9, 2009 - 20:05

Oy vey, this issue managed to slip past me and I've forgotten all about it. Let's see... permission checking.

Instead of

print check_plain($new_leader_node->field_firstname[0]['value']);

try
$profile = content_types('profile');
if(content_access('view', $profile['fields']['field_firstname'])) {
  print check_plain($new_leader_node->field_firstname[0]['value']);
}

content_types($type) gets data about the content $type; in this case, we want information about the fields in our 'profile' type.
content_access($op, $field, $user) checks if $user can do the $op (view or edit) on the $field, which is the array field definition given by content_types(). If $user is not specified, content_access() checks against the current user.

#9

YesCT - July 10, 2009 - 05:44

Thanks!

#10

JamesAn - July 11, 2009 - 05:37
Status:active» fixed

Unless there's something else, I think we're done here. =)

#11

System Message - July 25, 2009 - 05:40
Status:fixed» closed

Automatically closed -- issue fixed for 2 weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.