Skip to content

Instantly share code, notes, and snippets.

@elizoller
Last active April 3, 2023 15:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elizoller/9d0135c4c122cbec20c72e68a95ac8d8 to your computer and use it in GitHub Desktop.
Save elizoller/9d0135c4c122cbec20c72e68a95ac8d8 to your computer and use it in GitHub Desktop.
Group module usage. I'm sure this isn't all applicable to everyone. But basically its designed to create a group for a collection when one is created. When an item is created or updated, it should update the group it belongs to.
<?php
use Drupal\group\Entity\Group;
/**
* Implements hook_node_insert().
*
* Alternatively could trigger this with a context reaction.
*/
function asu_permissions_node_insert(NodeInterface $node) {
if ($node->bundle() == "collection") {
// Create a new group for the collection.
$pluginId = 'group_node:' . $node->bundle();
$col_group = Group::create(['type' => 'collection_group', 'label' => 'Collection ' . $node->id() . ' Group']);
$col_group->save();
$col_group->addContent($node, $pluginId);
$col_group->save();
}
elseif ($node->bundle() == 'asu_repository_item') {
// Add the asu_repository_item to the parent collection's group.
// We are assumming here one member_of relationship.
$parent = _get_collection_parent($node);
if ($parent) {
$pluginId = 'group_node:' . $node->bundle();
$group_contents = _get_group($parent);
if (!empty($group_contents)) {
foreach ($group_contents as $group_content) {
/** @var \Drupal\group\Entity\GroupContentInterface $group_content */
$group = $group_content->getGroup();
$group->addContent($node, $pluginId);
}
}
}
}
}
/**
* implemements hook_node_update
*/
function asu_permissions_node_update(NodeInterface $node) {
if ($node != NULL && $node->bundle() == 'asu_repository_item') {
$utils = \Drupal::service('islandora.utils');
if (!$utils->haveFieldsChanged($node, $node->original)) {
return;
}
if ($node->get('field_member_of')->entity && $node->original->get('field_member_of')->entity) {
if ($node->get('field_member_of')->entity->id() != $node->original->get('field_member_of')->entity->id()) {
_remove_object_from_parent_group($node, $node->original->get('field_member_of')->entity);
_add_object_to_parent_group($node);
}
}
elseif (!$node->get('field_member_of')->entity && $node->original->get('field_member_of')->entity) {
_remove_object_from_parent_group($node, $node->original->get('field_member_of')->entity);
}
elseif ($node->get('field_member_of')->entity && !$node->original->get('field_member_of')->entity) {
_add_object_to_parent_group($node);
}
}
}
/**
* Removes an entity from a collection group.
*/
function _remove_object_from_parent_group($entity, $parent) {
$group_contents = _get_group($entity);
if (empty($group_contents)) {
return;
}
foreach ($group_contents as $group_content) {
/** @var \Drupal\group\Entity\GroupContentInterface $group_content */
$group = $group_content->getGroup();
if ($group->label() == 'Collection ' . $parent->id() . ' Group') {
$group_content->delete();
}
}
}
/**
* Add object to group.
*/
function _add_object_to_parent_group($node) {
$pluginId = 'group_node:' . $node->bundle();
$group_contents = _get_group($node->get('field_member_of')->entity);
// Check if the node is already in the group.
$node_group_contents = _get_group($node);
if (!empty($node_group_contents)) {
foreach ($node_group_contents as $gc) {
$group = $gc->getGroup();
if ($group->label() == 'Collection ' . $node->get('field_member_of')->entity->id() . ' Group') {
return;
}
}
}
if (!empty($group_contents)) {
foreach ($group_contents as $group_content) {
/** @var \Drupal\group\Entity\GroupContentInterface $group_content */
$group = $group_content->getGroup();
$group->addContent($node, $pluginId);
}
}
}
/**
* Gets the groups that an entity belongs to.
*/
function _get_group($entity) {
$group_contents = \Drupal::entityTypeManager()
->getStorage('group_content')
->loadByEntity($entity);
return $group_contents;
}
/**
* Gets the collection parent of an entity.
*/
function _get_collection_parent($entity) {
$parent = $entity->field_member_of->entity;
if ($parent) {
if ($parent->bundle() != "collection") {
$parent = _get_collection_parent($parent);
}
return $parent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment