GroupMediaController.php in Group Media 8.2
File
src/Controller/GroupMediaController.php
View source
<?php
namespace Drupal\groupmedia\Controller;
use Drupal\group\Entity\Controller\GroupContentController;
use Drupal\group\Entity\GroupInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GroupMediaController extends GroupContentController {
protected $pluginManager;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->pluginManager = $container
->get('plugin.manager.group_content_enabler');
return $instance;
}
public function addPage(GroupInterface $group, $create_mode = FALSE) {
$build = parent::addPage($group, $create_mode);
if (!is_array($build)) {
return $build;
}
$storage_handler = $this->entityTypeManager
->getStorage('media_type');
foreach ($this
->addPageBundles($group, $create_mode) as $plugin_id => $bundle_name) {
if (!empty($build['#bundles'][$bundle_name])) {
$plugin = $group
->getGroupType()
->getContentPlugin($plugin_id);
$bundle_label = $storage_handler
->load($plugin
->getEntityBundle())
->label();
$t_args = [
'%media_type' => $bundle_label,
];
$description = $create_mode ? $this
->t('Create a media of type %media_type in the group.', $t_args) : $this
->t('Add an existing media of type %media_type to the group.', $t_args);
$build['#bundles'][$bundle_name]['label'] = $bundle_label;
$build['#bundles'][$bundle_name]['description'] = $description;
}
}
if (is_array($build['#bundles'])) {
uasort($build['#bundles'], function ($a, $b) {
return strnatcmp($a['label'], $b['label']);
});
}
return $build;
}
protected function addPageBundles(GroupInterface $group, $create_mode) {
$bundles = [];
$plugin_ids = $this->pluginManager
->getInstalledIds($group
->getGroupType());
foreach ($plugin_ids as $key => $plugin_id) {
if (strpos($plugin_id, 'group_media:') !== 0) {
unset($plugin_ids[$key]);
}
}
$storage = $this->entityTypeManager
->getStorage('group_content_type');
$properties = [
'group_type' => $group
->bundle(),
'content_plugin' => $plugin_ids,
];
foreach ($storage
->loadByProperties($properties) as $bundle => $group_content_type) {
$bundles[$group_content_type
->getContentPluginId()] = $bundle;
}
return $bundles;
}
}