public function AttachMediaToGroup::assignMediaToGroups in Group Media 8.2
Assign media items to groups.
Parameters
\Drupal\media\MediaInterface[] $items: List of media items to assign.
\Drupal\group\Entity\GroupInterface[] $groups: List of group to assign media.
bool $check: Flag whether to check assignment conditions.
1 call to AttachMediaToGroup::assignMediaToGroups()
- AttachMediaToGroup::attach in src/
AttachMediaToGroup.php - Attach media items from given entity to the same group(s).
File
- src/
AttachMediaToGroup.php, line 124
Class
- AttachMediaToGroup
- Class AttachMediaToGroup.
Namespace
Drupal\groupmediaCode
public function assignMediaToGroups(array $items, array $groups, $check = TRUE) {
$plugins_by_group_type = [];
// Get the list of installed group content instance IDs.
$group_content_instance_ids = $this->groupEnabler
->getInstalled()
->getInstanceIds();
/** @var \Drupal\media\MediaInterface $item */
foreach ($items as $item) {
// Build the instance ID.
$instance_id = 'group_media:' . $item
->bundle();
// Check if this media type should be group content or not.
if (in_array($instance_id, $group_content_instance_ids)) {
// Check what relations already exist for this media to control the
// group cardinality.
$group_contents = $this->groupContentStorage
->loadByEntity($item);
$group_ids = [];
/** @var \Drupal\group\Entity\GroupContentInterface $instance */
foreach ($group_contents as $instance) {
$group_ids[] = $instance
->getGroup()
->id();
}
$group_count = count(array_unique($group_ids));
foreach ($groups as $group) {
if ($check && !$this
->shouldBeAttached($item, $group)) {
$this->logger
->debug($this
->t('Media @label (@id) was not assigned to any group because of hook results', [
'@label' => $item
->label(),
'@id' => $item
->id(),
]));
continue;
}
if (!isset($plugins_by_group_type[$group
->bundle()])) {
$plugins_by_group_type[$group
->bundle()] = $this->groupEnabler
->getInstalled($group
->getGroupType());
}
// Check if the group type supports the plugin of type $instance_id.
if ($plugins_by_group_type[$group
->bundle()]
->has($instance_id)) {
$plugin = $plugins_by_group_type[$group
->bundle()]
->get($instance_id);
$group_cardinality = $plugin
->getGroupCardinality();
// Check if group cardinality still allows to create relation.
if ($group_cardinality == 0 || $group_count < $group_cardinality) {
$group_relations = $group
->getContentByEntityId($instance_id, $item
->id());
$entity_cardinality = $plugin
->getEntityCardinality();
// Add this media as group content if cardinality allows.
if ($entity_cardinality == 0 || count($group_relations) < $plugin
->getEntityCardinality()) {
$group
->addContent($item, $instance_id);
}
else {
$this->logger
->debug($this
->t('Media @label (@id) was not assigned to group @group_label because max entity cardinality was reached', [
'@label' => $item
->label(),
'@id' => $item
->id(),
'@group_label' => $group
->label(),
]));
}
}
else {
$this->logger
->debug($this
->t('Media @label (@id) was not assigned to group @group_label because max group cardinality was reached', [
'@label' => $item
->label(),
'@id' => $item
->id(),
'@group_label' => $group
->label(),
]));
}
}
}
}
else {
$this->logger
->debug($this
->t('Media @label (@id) was not assigned to any group because its bundle (@name) is not enabled in any group', [
'@label' => $item
->label(),
'@id' => $item
->id(),
'@name' => $item->bundle->entity
->label(),
]));
}
}
}