Link to home
Start Free TrialLog in
Avatar of glepiza
glepiza

asked on

Views Fields visibility Drupal 7

Dear experts,

I´ve been spending long time finding a way to display a view field only for anonymous users.

In this case I have the following fields:

Name
Address
Hobbies
Messages (this is a privatemsg field)

Authenticated users can see all the fields, but anonymous users can´t see the Messages field because only registered users have permissions to send private messages to one another.

I was wondering, is there a way to show Messages as a disable link field if you are an anonymous user but when you log in it gets enabled?

Any help would be greatly appreciate it.

Winter
ASKER CERTIFIED SOLUTION
Avatar of Aaron Feledy
Aaron Feledy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hagay Mandel
I suggest using 1 view with 2 variants (pages): one for authenticated users and the other for anonymous ones, with the proper differences.
Then (after enabling "PHP code " in input formats), create a basic content type with one long text field (aside the title), and make it use php code as input format.
Then, based on the user status, embed the relevant view:

<?php
  if ($user->uid) { // User is logged in therefore an authenticated user
     print views_embed_view('VIEW_NAME = 'AUTHENTICATED'); 
  } else {
      print views_embed_view('VIEW_NAME = 'ANYNOMOUS'); 
  }
?>

Open in new window

So many ways to cook this up. :)

Another method is to use .tpl.php files. You can put the logic (like Arrow_1's) in there to display or not display whatever you'd like. This is useful if your code is in version control such as Subversion, git, etc. The tpl.php would be version controllable while data inside a Views configuration isn't.

If you export your views or if you use the Features module, then this approach isn't going to help you with version control, and you should definitely use an in-interface solution like the ones above.

To find which .tpl.php file you would need to use, click on the Information link in Views. It should give you a list of .tpl.php names you can use.

Just wanted to add another layer of complexity to the mix. :)

Cheers,

Mike
Avatar of glepiza
glepiza

ASKER

Hello guys, thank you for all your answers, I am trying all your solutions. This is sort of new to me, so it is taking me a little time to get it. But I will be back as soon as I finish, hopefully this afternoon.
Avatar of glepiza

ASKER

Sorry for my delay, it seems that php filter module is giving me this error:
Notice: Undefined index: value in theme_status_report(), so I am looking for the path to it.

As soon as I fix that error, I will be right back.
Avatar of glepiza

ASKER

ok, I think I am getting there.
I tried Arrow_1 suggestion, but I had to add another view field named Global PHP to add the php code. For some reason just adding that gave me many index errors, so  I removed that field and tried a tried a mix between Mike´s and Arrow_1´s solutions.
So what I did was to take views-view--og-members.tpl.php, place it on my theme template and use Arrows code. The code is actually working but the output is not what I am expecting.

If I write:
<?php
global $user;
if ($user->uid) {
      print $row->name;
} else {
  print '<span>anonymous</span>';
}
?>

It gives me: Notice: Undefined variable: row in include()

So I am not including the variables needed. How would I add the correct variables to the code?

thanks in advance,

Winter
Avatar of glepiza

ASKER

I tried using <?php print_r($variables); ?> to check the available variables for that template, but it seems it goes to an infinite loop and then I get a blank page.
Avatar of glepiza

ASKER

Attached goes a list of the available tpl.php templates I could use. Please let me know if I am using the wrong one. I really appreciate all your help guys.

HagayMandel:
I would like to use your solution and see if it works, but there for some reason enabling php code in input format (which I guess it means adding the PHP Global field) gives me this error:
Notice: Undefined property: stdClass::$views_php_8 in views_php_handler_field->render(). I have spend now hours trying to debug it for can´t find a patch. If you have a suggestion I would really appreciate it as well.
list-1.png
list-2.png
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of glepiza

ASKER

Thanks Mike, I appreciate it.

I ´ve been reading some views documentation and trying some examples to see if I can get the variables. So, when I added this   print '<pre>' . htmlentities(print_r($rows, 1)) . '</pre>';
to views-view--og-members.tpl.php I get all a bunch or markup, such as <span class="field-content"><a href="/messages/new/48?destination=node/6">Message</a></span>.

If I write  print_r($rows); I get the output of all the rows, I mean, I get each user information (Name, Address, Hobbies, Messages).

Do you know how can I do to print a single row? for example just the name?

I am going to use the Devel module from now on also. the next comments are going to be with dms. Thanks!
I could be wrong, but if you use the tpl.php file with the word "fields" in it you can pull out separate fields. It is something like $fields['field_name']->raw. I'm mobile at the moment so I can't look up the exact format. Be sure that if you use a raw field that has been entered by a user, you should run it through check_plain() or one of the sanitizing functions.

I just wanted to get back to you quickly so I'm sorry that this is an incomplete answer to your question.

Mike
Avatar of glepiza

ASKER

Ok, I am now using views-view-fields--og-members.tpl.php, but not sure if I am using the right file. Here is the array I get when typing print print_r(array_keys(get_defined_vars()), 1);

Array ( [0] => template_file [1] => variables [2] => view [3] => options [4] => row [5] => field_alias [6] => theme_hook_suggestion [7] => theme_hook_suggestions [8] => zebra [9] => id [10] => directory [11] => classes_array [12] => attributes_array [13] => title_attributes_array [14] => content_attributes_array [15] => title_prefix [16] => title_suffix [17] => user [18] => db_is_active [19] => is_admin [20] => logged_in [21] => is_front [22] => fields [23]


Do you know how could I print out fields?

Thanks
Avatar of glepiza

ASKER

ok, I wrote this:  echo $row->{$view->field['name']->field_alias}; and now  I am getting the username of my users. Getting there. I am trying to output everything.
Are you using the fields mode of views where you choose the fields you want output? I just want to make sure you are. I'm again mobile but should have some time later to look at this on a proper desktop machine.
Avatar of glepiza

ASKER

Hello, sorry I´ve been so absence. But now I am back and I am going to stay online for most of the day so if you guys are around I would really appreciate it any help.

Well, so, I am not pretty sure what do you mean with fields mode... could you please clarify me that? .

Thanks :)
Avatar of glepiza

ASKER

ok, let´s start over again. on my views-view-fields--og-members.tpl.php I have the following:
<?php

/**
 * @file
 * 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->wrapper_prefix: A complete wrapper containing the inline_html to use.
 *   - $field->wrapper_suffix: The closing tag for the wrapper.
 *   - $field->separator: an optional separator that may appear before a field.
 *   - $field->label: The wrap label text to use.
 *   - $field->label_html: The full HTML of the label to use including
 *     configured element type.
 * - $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->wrapper_prefix; ?>
    <?php print $field->label_html; ?>
    <?php print $field->content; ?>
  <?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>

that prints out all the members of a groups plus some of their info such as name, last name and privatemsg_link. what I would like to do is something like this:


<?php
 global $user;
 if ($user->uid) {
     //      here I would print the rows name, lastname and privatemsn link
 } else {
   //here I would print name, lastname and a link that redirects to registration page
 }
?>

could you please help me? I´ve been looking around for some examples, but I just don´t get them very well. Could you please help me?

Thanks again,

Winter
Avatar of glepiza

ASKER

Even though I have´t found a solution to my question, the answers provided really helped me to understand a little bit more how views work. thank!