Find out to which group the entity belongs to by route.

Last updated on
11 April 2023

Sometimes there is need to know to which group the current entity belongs to when you view the full page, e.g. media/10 . In one of the project I was working on, the content pages had different theme per group and that is why all the content items inside the group should also follow the theme styles.

I started with creating a service in custom module. In your_module.services.yml paste these lines:

services:
  your_module.current_group:
    class: Drupal\your_module\CurrentGroup
    arguments: ['@current_route_match', '@entity_type.manager']

Then create a class CurrentGroup in your_module/src folder. The class will have 2 methods:

/**
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *
   * @return bool|\Drupal\group\Entity\GroupInterface
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  public function getGroupByEntity(EntityInterface $entity) {
    $group = FALSE;
    if ($entity instanceof GroupInterface) {
      return $entity;
    }
    $entity_type = $entity->getEntityTypeId();
    // Load all the group content for this entity.
    /** @var \Drupal\group\Entity\GroupContent $group_content */
    $group_content = GroupContent::loadByEntity($entity);
    // Assuming that the content can be related only to 1 group.
    $group_content = reset($group_content);
    if (!empty($group_content)) {
      $group = $group_content->getGroup();
    }
    return $group;
  }

  /**
   * Get the group from the current route match.
   *
   * @return bool|\Drupal\group\Entity\GroupInterface
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  public function getGroupFromRoute() {
    $entity = FALSE;
    $parameters = $this->routeMatch->getParameters()->all();
    if (!empty($parameters['group']) && is_numeric($parameters['group'])) {
      $group = Group::load($parameters['group']);
      return $group;
    }
    if (!empty($parameters)) {
      foreach ($parameters as $parameter) {
        if ($parameter instanceof EntityInterface) {
          $entity = $parameter;
          break;
        }
      }
    }
    if ($entity) {
      return $this->getGroupByEntity($entity);
    }
    return FALSE;
  }

In getGroupByEntity there is 'your_group_type' in $group_content_type variable. Substitute it with the desired group type or $group_content_type could be also an array that will list the content types for all available group types.

Then on hook_preprocess_page(), for example, you can call the service to find the current group and then load the styles you need.

/**
 * Implements hook_preprocess_page().
 *
 * @param $variables
 */
function your_module_preprocess_page(&$variables) {
  $group = \Drupal::service('your_module.current_group')->getGroupFromRoute();
  // Some magic function to get the library name for your styles.
  $styles_per_group = _your_module_get_styles_per_group($group);
  $library = 'your_module/' . $styles_per_group;
  $variables['#attached']['library'][] = $library;
}

Help improve this page

Page status: No known problems

You can: