I am using contemplate with cck_field_perms...it seems like a dangerous combination.

When I don't adjust the template, the cck_field_permissions work properly. The user only sees and can edit the proper items.

But when I enable the "Affect body output" checkbox in the template, and don't change a thing in the code, the fields that are supposed to be blocked from view are now viewable! As I said, I didn't even change the code.

So is there extra PHP code that I need to surround the fields that I want it to listen to the CCK field permissions?

CommentFileSizeAuthor
#3 body_var.txt1.86 KBmdowsett
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jrglasgow’s picture

Status: Active » Postponed (maintainer needs more info)

Can you post a copy of the 'Body variables' that you have in the template?

mdowsett’s picture

Gladly - thanks for the offer:

First Name

print $node->field_fname[0]['view']

Last Name

print $node->field_lname[0]['view']

Address

print $node->field_address[0]['view']

Preferred Phone Number

print $node->field_pref_phone[0]['view']

Alternate Phone Number

print $node->field_alt_phone[0]['view']

Last Membership Year

print $node->field_year_last_member[0]['view']

Received Handbook

print $node->field_received_handbook[0]['view']

Whitewater List

print $node->field_ww_list[0]['view']
mdowsett’s picture

FileSize
1.86 KB

that didn't post too well, did it?!

Attached is a txt file.

jrglasgow’s picture

I was looking for something a little more like this



An example node has been loaded and its properties appear below. Click on the the property names to add them to your template.

$node->nid
    542 
$node->vid
    543 
$node->type
    page 
$node->status
    1 
$node->created
    1206478553 
$node->changed
    1206478553 
$node->comment
    2 
$node->promote
    1 
$node->sticky
    0 
$node->revision_timestamp
    1206478553 
$node->title **
    Sample Title
$node->body
    <p>sample body</p>
$node->log
$node->format
    1 
$node->uid
    192 
$node->name
    cristyn.sorlien 
$node->picture
$node->data
    a:5:{s:18:"profile_store_name";s:14:"Klamath Office";s:7:"contact";i:1;s:14:"picture_delete";s:0:"";s:14:"picture_upload";s:0:"";s:5:"block";a:4:{s:4:"menu";a:2:{i:96;i:1;i:29;i:1;}s:8:"approval";a:3:{i:2;i:1;i:1;i:1;i:0;i:1;}s:14:"IdahoOfficeFax";a:1:{i:0;i:1;}s:4:"user";a:2:{i:2;i:0;i:3;i:1;}}} 
$node->last_comment_timestamp
    1206836526 
$node->last_comment_name
$node->comment_count
    1 
$node->taxonomy (array)

$node->files (array)

    $node->files[685] (object)

        $node->files[685]->fid
            685 
        $node->files[685]->nid
            542 
        $node->files[685]->filename
            Employee Disciplinary Action Policy.doc 
        $node->files[685]->filepath
            files/Employee Disciplinary Action Policy.doc 
        $node->files[685]->filemime
            application/msword 
        $node->files[685]->filesize
            27648 
        $node->files[685]->vid
            543 
        $node->files[685]->description
            Employee Disciplinary Action Policy.doc 
        $node->files[685]->list
            1 

    $node->files[686] (object)

        $node->files[686]->fid
            686 
        $node->files[686]->nid
            542 
        $node->files[686]->filename
            S & H DA Form.doc 
        $node->files[686]->filepath
            files/S & H DA Form.doc 
        $node->files[686]->filemime
            application/msword 
        $node->files[686]->filesize
            29184 
        $node->files[686]->vid
            543 
        $node->files[686]->description
            S & H DA Form.doc 
        $node->files[686]->list
            1 

$node->readmore (boolean)
    TRUE 
$node->content (array)

    $node->content['body'] (array)

        $node->content['body']['#weight'] (integer)
            0 
        $node->content['body']['#value']
            <p>sample content body</p> 
        $node->content['body']['#printed'] (boolean)
            TRUE 

    $node->content['files'] (array)

        $node->content['files']['#weight'] (integer)
            50 
        $node->content['files']['#value']
            sample attachments
        $node->content['files']['#printed'] (boolean)
            TRUE 

    $node->content['#children']
        <p>sample content</p>
    $node->content['#printed'] (boolean)
        TRUE 

