OpignoModuleGroupController.php in Opigno module 3.x
File
modules/opigno_module_group/src/Controller/OpignoModuleGroupController.php
View source
<?php
namespace Drupal\opigno_module_group\Controller;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\gnode\Controller\GroupNodeController;
use Drupal\group\Entity\GroupInterface;
use Drupal\group\Plugin\GroupContentEnablerManagerInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OpignoModuleGroupController extends GroupNodeController {
protected $pluginManager;
public function __construct(GroupContentEnablerManagerInterface $plugin_manager, PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager, EntityFormBuilderInterface $entity_form_builder, RendererInterface $renderer) {
parent::__construct($temp_store_factory, $entity_type_manager, $entity_form_builder, $renderer);
$this->pluginManager = $plugin_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.group_content_enabler'), $container
->get('tempstore.private'), $container
->get('entity_type.manager'), $container
->get('entity.form_builder'), $container
->get('renderer'));
}
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('node_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 = [
'%node_type' => $bundle_label,
];
$description = $create_mode ? $this
->t('Create a node of type %node_type in the group.', $t_args) : $this
->t('Add an existing node of type %node_type to the group.', $t_args);
$build['#bundles'][$bundle_name]['label'] = $bundle_label;
$build['#bundles'][$bundle_name]['description'] = $description;
}
}
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_node:') !== 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;
}
}