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
glepizaAsked:
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.

Aaron FeledyDrupal Developer and ConsultantCommented:
You'll want to enable the php filter module (included with drupal core), then install the views php module which will allow you to set your input filter to "PHP code" when you are editing your custom text field in your view. This code will determine if the user is logged in or not and display the appropriate link or text. This isn't tested but should give you the right idea:

<?php
global $user;
if ($user->uid) {
  print '[privatemsg]';
} else {
  print '<span>Link Placeholder Text</span>';
}
?>

Open in new window

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
HagayMandelCommented:
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

0
junipllcCommented:
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
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

glepizaAuthor Commented:
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.
0
glepizaAuthor Commented:
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.
0
glepizaAuthor Commented:
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
0
glepizaAuthor Commented:
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.
0
glepizaAuthor Commented:
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
0
junipllcCommented:
It looks like you have the .tpl.php file right. I only have a few minutes so I can't look too in-depth into the stuff you posted above, but i see that you're trying to use print_r(). If you install the devel module you can use dpm(), dsm(), and kpr(). It will take the data structures and put them into a nice, JavaScript-driven, clickable interface right on your screen. For example, do kpr($variables).

It usually works. Not always, but usually.

I'll have more time tomorrow, hopefully, to look over what you have above.

Mike
0
glepizaAuthor Commented:
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!
0
junipllcCommented:
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
0
glepizaAuthor Commented:
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
0
glepizaAuthor Commented:
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.
0
junipllcCommented:
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.
0
glepizaAuthor Commented:
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 :)
0
glepizaAuthor Commented:
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
0
glepizaAuthor Commented:
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!
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.