0

Problem: I have a user directory that anyone can access, with pages for each user. However, there is a view (that's a block) with personal info that I only want shown to the user who's page it is, or users with an admin role. They are not the authors of the page. I know how to fix the admin role issue, and how to get the username using php, but not how to get the field containing the username of the person who owns the page. I can't use Views PHP, just Views. Thanks!

EDIT: I tried doing something where in the view's header I had

if ($current_user == "[field_user_name]") { ?>

Then in the footer of the view:

<?php } ?>

But that hasn't worked.

6
  • Which version of Drupal are you using? Nov 25 '16 at 22:29
  • @ChrisHappy Drupal 7
    – theEpsilon
    Nov 25 '16 at 22:39
  • Would you mind using jQuery? I have a hackish idea right now... Nov 25 '16 at 22:40
  • @ChrisHappy no it's got to be server side so that it's secure
    – theEpsilon
    Nov 25 '16 at 22:52
  • Okay, there goes the front developer plan.... Researching... Nov 25 '16 at 22:59
1

Okay, I think I got it now. Already tested.

  1. Override the view's template (Tutorial)
  2. Include global $user; somewhere in the first php tag.
  3. Use an if statement: <?php if (strip_tags($view->render_field("YOUR_FIELD", $id)) == $user->name): ?> with YOUR_FIELD as the field's replacement pattern without brackets. (remember the <?php endif; ?>)

Sample view template (for unformated view display):

<?php

/**
 * @file
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */
global $user;
?>
<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <?php if (strip_tags($view->render_field("title", $id)) == $user->name): ?>
    <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>
       <?php print $row; ?>
    </div>
  <?php endif; ?>
<?php endforeach; ?>
0

The Rules block visibility module may possibly be an alternative. Here is a quote from its project page:

The Rules block visibility module allows Rules components to be used to control block visibility. This provides Drupal administrators and developers extreme flexibility in controlling when blocks should be displayed on their websites, in addition to the default visibility options provided by Drupal.

The general idea is that if you can do it with Rules, you can use it to control block visibility, so the possibilities are limitless.

Need to show a block only for users registered more than a month ago?

Perhaps you have a block that must be shown only between 8am-5pm on weekdays?

What about displaying or hiding a block based on current weather conditions?

All of this can be done by using Rules block visibility.

Moreover, as per the "if you can do it with Rules, you can use it to control block visibility" above, you have the power of Rules available to implement all sorts of custom logic (cfr. the "programmatically / custom module" part of your.

For an illustration of how to use this module, refer to my answer to "How to stop a Views block from displaying to admins, such as user/1?".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.