Thank you for such a wonderful module. It enabled me to get my own custom entity/custom module up and running very successfully in July 2016. ('Group' also basically serves as the best tutorial for how to write custom Drupal 7 modules that I've ever seen -- I used to use 'field collection' for this purpose, but not anymore.)

When pushing quickly to go live with my code in July, I didn't fully understand the ways that Group allows developers to programmatically hook into it, so at the time, all I did to relate my new module table "Foobar" to the Group tables was to simply do it fully in SQL, using a relational pointer. In my implementation of hook_schema() I simply added the 'gid' (group id), and then in my submit handler where I validate my user's interactions with my new entity, I simply add the group id of the logged-in user to my new entity's data using an entity_create() statement.

However, now I've inevitably realized that I didn't bother to override the Entity API delete method when I wrote my EntityAPIController class, so that when Group 'X' is deleted, my instances of my new entity 'Foobar' that are related to Group 'X' are not also deleted.

I think mostly in MySQL, so I pretty much already knew this was going to be a problem when I did a SELECT * FROM group_entity and got NO results. You can't delete something that isn't there!

To fix this, I looked at how you did this for nodes in GNode, and I looked at how group.controller.inc deletes a group, and came up with the following four steps to fix my mistake:

  1. As just mentioned, when my new class FoobarEntityController extends EntityAPIController, override the delete method.

  2. Using the crucial comment you made here, I found it necessary to modify my hook_entity_info() to include a group entity key, $info['foobar']['group entity'] = 'single'; which was not immediately obvious until I found this comment.

  3. In my aforementioned submit handler, do a group_load($gid) and then add the line $group->addEntity($foobar_id, 'foobar_name', 'foobar_name').

    This fixed everything for all future interactions between users belonging to these groups and my new entity. To fix all past instances, so that any old ones can be deleted later, I had to add a fourth step,

  4. Go to the MySQL command line and execute the following statements:
    • INSERT INTO group_entity (gid, entity_id) SELECT gid, foobar_id FROM foobar_table;
    • UPDATE group_entity SET entity_type="foobar_name", bundle="foobar_name";

Questions:

  1. I tend to think best in plain MySQL on the command line, so that worked best and fastest for me, but would there have been some other, more "Group-y" or "Drupal-y" way to do step #4 (SQL commands)?

  2. Is all of this documented somewhere? I didn't see anything in the Development Guide by Pierre et al. and I don't know if that would be an appropriate place to add something like this, or whether this use case among Developers is too small to bother documenting -- however, I know that it took me quite a while to work it out on my own, and do wonder if writing some of this down might help someone else.

  3. Looking at Get Group's Content, and seeing getEntities(), and staring at my (formerly) empty group_entity table in MySQL, I realized that the way I related my new entity's information to a group was pretty sad. Instead of ever using getEntities, instead I just added a path to my hook_menu_alter() like $items['group/%group/foobar'] = array('page callback' => 'foobar_group'); and then used some Group methods like $group->userHasRole() in my callback to determine what appropriate content to show in the tab. Would it be much more efficient to rewrite this using getEntities()?

  4. In your comments above your addEntity() function, you say that (presumably instead of all the steps I did above), you could instead "manipulate" the group property on the entity you wish to add: You could also manipulate the 'group' property on the entity you wish to add. Upon saving that entity, it will automatically be added to the group.
  5. I have no idea what you mean when you use the word "manipulate" or when you say that "upon saving that entity, it will be automatically added to the group" -- because nothing I did was in any way "automatic". Did I just do a whole lot of over-thinking again, or is there some other really obvious thing I am missing here?

Comments

chickenofeathers created an issue. See original summary.

cmah’s picture

Issue summary: View changes
cmah’s picture

Issue summary: View changes
cmah’s picture

Issue summary: View changes
cmah’s picture

Issue summary: View changes
cmah’s picture

Issue summary: View changes
Soul88’s picture

Status: Active » Closed (outdated)

We thank everyone for their collaboration on this issue, but as the D7 version is no longer supported, we will now close all D7 issues to keep the issue queue a bit tidier. This information won't go anywhere, it just won't show up on the list of open issues anymore.

Please see: https://www.drupal.org/project/group/issues/3163655 and https://www.drupal.org/project/group/issues/3203863#comment-14100281 for more details.