Targeted Content Display with Taxonomy and Roles

Here's the situation: you've got a Drupal-based website with multiple target audiences.  You've also got a dashboard with content targeted at the authenticated user.  Not all of your target audiences authenticate so you need a way to display relevant news items to both authenticated and non-authenticated users.  Here's a little recipe to do exactly that.

First you create a news content type.  Then you create a taxonomy vocabulary with items for each target audience (e.g. students, staff, faculty, patients, alumni, etc.) and assign it to the news content type.  It's easy to create a page display in Views to show a list of news item teasers filtered on target audience.  But what if you want to create a dashboard page that shows news items targeted only to the authenticated user?  What if there are news items that should be visible only to certain roles and not others?

Requirements:

  • Tag item with a Target Audience(s)
  • Select whether item is visible for all users or just the target audience (access control)
  • Display block of latest news items on front page with link to more news page
  • Display page of news items
  • Display news block on Dashboard page filtered to only show items relevant to authenticated user

Modules:

Vocabularies:

  • Target Audience
    • Faculty
    • Staff
    • Students
    • Patients
  • Visibility
    • Everyone
    • Target audience only

Roles:

  • Contributor
  • Faculty
  • Staff
  • Students

[Note: Role names and taxonomy terms must match exactly for this method to work.]

TAC Lite Configuration:

The following will allow you to set up TAC Lite permissions that will allow taxonomy based access to news items.  With this configuration, items tagged for a specific target audience will always be visible to the coinciding role.  News items tagged as visible to Everyone will be available to the anonymous and authenticated user roles.  Content contributors will be able to view, edit and delete all content regardless of target audience.

Settings:

  • Enable both “Target Audience” and “Visibility” vocabularies
  • Create 2 schemes

Scheme 1:

  • Permissions: View
  • Anonymous user
    • Visibility: Everyone
  • Authenticated user
    • Visibility: Everyone
  • Faculty
    • Target Audience: Faculty
  • Staff
    • Target Audience: Staff
  • Students
    • Target Audience: Students

Scheme 2:

  • Permissions: View, update, delete
  • Contributor:
    • Visibility: Everyone, Target audience only
    • Target Audience: [all terms]

Views:

Now let's create a view to display news items targeted to the authenticated user:

  • Create a node view called “targeted_news”
  • Configure defaults to create a list of news nodes
  • Create block display for front page
  • Create page display for news page
  • Create page display for Dashboard
  • Here’s the magic:
    • Create a Taxonomy: Term ID argument
    • Select to provide a default argument of type PHP code
    • Drop in this code and validate against Taxonomy term > Target Audience vocabulary > Term ID:
    global $user;
    $terms = "";
    foreach($user->roles as $role){
      $tid = db_result(db_query("SELECT tid FROM term_data WHERE name = '%s'", $role));
      if ($terms == "")
        $terms .= $tid;
      else
        $terms .= "+" . $tid;
    }
    if ($terms !== "") {
      return $terms;
    }

This little code snippet will match taxonomy terms to roles and return a string of term ID's in the form "1+2+3".  The string is then validated and passed as a Term ID arguement.  Thus you get a list of news items targeted specifically at the authenticated user based on their role(s).

0
No votes yet
Your rating: None