groupmedia_paragraphs.module in Group Media 8.2
Allows to associate media content from paragraphs to group.
File
modules/groupmedia_paragraphs/groupmedia_paragraphs.moduleView source
<?php
/**
* @file
* Allows to associate media content from paragraphs to group.
*/
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\group\Entity\GroupContentInterface;
use Drupal\paragraphs\ParagraphInterface;
/**
* Implements hook_entity_insert().
*/
function groupmedia_paragraphs_entity_insert(EntityInterface $entity) {
// In addition to groupmedia_entity_insert react on paragraphs insert, because
// it can be related to parent entity that belongs to a group.
if ($entity instanceof ParagraphInterface) {
\Drupal::service('groupmedia.attach_group')
->attach($entity);
}
}
/**
* Implements hook_groupmedia_entity_group_alter().
*/
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();
}
}
}
}
}
}
}
Functions
Name![]() |
Description |
---|---|
groupmedia_paragraphs_entity_insert | Implements hook_entity_insert(). |
groupmedia_paragraphs_groupmedia_entity_group_alter | Implements hook_groupmedia_entity_group_alter(). |