SOReader
SOReader

Reputation: 6027

Filtering views content based on currently logged in user

I'm new to Drupal and don't really know what to look for.

I need to achieve behavior like this:

I don't want to use groups for this, because there will be like 100s of users and using groups and permissions will be really annoying.

EDIT Available context filters: enter image description here

Upvotes: 0

Views: 2419

Answers (2)

SOReader
SOReader

Reputation: 6027

I fixed it very nasty and ugly way. I've changed node.tpl.php of my theme to have <article> section render conditionally:

<?php 
    $renderContent = TRUE;
    $fieldname = 'field_fulltext_user_search';
    if (property_exists($node, $fieldname) && array_key_exists($node->language, $node->{$fieldname})) {
        $fieldValue = $node->{$fieldname}[$node->language][0]['value'];
        if (isset($fieldValue) && $fieldValue) {
            // check if content contains current user's name
            $safeBody = $body[0]['safe_value'];
            $theUser = $user->name;
            $renderContent = strpos($safeBody, $theUser)!==false;
        }
    }

    if ($renderContent):
?>

So now, when a node has field_fulltext_user_search field and its value is true then node content is searched for current user name. If it does not exist node is not rendered.

There're probably better solutions to this task but this worked for me fine. If you can think of better one, please let me know.

Upvotes: 0

Muhammad Reda
Muhammad Reda

Reputation: 27043

Follow the following steps:

  1. In your view settings page, under "Advanced" section, click to add new "Contextual argument".

  2. From the list of fields, choose the field that contains the "usernames" in your content type X.

  3. From the section "When the filter value is NOT available", choose "Provide default value" and for the type, choose "User ID from logged in user".

That would be all.

Upvotes: 2

Related Questions