$node->links (array)

    $node->links['comment_add'] (array)

        $node->links['comment_add']['title']
            Add new comment 
        $node->links['comment_add']['href']
            comment/reply/542 
        $node->links['comment_add']['attributes'] (array)

            $node->links['comment_add']['attributes']['title']
                Share your thoughts and opinions related to this posting. 

        $node->links['comment_add']['fragment']
            comment-form 

    $node->links['fasttoggle_status'] (array)

        $node->links['fasttoggle_status']['title']
            published 
        $node->links['fasttoggle_status']['href']
            node/542/toggle/status 
        $node->links['fasttoggle_status']['query']
            destination=admin%2Fcontent%2Ftemplates%2Fpage&token=61d48367eff7f25858b2a40cf8a6d3ac 
        $node->links['fasttoggle_status']['attributes'] (array)

            $node->links['fasttoggle_status']['attributes']['class']
                fasttoggle 
            $node->links['fasttoggle_status']['attributes']['title']
                Toggle this setting 

    $node->links['fasttoggle_sticky'] (array)

        $node->links['fasttoggle_sticky']['title']
            not sticky 
        $node->links['fasttoggle_sticky']['href']
            node/542/toggle/sticky 
        $node->links['fasttoggle_sticky']['query']
            destination=admin%2Fcontent%2Ftemplates%2Fpage&token=c5fa761a9adaa862221e0d5efa46a226 
        $node->links['fasttoggle_sticky']['attributes'] (array)

            $node->links['fasttoggle_sticky']['attributes']['class']
                fasttoggle 
            $node->links['fasttoggle_sticky']['attributes']['title']
                Toggle this setting 

    $node->links['fasttoggle_promote'] (array)

        $node->links['fasttoggle_promote']['title']
            promoted 
        $node->links['fasttoggle_promote']['href']
            node/542/toggle/promote 
        $node->links['fasttoggle_promote']['query']
            destination=admin%2Fcontent%2Ftemplates%2Fpage&token=dd88bb02380aab293a2f7c1b4926b365 
        $node->links['fasttoggle_promote']['attributes'] (array)

            $node->links['fasttoggle_promote']['attributes']['class']
                fasttoggle 
            $node->links['fasttoggle_promote']['attributes']['title']
                Toggle this setting 


I just copied and pasted the vars and put them in <code> tags.

mdowsett’s picture

An example node has been loaded and its properties appear below. Click on the the property names to add them to your template.

$node->nid
    7591 
$node->vid
    7904 
$node->type
    bio 
$node->status
    1 
$node->created
    1206762997 
$node->changed
    1206848254 
$node->comment
    0 
$node->promote
    1 
$node->sticky
    0 
$node->revision_timestamp
    1206848254 
$node->title **
    hi 
$node->body
    <h3 >Name:</h3> <div class="field field-type-text field-field-fname"> <div class="field-items"> <div class="field-item">admin&#039;s first name </div> </div> </div> <div class="field field-type-cck-address field-field-address"> <h3 class="field-label">Address</h3> <div class="field-items"> <div class="field-item"></div> </div> </div> <div class="field field-type-text field-field-pref-phone"> <h3 class="field-label">Preferred Phone</h3> <div class="field-items"> <div class="field-item"></div> </div> </div> <div class="field field-type-text field-field-alt-phone"> <h3 class="field-label">Alternate Phone</h3> <div class="field-items"> <div class="field-item"></div> </div> </div> <div class="field field-type-date field-field-year-last-member"> <h3 class="field-label">Last Membership Year</h3> <div class="field-items"> <div class="field-item"></div> </div> </div> 
$node->log
$node->format
    3 
$node->uid
    1 
$node->name
    admin 
$node->picture
$node->data
    a:15:{s:6:"notify";i:1;s:6:"submit";s:18:"Create new account";s:10:"form_token";s:32:"a9080a0a3fc8edef273c232716e0f0dd";s:7:"form_id";s:13:"user_register";s:7:"contact";i:0;s:14:"picture_delete";s:0:"";s:15:"profile_ww_list";i:0;s:24:"profile_year_last_member";a:3:{s:4:"year";s:4:"2000";s:5:"month";s:1:"4";s:3:"day";s:1:"1";}s:23:"subscriptions_subscribe";i:1;s:18:"subscriptions_auto";i:0;s:14:"tinymce_status";s:5:"false";s:8:"og_email";s:1:"2";s:11:"mail_accept";i:0;s:5:"block";a:2:{s:5:"block";a:1:{i:1;i:0;}s:2:"og";a:1:{i:4;i:1;}}s:17:"mimemail_textonly";i:0;} 
