You are here

function groupmedia_paragraphs_groupmedia_entity_group_alter in Group Media 8.2

Implements hook_groupmedia_entity_group_alter().

File

modules/groupmedia_paragraphs/groupmedia_paragraphs.module, line 27
Allows to associate media content from paragraphs to group.

Code

function groupmedia_paragraphs_groupmedia_entity_group_alter(&$groups, EntityInterface $entity) {
  if ($entity instanceof ParagraphInterface) {
    $parent = $entity
      ->getParentEntity();
    if (empty($parent)) {
      return;
    }

    // Check for nested paragraphs.
    // @TODO: Fix when the parent paragraph was not yet saved.
    while ($parent
      ->getEntityTypeId() == 'paragraph') {
      $parent = $parent
        ->getParentEntity();
      if (empty($parent)) {
        break;
      }
    }

    // We found parent that is not paragraph.
    if ($parent instanceof ContentEntityInterface) {

      // Get parent groups if any.
      $group_contents = \Drupal::entityTypeManager()
        ->getStorage('group_content')
        ->loadByEntity($parent);
      foreach ($group_contents as $group_content) {
        $groups[] = $group_content
          ->getGroup();
      }
    }

    // If parent entity was not yet added to a group, e.g. on group content
    // add form, content is saved before the group relation.
    if (empty($groups)) {

      // When this issue is resolved https://dgo.to/2475719
      // it would be possible to get the group id from the group_entity
      // collection like this:
      // $items = \Drupal::service('tempstore.private')->get('group_entity')
      // ->getAllUser();
      // and then loop through items and get the group id knowing the structure
      // of item key.
      // For now try to get the group id from route. Not reliable, but at
      // least something.
      $group = \Drupal::service('current_route_match')
        ->getParameter('group');
      if (!empty($group)) {
        $groups[] = $group;
      }
      else {

        // This could happen when creating the new content that still has no id,
        // parentEntity() method will return null, therefore let's try to get
        // the most top parent from route, if applicable.
        $parameters = \Drupal::service('current_route_match')
          ->getParameters();
        foreach ($parameters as $parameter) {

          // If paragraph belongs to group content.
          if ($parameter instanceof GroupContentInterface) {
            $groups[] = $parameter
              ->getGroup();
            break;
          }
          elseif ($parameter instanceof ContentEntityInterface) {

            // If paragraph belongs to node, media, taxonomy term, etc.
            $group_contents = \Drupal::entityTypeManager()
              ->getStorage('group_content')
              ->loadByEntity($parameter);
            foreach ($group_contents as $group_content) {
              $groups[] = $group_content
                ->getGroup();
            }
          }
        }
      }
    }
  }
}