Drupal - Display a block for a specific user group - drupal

I have about 6 different groups, eg Team A, Team B etc. I would like to display a block for one of these groups. Was thinking something like checking if the logged in users gid = X then display the block. That way I can setup 6 different blocks all targeting the different groups so that when the user is logged in, it will display the relevant block for them. Does that make sense? How would I implement it?

Depending on your exact setup, it looks like the Context module may help you.
Here's how you can do that.
Create your 6 separate blocks
Download and install the context module
Create a new context at admin/structure/context/add
Fill in the Conditions section based on one of my options below
Fill in the Reactions section, choose to add 'Blocks' and then select the exact block you want to show for the condition selected. You can show more than one, so add any that you want to appear.
Create a separate context for each of your groups (so 6 in total). You can show multiple blocks per each group.
Creating a new context allows you to show certain blocks for only CERTAIN CONTEXTS. Example contexts are showing blocks on only certain pages (via the Path context) or only for users of a certain role (via the User role context) or even on certain node types or on pages that have a certain term attached, etc.
In your case, if you are using the Organic Groups module to implement your user groups, context will integrate with that. That means that when you create your context, there will be an option under the 'Conditions' section to select the Organic Group you want to show certain blocks for. You choose the exact blocks you want to show in the 'Reaction' section.
Let us know if that helps!

