In many big structures, we have often to confront ourselves to the question of the hierarchy. Then, some specific needs will be asked, for instance :

“How can the HR can publish specific documentation that are not allowed to other ?”.
- No problem, we have a wonderful module called Organic Groups (OG) that handles this easily, where you can divide up whatever content you need in specific groups. Just create a HR group and put your documents in there.
- Well, perfect, but in our organisation HR is not a simple “group”, we have a whole hierarchy some of these documents will be allowed for board only although others could be viewed by every HR services.

With this basic example, you might understand that with OG only, you have to create different layers of groups which can easily be messy when you deal with a wide hierarchy.

--> The solution comes then with Group Audience field in OG + OG Subgroups module that already existed in Drupal 6 and has been ported in Drupal 7.

In this tutorial we are going to see:

  • How to create several groups and organize them in a tree
  • Create a content type and put it in a group
  • Use Entity Reference Tree Module to have a nice tree view of Group Audience
  • Specify for the content type that it can belong only to a single group type
  • Use OG subgroup to spread the “read-only” rights to all children (parents, or both)

Create Groups and Documents based on hierarchy

Watch the video!

Step 1: Install Drupal and the following modules (Drupal 7.9 is used in this tutorial)

- OG (1.3 is used in this tutorial) + OG access control + OG UI
- Entity API (OG dependency)
- Features (Not necessary, but very convenient to manage content types)
- Entity Reference Tree
- OG Tree Views (Deprecate - Not usefull anymore)

Normally it will ask to rebuild permissions, if not go to admin/reports/status/rebuild

Step 2 : Create a new OG Group content type (called “Unit” in this tutorial), that is also a Group content and change the Groups audience widget to “Tree view field”

Step 3 : Create several units that will belong one to another (the idea is to create a tree)

Step 4 : Create a new role called “contributor” and several users that you will attach to this role

Step 5 : Choose randomly some Units where you will put these Users (Ordinarily a user belongs to a single Unit)

Step 6 : Create a new content type (called “Document” in this tutorial), set it as Group content and change the Groups audience widget to “Tree view field”

Step 7 : Go to admin/config/group/fields and add to Document the “Group visibility” field

Step 8 : Create a new Feature called “Document” where you add the Document content type, save it, add it to the module folder.

Step 9 : Add the following lines to document.module, if you wish to restrict your user to choose a group audience where they belong. Then activate your new Document Feature.

function document_form_document_node_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  $account = user_load($user->uid);
  $i = 0;
  $allowed_ids = array();
  while (isset($account->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][$i]['gid'])) {
    $group = og_load($account->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][$i]['gid']);
    $allowed_ids[] = $group->etid;
    $i++;
  }
  $form['group_audience'][LANGUAGE_NONE]['#allowed_ids'] = $allowed_ids;
}

Allow read-only propagation based on the hierarchy

Watch the video!

Step 1 : OG Subgroups + custom patch provided

Step 2 : Go to admin/config/group/subgroups and select Document Group content.

Step 3 : Go to admin/config/group/fields and add to Document the “Subgroup direction” field.

Step 4 : Create a new Document and choose the subgroup direction wished and choose visibility “private”