$node->last_comment_timestamp
    1206762997 
$node->last_comment_name (NULL)
$node->comment_count
    0 
$node->field_fname (array)

    $node->field_fname[0] (array)

        $node->field_fname[0]['value'] **
            admin's first name 
        $node->field_fname[0]['view']
            admin&#039;s first name 

$node->field_lname (array)

    $node->field_lname[0] (array)

        $node->field_lname[0]['value'] **
        $node->field_lname[0]['view']

$node->field_address (array)

    $node->field_address[0] (array)

        $node->field_address[0]['street1']
        $node->field_address[0]['street2'] (NULL)
        $node->field_address[0]['apt'] (NULL)
        $node->field_address[0]['city']
        $node->field_address[0]['state']
        $node->field_address[0]['zip']
        $node->field_address[0]['country']
        $node->field_address[0]['other'] (NULL)

$node->field_pref_phone (array)

    $node->field_pref_phone[0] (array)

        $node->field_pref_phone[0]['value'] **
        $node->field_pref_phone[0]['view']

$node->field_alt_phone (array)

    $node->field_alt_phone[0] (array)

        $node->field_alt_phone[0]['value'] **
        $node->field_alt_phone[0]['view']

$node->field_year_last_member (array)

    $node->field_year_last_member[0] (array)

        $node->field_year_last_member[0]['value'] ** (NULL)
        $node->field_year_last_member[0]['view']

$node->field_received_handbook (array)

    $node->field_received_handbook[0] (array)

        $node->field_received_handbook[0]['value'] **
        $node->field_received_handbook[0]['view'] (NULL)

$node->field_ww_list (array)

    $node->field_ww_list[0] (array)

        $node->field_ww_list[0]['value'] **
            Yes 
        $node->field_ww_list[0]['view']
            Yes 

$node->taxonomy (array)

$node->readmore (boolean)
    FALSE 
$node->content (array)

    $node->content['field_fname'] (array)

        $node->content['field_fname']['#access'] (boolean)
            TRUE 
        $node->content['field_fname']['#value']
            <div class="field field-type-text field-field-fname"><div class="field-label">First Name:&nbsp;</div><div class="field-items"><div class="field-item">admin&#039;s first name</div></div></div> 
        $node->content['field_fname']['#weight']
            -4 
        $node->content['field_fname']['#printed'] (boolean)
            TRUE 

    $node->content['field_lname'] (array)

        $node->content['field_lname']['#access'] (boolean)
            TRUE 
        $node->content['field_lname']['#value']
        $node->content['field_lname']['#weight']
            -3 
        $node->content['field_lname']['#printed'] (boolean)
            TRUE 

    $node->content['field_address'] (array)

        $node->content['field_address']['#access'] (boolean)
            TRUE 
        $node->content['field_address']['#value'] (NULL)
        $node->content['field_address']['#weight']
            -2 
        $node->content['field_address']['#printed'] (boolean)
            TRUE 

    $node->content['field_pref_phone'] (array)

        $node->content['field_pref_phone']['#access'] (boolean)
            TRUE 
        $node->content['field_pref_phone']['#value']
        $node->content['field_pref_phone']['#weight']
            -1 
        $node->content['field_pref_phone']['#printed'] (boolean)
            TRUE 

    $node->content['field_alt_phone'] (array)

        $node->content['field_alt_phone']['#access'] (boolean)
            TRUE 
        $node->content['field_alt_phone']['#value']
        $node->content['field_alt_phone']['#weight']
            0 
        $node->content['field_alt_phone']['#printed'] (boolean)
            TRUE 

    $node->content['body'] (array)

        $node->content['body']['#weight'] (integer)
            0 
        $node->content['body']['#value']
        $node->content['body']['#printed'] (boolean)
            TRUE 

    $node->content['field_year_last_member'] (array)

        $node->content['field_year_last_member']['#access'] (boolean)
            TRUE 
        $node->content['field_year_last_member']['#value']
        $node->content['field_year_last_member']['#weight']
            1 
        $node->content['field_year_last_member']['#printed'] (boolean)
            TRUE 

    $node->content['field_received_handbook'] (array)

        $node->content['field_received_handbook']['#access'] (boolean)
            TRUE 
        $node->content['field_received_handbook']['#value']
        $node->content['field_received_handbook']['#weight']
            2 
        $node->content['field_received_handbook']['#printed'] (boolean)
            TRUE 

    $node->content['field_ww_list'] (array)

        $node->content['field_ww_list']['#access'] (boolean)
            TRUE 
        $node->content['field_ww_list']['#value']
            <div class="field field-type-text field-field-ww-list"><div class="field-label">Whitewater List:&nbsp;</div><div class="field-items"><div class="field-item">Yes</div></div></div> 
        $node->content['field_ww_list']['#weight']
            3 
        $node->content['field_ww_list']['#printed'] (boolean)
            TRUE 

    $node->content['#children']
        <div class="field field-type-text field-field-fname"><div class="field-label">First Name:&nbsp;</div><div class="field-items"><div class="field-item">admin&#039;s first name</div></div></div><div class="field field-type-text field-field-ww-list"><div class="field-label">Whitewater List:&nbsp;</div><div class="field-items"><div class="field-item">Yes</div></div></div> 
    $node->content['#printed'] (boolean)
        TRUE 