After more than a week of research and playing around, I found a little bit of code and modified it below to what I needed.
<?php
global $user;
$uid = $user->uid;
$result = db_query ( "SELECT * FROM {og_membership}
WHERE etid = :uid
and entity_type = 'user'
order by gid DESC", array (':uid' => $uid ) );
foreach ( $result as $row ) {
$gid = $row->gid;
break;
}
?>
<?php if ($gid == "GROUP ID HERE"): ?>
(load block here)
<?php endif; ?>

Related

drupal 7 block restriction based on user term reference

Users are set up on my D7 site with various term references as profile fields.
I need to be able to show hide certain blocks based on these term references, is this possible?
I cant see a way to link a block with a term reference.
One way to achieve this is to enable php filter and set
Show block on specific pages -> Pages on which this PHP code returns TRUE (experts only)
in the block settings.
then you can access the user profile field terms of the logged in user like this
global $user;
$user = user_load($user->uid);
$term1 = $user->field_term1[LANGUAGE_NONE][0]['value']; //the value should be the tid of the term
$term2 = $user->field_term2[LANGUAGE_NONE][0]['value'];
then you can return true accordingly to control the visibility of the block.
for example
return $term1 == 15 //this is the tid of the term
The above code will show the block only to users with custom field term with tid = 15
Hope I am not confusing you.

Drupal 7 Lists the organic groups current user is a member

I want show in profile page the lists of organic groups that current user is a member.
By default if i call <?php print render($user_profile); ?> on my result i have one field with the list of my groups membership.
But I can not intercept the function or variable that generates this result.
How i can obtain this with drupal 7? thanks in advance!
You can use og_get_groups_by_user() to get group of current user.
$groups = og_get_groups_by_user($account);
This will return group IDs of all the groups this user is an approved member of.
I found the direct access for this
<?php print render ($user_profile['og_user_node']); ?>
Or we can also create a simple view block for profile page..the og module already provides these views by default.

Drupal Views Entity Reference Context

I'm running Drupal 7 with Entity Reference and Organic Groups. I have two content types, one of which is a group and the other is group content. I have an Entity Reference field (Select List) that references group content associated with the group.
I want to create a View that ONLY shows the value of the field that is selected from this Entity Reference field in the group content type.
For instance:
Team: Red Sox
Location: Fenway
Location is a content type (group content) and Team is the group. There are many teams and many locations but when I'm looking at the group page I want a View that ONLY shows a single location (the one SELECTED in the group content type).
After much research I realized that Views and context can't handle this on their own. I ended up using View PHP to construct a filter that effectively filtered out all OTHER results than the one that I wanted:
$node = menu_get_object();
$item = field_get_items('node', $node, 'field_name');
$loc = $item[0]['target_id'];
$refnode = node_load($loc);
$primary = $refnode->title;
if ($primary != $row->title) {
return TRUE;
}
You're welcome for this one :) If anybody has any better suggestions on how to code this feel free to comment or post alternate solutions.

Drupal 7 - Using Views Relationships With Filters

In Views, I have an exposed filter that looks at the UID (User ID / Author), but is there a way to limit this to "ONLY" the users who have posted in this content type?
I tried adding a "Content: Author" relationship and hit Apply. I'm not exactly sure why, but it wasn't until this point that I could go back into the relationships and see MORE options, like: "User: Content authored" (which must then be dependent on the first relationship?) so I selected that one too and set it up like so:
Now I was able to go to the exposed filter and select the relationship:
But this didn't work-- the exposed filter continues to show all the registered users.
I also tried putting in a User Reference field (to this content type) and attaching that to a relationship, but it didn't allow anything to show and gave this SQL warning:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_data_field_hidden_name.uid' in 'on clause'
How can I limit this author exposed filter to "ONLY" the users who have posted in this content type?
There are a few drupal modules that may help you out. Such as Corresponding node references
However, if you are good with php code, you can create the logic to query the database and return the desired results (which would be the columns of the table). Create a contextual filter, then select a field (any field should work, but best select something in either content type). Then edit it, WHEN THE FILTER VALUE IS NOT AVAILABLE -> Provide default value -> PHP Code
$nid = arg(1);// current node id
$query = "SELECT <desired_field_column_name> FROM {<field_data_table_name>} c
WHERE c.<column_that_is_same_as_nid> = :nid";
$result = db_query($query, array(':nid' =>$nid));
$id = array();
while ($row = $result->fetch())
{
array_push($id, $row->field_curator_target_id);//put each node id that's referenced in array
}
$separated = implode("+", $id); // separate them by + for AND , for OR
return $separated; //eg.32 + 30 would represent nodes that you want.
In Linked theme there is answer about Views Selective Filters (aka "Views Selective Exposed Filters", aka views_filters_selective, aka views_selective_filters).
This module adds filters that have suffix "(selective)" in their names – add one you need instead of filter w/o the suffix.

Drupal - Views. Setting a filter programmatically

I hope this is not a stupid question I have been searching for most of the day!
I have a Content Type (Documents) which simply contains a title, file and a category. The category value is required and is 'powered' by Taxonomy.
I now wish to create a view which will display these documents grouped and titled by the taxonomy term.
Using my limited Drupal knowledge I intent to iterate through the relevant terms IDs (using taxonomy_get_tree($vid)) and then render each view accordingly.
To do this I have been hoping to use this snippet.
view = views_get_view('documents');
$view->set_display($display_id);
$filter = $view->get_item($display_id, 'filter', 'field_dl_category');
$filter['value']['value'] = $filter_value;
$view->set_item($display_id, 'filter', 'field_dl_category', $filter);
$viewsoutput = $view->render();
But this is not working; when I query the value of the $filter ($view->get_item($display_id, 'filter', 'field_dl_category')) I get null returned.
Might this be that my filter name is not the same as the CCK field name?
I am using Drupal 7.
Any help much appreciated, I am running out of ideas (and time).
I finally managed to get this working but I took a slightly different approach.
I changed my view and added the relevant contextual filter and then used this function views_embed_view to get at my required results.
If this helps! this is my solution:
$display_id = 'default';
$vid = 7;
$terms = taxonomy_get_tree($vid);
foreach($terms As $term){
$content = views_embed_view('documents', $display_id, $term->tid);
//now we see if any content has been provided
if(trim($content) != ''){
print "<h3>" . $term->name . "</h3>";
print $content;
}
}
In my case the trim($content) returns '' with no data as the view template has been edited, this might not be the case for all.
I am a very new Drupal developer so I'm sure there are much better ways of doing this, if so please do post.
I am going to go ahead and assume that you want to show, using Views, a list of document nodes grouped by the category that they have been tagged with.
There are two (of maybe more) ways by which you can do this in Views 3:
(a) Choose a display style that allows you to select a Grouping field. (You could try the table style that ships with Views by default). Suppose you have properly related the node table to the taxonomy_term_data table through a Views relationship, you could choose taxonomy_term_data.name as the grouping field.
Note that this grouping is done before the view is just rendered. So, your query would just have to select a flat list of (content, tag) pairs.
(b) You could also make use of the Attachment display type to achieve something similar. Show the used categories first in a list view clicking on which will show a page (attachment) with all documents tagged in that chosen category.
To understand how to do (a) or (b), turn on the advanced_help module (which is not a Views requisite but is recommended) first.
For (a), read the section on Grouping in styles i.e. views/help/style-grouping.html and
For (b), read the section on Attachment display i.e. views/help/display-attachment.html
A couple of things about your approach:
(a) It will show all terms from that vocabulary irrespective of whether or not they were used to tag at least one document.
(b) views_embed_view() will return NULL even if the currently viewing user does not have access to the view. So, ensure that you catch that case.
Here's an alternative:
$view = views_get_view('view_machine_name');
$view->init_display('default');
$view->display_handler->display->display_options['filters']['your_filter_name']['default_value'] = 'your_value';
$view->is_cacheable = FALSE;
$view->execute();
print $view->render();
I know you can probably set this using some convoluted method and obviously that would be better. But if you just want a quick and dirty straight access without messing around this will get you there.

Resources