Step 5 : Test it with the users you previously created, you will see that only the users belonging to the group and the one below and/or above (depending on the direction chosen, will be able to display the document

Restrict the group audience to only a single group type

Step 1 : Create a new group content type (called “Projet” in this tutorial)

Step 2 : Add the following lines to document.module, if you wish to restrict the document only to “Unit” groups. If you already add a the document_form_document_node_form_alter mentioned above, you may only need to add line $bundle_name = 'unit'; the if condition and unset function.

function document_form_document_node_form_alter (&$form, &$form_state, $form_id) {
  $i = 0;
  $bundle_name = 'unit';
  while (isset($form['group_audience'][LANGUAGE_NONE]['#allowed_bundles'][$i])) {
    if ($form['group_audience'][LANGUAGE_NONE]['#allowed_bundles'][$i]['bundle_name'] <> $bundle_name)
      unset ($form['group_audience'][LANGUAGE_NONE]['#allowed_bundles'][$i]);
    $i++;
  }
}

Comments

andrés chandía’s picture

I only was able to arrive to the download step because when I enable the "OG Tree Views" I only get a blank page, then I reload and the module never become enabled.

Could this have to do with the previous og_subgroups installations?

Any help?

@ch

lahode’s picture

Hello Andrés,

You're right for OG subgroups, I gave the wrong patch. I updated the link to it, it should work, I tested it.

As for the use case shown above, I attached a video that might help you to make it work

Cheers

Fredrik Lahode
Développeur et Formateur web indépendant

andrés chandía’s picture

Ok, thanks, now it works all right, but I see now that may be this is too much for what I need, I better explain what I need:

Create masters groups wich have subgroups inside.
If I assign user1 as admin of mastergroup1 I would like that user to became admin of subgroupA and subgroupB under mastergroup1

Also, if it needs, the ability of a user in mastergroup1 to publish any content that should appear in mastergroup1, subgroupA and subgroupB; or only in subgroupB if I need it that way.

The ability of a user in subgroupA to publish any content that could propagate either to subgroupB or to mastergroup1, or both if he has permission to.

I hope I explained myself.

By the way link to video tutorial is not working!

Thanks.

@ch

lahode’s picture

Hi,

there are several points you are mentioning:
1. The present version of this module manages only the display of the nodes. OG_ACCESS_AUTHENTICATED_REALM is used for that in og.module. The problem is that if you want that user1 becomes an admin of a subgroup, he should normally belong to the group and also be member to the group.
2. Seems to be related to the same problematic
3. Version of D6 used to manage siblings, the present module only manahe children and parents, but why not propose a new patch

For the video, thx but it seems to work, I even tried them from my smartphone

Cheers

Fredrik Lahode
Développeur et Formateur web indépendant

lahode’s picture

I haphazardly found the following link that might interest you : http://drupal.org/node/1317164

Maybe a lead to enhance OG subgroups...

Fredrik Lahode
Développeur et Formateur web indépendant

andrés chandía’s picture

Thanks for deriving me to that link, but also related with that, I am looking a solution for a kind of inverse situation.
I explain myself: I need a way that the users of groups (members) that can add content inside their groups, can not add content to the general site. I mean, when you create a user, that user has to be authenticated (at least). To give permission to create content to the members inside the group you have to give permission to the authenticated user to create content in the general site environment, so the member that is enabled to create group content is also enabled to create site content. So what I'm looking for is the way that the member that can create group content can not create site content by default.

Thanks again.

@ch

lahode’s picture

Well, I'm not sure if I understood your purpose but this deals more with general user access. In facy you should create roles with different access and give only access to your user belonging to a group to create a specific content type. I you then make the field group audience mandarory with a standard form alter, you should get what you're looking for.

Fredrik Lahode
Développeur et Formateur web indépendant

andrés chandía’s picture

Thanks, I haven't thought in that, "make the field group audience mandatory", yes I have already created a rol for this purpose, but I was lacking something, the key you just gave me.

Thanks again.

@ch

Vandborg’s picture

I tried to follow your video tutorial. But when I get to the step to create several Unit's I get this error from your custom module. And I can't seem to fix it myself, and it doesn't seem to that anyother have the same problem. I use Drupal 7.10.

Notice: Trying to get property of non-object in _entity_reference_tree_get_options() (line 773 of /var/www/html/drupal7/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_get_options() (line 773 of /var/www/html/drupal7/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Warning: Attempt to modify property of non-object in _entity_reference_tree_get_options() (line 774 of /var/www/html/drupal7/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_level() (line 966 of /var/www/html/drupal7/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_item() (line 872 of /var/www/html/drupal7/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_item() (line 880 of /var/www/html/drupal7/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_item() (line 881 of /var/www/html/drupal7/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_item() (line 883 of /var/www/html/drupal7/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_item() (line 885 of /var/www/html/drupal7/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_item() (line 914 of /var/www/html/drupal7/sites/all/modules/entity_reference_tree/entity_reference_tree.module).

And it's not possible to do this with a drupal 6 right? I would prefer drupal 6, because I already made a lot of my Intranet with Drupal 6.

Vandborg’s picture

Hi agian

I just tried with a Drupal 7.9, I get the same error. :/

lahode’s picture

I made some correction on Entity References Tree and on OG Tree Views. Latest version should fix the problem.

Please give me your feedback

Cheers

Fredrik Lahode
Développeur et Formateur web indépendant

Vandborg’s picture

I just tried it out you correction on a new install I still get this error:

Notice: Trying to get property of non-object in _entity_reference_tree_get_options() (line 779 of /home/www/net2.frise.dk/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_get_options() (line 779 of /home/www/net2.frise.dk/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_level() (line 976 of /home/www/net2.frise.dk/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_item() (line 882 of /home/www/net2.frise.dk/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_item() (line 890 of /home/www/net2.frise.dk/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_item() (line 891 of /home/www/net2.frise.dk/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_item() (line 893 of /home/www/net2.frise.dk/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_item() (line 895 of /home/www/net2.frise.dk/sites/all/modules/entity_reference_tree/entity_reference_tree.module).
Notice: Trying to get property of non-object in _entity_reference_tree_build_item() (line 924 of /home/www/net2.frise.dk/sites/all/modules/entity_reference_tree/entity_reference_tree.module).

And I've diffed your changes and found them in the module I uploaded to my drupal installation, and it's the newest version :/

Vandborg’s picture

Damn.. I just found out the OG Tree Views integration module wasn't activated. I couldn't do the same time as OG-Subgroups, because it redeclare a function from there.

Do I have to patch OG-subgroups to complete the first part of the tutorial? Because I didn't get that.

Vandborg’s picture

I've been emailing with Lahode. And here is what I wrote to him. :)

I've found out that OG Tree Views, wasn't activated. (I did activate it) But I wasn't told it did succes. So I tried a lot of time til activate it, where it finally gave me an error. Where it told me it couldn't activate because OG Tree Views redeclared an function, from OG Subgroups. So after reading a bit more about you patches. I found out your patches for OG Subgroups fixed this. And now it's working. :)

simzack’s picture

Yeah, this patch is perfect, issue solved, thank you !

lahode’s picture

Unfortunately my example was made only for D7, working with entities. However OG subgroups for D6 is quite elaborated and maybe you can do some similar stuff with it

Fredrik Lahode
Développeur et Formateur web indépendant

seaji’s picture

Entity Reference Tree module is emty.
Switching from git master branch to 7.x-1.x no help.

seaji’s picture

After installing Entity Reference Tree and OG Tree Views I have broken views for OG content.
Error message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'og_membership_node.group' in 'where clause'"

lahode’s picture

Hi,

What version of OG are you using? My modules have been developed on the base of OG 7.x.1.x module.

Fredrik Lahode
Développeur et Formateur web indépendant

seaji’s picture

Hi
I'm using OG 7.x-1.3
All my errors disappeared after I deleted my custom membership type.
It's all magic...

Ale.bcn’s picture

Hi Lahode,

I've tried to watch the tutorials but I've got:

Not Found

The requested URL /drupal/video/step2.html was not found on this server.

Could you please check it?

thanks for the tutorial! :)

memoo’s picture

Links to video are broken.

lahode’s picture

Sorry pals,

I migrated all my site and didn't notice the errors.
I checked it should work now

As of my 2 modules, I'm not sure if the I posted the last version on the sandbox, I got bored trying to release them officially, so don't hesitate to write me again if nothing works

Cheers

Fredrik Lahode
Développeur et Formateur web indépendant

cpaulsen’s picture

Was wondering what the status of this is - I found the first patch but the second seems to be empty.

Anyone know if this has been incorporated elsewhere?

Thanks,
Clem Paulsen

lahode’s picture

Hi cpaulsen,

I didn't review my whole post, because it was done with OG 7.1

However, I took time to refresh https://drupal.org/sandbox/lahode/1325398 (hoping it will be promoted this time).

Have a nice day

Fredrik Lahode
Développeur et Formateur web indépendant