sites_group_overrides-1.x-dev/modules/sites_group_media/sites_group_media.module
modules/sites_group_media/sites_group_media.module
<?php
/**
* @file
* Sites group media module.
*/
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\sites_group_media\Form\FileUploadForm;
use Drupal\sites_group_media\Form\OEmbedForm;
use Drupal\group\Entity\GroupInterface;
use Drupal\media\MediaInterface;
use Drupal\sites_group\Plugin\Site\GroupSiteInterface;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;
/**
* Implements hook_media_source_info_alter().
*/
function sites_group_media_media_source_info_alter(array &$sources) {
if (isset($sources['audio_file']['forms']['media_library_add'])) {
$sources['audio_file']['forms']['media_library_add'] = FileUploadForm::class;
}
if (isset($sources['file']['forms']['media_library_add'])) {
$sources['file']['forms']['media_library_add'] = FileUploadForm::class;
}
if (isset($sources['image']['forms']['media_library_add'])) {
$sources['image']['forms']['media_library_add'] = FileUploadForm::class;
}
if (isset($sources['video_file']['forms']['media_library_add'])) {
$sources['video_file']['forms']['media_library_add'] = FileUploadForm::class;
}
if (isset($sources['oembed:video']['forms']['media_library_add'])) {
$sources['oembed:video']['forms']['media_library_add'] = OEmbedForm::class;
}
}
/**
* Implements hook_views_query_alter().
*/
function sites_group_media_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
// When media_library is being used to add media to a node, we try to limit
// the available media to only those that belong to the site group the node
// belongs to.
if ($view->id() == 'media_library') {
/** @var \Drupal\sites\SiteInterface $site */
if ($site = \Drupal::service('sites.current_site')->getSiteFromContext()) {
if ($site instanceof GroupSiteInterface) {
$group = $site->getGroup();
}
}
if (!empty($group) && $group instanceof GroupInterface) {
// Get all media types (bundles).
$media_ids = [];
$bundle_info = \Drupal::service('entity_type.bundle.info')->getBundleInfo('media');
// Get group content for each media bundle.
foreach ($bundle_info as $key => $bundle) {
// @todo Figure out a better way to know which media types/bundles
// are enabled as group content so we don't have to try to load
// plugins that may not be enabled.
try {
$group_contents = $group->getRelatedEntities('group_media:' . $key);
} catch (PluginNotFoundException $exception) {
continue;
}
if (!empty($group_contents)) {
// Get id of each media.
foreach ($group_contents as $group_content) {
$media_ids[] = $group_content->id();
}
}
}
if ($uploaded_media_id = \Drupal::request()->query->get('media')) {
$media_ids[] = $uploaded_media_id;
}
// Restrict media to only those in group.
if (!empty($media_ids)) {
$media_content_data = $query->setWhereGroup('AND');
$query->addWhere($media_content_data, 'media_field_data.mid', $media_ids, 'IN');
}
else {
$media_content_data = $query->setWhereGroup('AND');
$query->addWhere($media_content_data, 'media_field_data.mid', NULL);
}
}
}
}
/**
* Implements hook_groupmedia_attach_group_alter().
*/
function sites_group_media_groupmedia_attach_group_alter(array &$result, MediaInterface $media, GroupInterface $group) {
if (\Drupal::routeMatch()->getRouteName() == 'layout_builder.overrides.node.view') {
$result[] = FALSE;
}
}