$node->links (array)
jrglasgow’s picture

this node whose variables you just pasted, are all permissions set to TRUE or permitted? I see some variables that seem to have permission settings

$node->content['field_year_last_member'] (array)

        $node->content['field_year_last_member']['#access'] (boolean)
            TRUE // this looks like it might check to see if the current user has access to this variable
        $node->content['field_year_last_member']['#value']
        $node->content['field_year_last_member']['#weight']
            1
        $node->content['field_year_last_member']['#printed'] (boolean)
            TRUE 

If this is not the case could you please paste in a set of variables where some CCK fields are not permitted by the current user.

mdowsett’s picture

No, some are restricted (and it works if contemplate is not enabled).

The restricted ones are:
ww_list
mem_amt_pd
received_handbook

And the variables above haven't changed, but here they are again

$node->content['field_mem_amt_pd'] (array)

    $node->content['field_mem_amt_pd']['#access'] (boolean)
        TRUE 
    $node->content['field_mem_amt_pd']['#value']
    $node->content['field_mem_amt_pd']['#weight']
        2 
    $node->content['field_mem_amt_pd']['#printed'] (boolean)
        TRUE 

$node->content['field_ww_list'] (array)

    $node->content['field_ww_list']['#access'] (boolean)
        TRUE 
    $node->content['field_ww_list']['#value']
    $node->content['field_ww_list']['#weight']
        3 
    $node->content['field_ww_list']['#printed'] (boolean)
        TRUE 

$node->content['field_received_handbook'] (array)

    $node->content['field_received_handbook']['#access'] (boolean)
        TRUE 
    $node->content['field_received_handbook']['#value']
    $node->content['field_received_handbook']['#weight']
        5 
    $node->content['field_received_handbook']['#printed'] (boolean)
        TRUE 


Thanks so much for your help

jrglasgow’s picture

I set up cck_field_perms on my test site with a birth date field, I get this in my variables box.

$node->content (array)

    $node->content['field_birth_date'] (array)

        $node->content['field_birth_date']['#access'] (boolean)
            TRUE 
        $node->content['field_birth_date']['#value']
            <div class="field field-type-text field-field-birth-date"><div class="field-label">birth_date:&nbsp;</div><div class="field-items"><div class="field-item">11 Nov 1979</div></div></div> 
        $node->content['field_birth_date']['#weight']
            0 
        $node->content['field_birth_date']['#printed'] (boolean)
            TRUE 

The default contemplate for this test content type I set up:

<div class="field field-type-text field-field-birth-date">
  <h3 class="field-label">birth_date</h3>
  <div class="field-items">
      <div class="field-item"><?php print $node->field_birth_date[0]['view'] ?></div>
  </div>
</div>

Contemplate by default sees a CCK field and assumes everyone will view it, so it show it to everyone, it also doesn't show the node body. The following will check the field permissions, and show the body.

<?php
<div class="field field-type-text field-field-birth-date">
<?php 
if($node->content['field_birth_date']['#access'] ) {
?>
  <h3 class="field-label">birth_date</h3>
  <div class="field-items">
      <div class="field-item">
<? print $node->field_birth_date[0]['view'] ; ?></div>
<?php
}
else {

}
?>
  </div>
