I use the Group module and I have created several groups and assigned several nodes as content of these groups.

For example, I know id of some node. How can I figure out which groups contain this node ? That is, how to get group ids by nid, if this node is a content of these groups ? And I don't want to use SQL for it. Are there methods of classes for it ?

Comments

roman-yrv created an issue. See original summary.

drupalfan2’s picture

Try this in your theme file mytheme.theme to get the group id and group name related to some node:

$node_id = $variables['node']->id();

$ids = \Drupal::entityQuery('group_content')
    ->condition('entity_id', $node_id)
    ->execute();

$relations = \Drupal\group\Entity\GroupContent::loadMultiple($ids);
foreach ($relations as $rel) {
  if ($rel->getEntity()->getEntityTypeId() == 'node') {
    $group_ids[] = $rel->getGroup()->id();
    $group_lables[] = $rel->getGroup()->label();
  }
}   
$first_group_id = $group_ids[0];
$first_group_label = $group_lables[0];
maaty388’s picture

Status: Active » Closed (works as designed)
vijayan08’s picture

Hi drupalfan2,
#2 its working fine , as I expected