</div>
<?php print $node->content['body']['#value'] ?>
?>

Remember that you must be logged in as a user without permission to not be able to view the field. If you are logged on as the root user (uid == 1) you have all permissions, regardless of the acces control settings.
This code tested out properly on my test site.

mdowsett’s picture

For testing, I do log in as a plain-old authenticated user and the cck_field_perms are set as per the attached pic.

I'm probably not understanding contemplate....I haven't used it before.

Here is the variables related to one field I'd like to hide:

$node->content['field_mem_amt_pd'] (array)

    $node->content['field_mem_amt_pd']['#access'] (boolean)
        TRUE 
    $node->content['field_mem_amt_pd']['#value']
    $node->content['field_mem_amt_pd']['#weight']
        2 
    $node->content['field_mem_amt_pd']['#printed'] (boolean)
        TRUE 

All the lines that start with $node are clickable links that add code to the tempate....am I to pick one of them to properly show/restrict access as desired?????

My guess is that I am to click on the correct clickable links to add the code....so I tried it by clicking on all four to add them all - and this is what it added:

<?php print $node->content['field_mem_amt_pd']['#access'] ?>
<?php print $node->content['field_mem_amt_pd']['#value'] ?>
<?php print $node->content['field_mem_amt_pd']['#weight'] ?>
<?php print $node->content['field_mem_amt_pd']['#printed'] ?>

The result...to the admin user (UID=0) it showed:
1
Membership Amount Paid:
$4444
21
(I have no idea where the '1' and '21' came from"

And for the "authenticated user" it shows:
Membership Amount Paid:
$4444
2

And it shouldn't show the membership amount paid (due to the cck_field_perms set for that field and user) and it also shows the '2' but neither of the 1's....

I'm lost here....thanks for sticking with me....I hope you can make things click for me here.

jrglasgow’s picture

"am I to pick one of them to properly show/restrict access as desired?????"
Yes

notice the line $node->content['field_mem_amt_pd']['#access']. When this is set to TRUE or 1 (as it printed out in your example) the user is allowed to view the field. If you put this in your contemplate:

<?php
if ($node->content['field_mem_amt_pd']['#access']) {
  print $node->content['field_mem_amt_pd']['#value'];
}
?>

it will only print the '#value' of the field if the user has '#access'
-------------------------------------------------------------------------------------------------------------
Now I will explain things to you:

admin (root) user:
when you are logged in as the root user you see these results

<?php print $node->content['field_mem_amt_pd']['#access'] ?> : 1 (this means TRUE) - the user has access to this field
<?php print $node->content['field_mem_amt_pd']['#value'] ?> : Membership Amount Paid: $4444 - the value of the field
<?php print $node->content['field_mem_amt_pd']['#weight'] ?> : 2 - this is the weight of the field in the list (the lighter rise to the top, the heavier sink to the bottom), sort order
<?php print $node->content['field_mem_amt_pd']['#printed'] ?> : 1 (TRUE) - this field gets printed to the page

authenticated user:

<?php print $node->content['field_mem_amt_pd']['#access'] ?> : NULL (an empty character, FALSE) this could also be represented by 0 - the user doesn't have permissions to view this field
<?php print $node->content['field_mem_amt_pd']['#value'] ?> : Membership Amount Paid: $4444 - the value of the field
<?php print $node->content['field_mem_amt_pd']['#weight'] ?> : 2 - weight, sort order
<?php print $node->content['field_mem_amt_pd']['#printed'] ?> : NULL - this is not to be printed to the page because the user does not have permission

Contemplate was not written with cck_field_perms in mind, in the future it may have awareness built in. If you plan to use Contemplate you will want to gain an understanding of how the node object is constructed, or at least how you can use the many variables provided. I had to actually read your variable list a couple of times before I started to understand what was going on. If you need any more help don't be afraid to ask.

mdowsett’s picture

Category: support » feature
Status: Postponed (maintainer needs more info) » Active

MANY MANY thanks for your explanation.

I guess that answers my ultimate question....contemplate can't be used with cck_field_perms....how disappointing

I've moved this to a feature request

jrglasgow’s picture

You are not understanding what I am saying. Contemplate can be used with cck_field_perms. All you have to do is a simple bit of code in your template

<?php
if ($node->content['field_mem_amt_pd']['#access']) { // this checks to see if the user has permission to view the information in the field
  print $node->content['field_mem_amt_pd']['#value']; // then displays it if they have permission
}
?>

This is how Contemplate is designed, to allow the site administrator to decide how things were laid out, you just have to do a little work.

jrglasgow’s picture

Status: Active » Postponed (maintainer needs more info)

do you still need help with this?

mdowsett’s picture

Status: Postponed (maintainer needs more info) » Postponed

I don't have the ability/need now for this...I can't test it. I must have missed or forgotten your last reply so I thank you for your reminder. I'll try to get this working on another site.

YesCT’s picture

thank you for working through this issue here so everyone can see. This has been very helpful!

SocialNicheGuru’s picture

does cck permissions remove the field from the node edit form?

if so, how?

mdowsett’s picture

there are separate permissions to disallow node owners from seeing the field in the original node creation AND the editing.

As to HOW it does it, I have no idea.

pizangdesain’s picture

how to prnt picture in contemplate?
i just use $node->picture but its not appear...

ramones79’s picture

In my case the code for a value is for example :

<?php print $node->field_camera_image[0]['view'] ?>

I find the code you provided to check if a user has access permissions for a very useful one and I intend to use it. This one:

<?php
if ($node->content['field_mem_amt_pd']['#access']) { // this checks to see if the user has permission to view the information in the field
  print $node->content['field_mem_amt_pd']['#value']; // then displays it if they have permission
}
?>

But I'm in doubt how to implement it. Should I call for the value this way :

print $node->content['field_mem_amt_pd']['#value'];

or this way :

php print $node->field_camera_image[0]['view']

??

In the first case I call for the field X from the content of the node Y
In the second case I directly call for the field X from node Y without to involve the 'content' statement.
What is the best approach or are they both correct?

Btw I'm using actively the Views module, is it involved somehow in the way I should use to print the value of a given field ?

Thank you very much in advance :)

ramones79’s picture

Looks like I found the working code for my case (and perhaps your case) :

<?php
if ($node->field_camera_image[0]['view']['#access']) { // this checks to see if the user has permission to view the information in the field
  print $node->field_camera_image[0]['view']; // then displays it if they have permission
}
?>

Hope this helps for those of you, who have the same as mine output code from the Contemplates (in this case this code is part of my node-content_type-body.tpl.php file)

pizangdesain’s picture

Hmmm... i've try it, but it didnt appear.
I use image attached to insert image in node.
I try like this in contemplate.

<?php
if ($node->files[image]['view']['#access']) { // this checks to see if the user has permission to view the information in the field
print $node->files[image]['view']; // then displays it if they have permission
}
?>

and

<?php
if ($node->content[files]['view']['#access']) { // this checks to see if the user has permission to view the information in the field
print $node->content[files]['view']; // then displays it if they have permission
}
?>

please correct me if thats wrong coding...

ramones79’s picture

The coding itself is not wrong, but the way you try to access the fields maybe not appropriate /and non-working/
You are trying to retrieve type of content from NODE - FILES or from CONTENT - FILES.
My approach is different, because I directly call for the field (and in my case it is a custom field, called field_camera_image

It is important to check what code do you see in Contemplates for the particular content type ? You should use this code and this way to get the fields. Because I'm using some modules and they produce one kind of code, but probably you use another bunch of modules and they produce another code.

I assume that you're trying to create a custom body for a given content type (as it is my case)
Then go to Administer -> Content Management -> Content Types - and then click on the tab "Content Templates".

Find the desired content type to be changed and click on Edit Template

Click on the Body to expand and then click on Affect body ...

The code you see and the way fields are called - its the way you should call the fields too.
(If you see "This template is being read from.." you need to temporarily rename the .tpl file from the server - just add .old at the end, and refresh the page to access the code, after you see it - rename back your tpl to the original name and refresh again.)

Do you use file /disc/ based templates?
I have installed these modules on my Drupal: Views, CCK, Content Permissions, Image, ImageCache and ImageAPI

ramones79’s picture

If you use content permissions check if the anonymous user role has the permissions to view your image field. It is very possible that you don't see anything just because the code IS working, but the Anonymous user account doesn't have access rights.