View source
<?php
namespace Drupal\ggroup\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Render\RendererInterface;
use Drupal\group\Entity\GroupContent;
use Drupal\group\Entity\GroupInterface;
use Drupal\group\Plugin\GroupContentEnablerManagerInterface;
use Drupal\group\Entity\Group;
use Drupal\group\Entity\GroupTypeInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class SubgroupWizardController extends ControllerBase {
protected $privateTempStoreFactory;
protected $entityTypeManager;
protected $entityFormBuilder;
protected $pluginManager;
protected $renderer;
public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager, EntityFormBuilderInterface $entity_form_builder, GroupContentEnablerManagerInterface $plugin_manager, RendererInterface $renderer) {
$this->privateTempStoreFactory = $temp_store_factory
->get('ggroup_add_temp');
$this->entityTypeManager = $entity_type_manager;
$this->entityFormBuilder = $entity_form_builder;
$this->pluginManager = $plugin_manager;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.private'), $container
->get('entity_type.manager'), $container
->get('entity.form_builder'), $container
->get('plugin.manager.group_content_enabler'), $container
->get('renderer'));
}
public function addForm(GroupInterface $group, GroupTypeInterface $group_type) {
$plugin_id = "subgroup:{$group_type->id()}";
$storage_id = "{$plugin_id}:{$group->id()}";
$creation_wizard = $group
->getGroupType()
->getContentPlugin($plugin_id)
->getConfiguration()['use_creation_wizard'];
if ($this->privateTempStoreFactory
->get("{$storage_id}:step") !== 2) {
$this->privateTempStoreFactory
->set("{$storage_id}:step", 1);
if (!($entity = $this->privateTempStoreFactory
->get("{$storage_id}:group"))) {
$entity = Group::create([
'type' => $group_type
->id(),
]);
}
}
else {
$plugin = $group
->getGroupType()
->getContentPlugin($plugin_id);
$entity = GroupContent::create([
'type' => $plugin
->getContentTypeConfigId(),
'gid' => $group
->id(),
]);
if (!$creation_wizard && ($entity = $this->privateTempStoreFactory
->get("{$storage_id}:group"))) {
$entity
->save();
$group
->addContent($entity, $plugin_id);
$this->privateTempStoreFactory
->delete("{$storage_id}:step");
$this->privateTempStoreFactory
->delete("{$storage_id}:group");
return $this
->redirect('entity.group.canonical', [
'group' => $entity
->id(),
]);
}
}
$extra = [
'group' => $group,
'storage_id' => $storage_id,
'wizard' => $creation_wizard,
];
return $this
->entityFormBuilder()
->getForm($entity, 'ggroup-form', $extra);
}
public function addFormTitle(GroupInterface $group, GroupTypeInterface $group_type) {
return $this
->t('Create %type in %label', [
'%type' => $group_type
->label(),
'%label' => $group
->label(),
]);
}
public function addPage(GroupInterface $group) {
$build = [
'#theme' => 'entity_add_list',
'#bundles' => [],
];
$add_form_route = 'entity.group_content.subgroup_add_form';
$plugin_ids = $this->pluginManager
->getInstalledIds($group
->getGroupType());
foreach ($plugin_ids as $key => $plugin_id) {
if (strpos($plugin_id, 'subgroup:') !== 0) {
unset($plugin_ids[$key]);
}
}
$storage = $this->entityTypeManager
->getStorage('group_content_type');
$properties = [
'group_type' => $group
->bundle(),
'content_plugin' => $plugin_ids,
];
$bundles = $storage
->loadByProperties($properties);
$access_control_handler = $this->entityTypeManager
->getAccessControlHandler('group_content');
foreach (array_keys($bundles) as $bundle) {
$access = $access_control_handler
->createAccess($bundle, NULL, [
'group' => $group,
], TRUE);
$this->renderer
->addCacheableDependency($build, $access);
if (!$access
->isAllowed()) {
unset($bundles[$bundle]);
}
}
if (count($bundles) == 1) {
$group_content_type = reset($bundles);
$plugin = $group_content_type
->getContentPlugin();
$route_params = [
'group' => $group
->id(),
'group_type' => $plugin
->getEntityBundle(),
];
$url = Url::fromRoute($add_form_route, $route_params, [
'absolute' => TRUE,
]);
return new RedirectResponse($url
->toString());
}
$storage_handler = $this->entityTypeManager
->getStorage('group_type');
foreach ($bundles as $bundle => $group_content_type) {
$plugin = $group_content_type
->getContentPlugin();
$bundle_label = $storage_handler
->load($plugin
->getEntityBundle())
->label();
$route_params = [
'group' => $group
->id(),
'group_type' => $plugin
->getEntityBundle(),
];
$build['#bundles'][$bundle] = [
'label' => $bundle_label,
'description' => $this
->t('Create a subgroup of type %group_type for the group.', [
'%group_type' => $bundle_label,
]),
'add_link' => Link::createFromRoute($bundle_label, $add_form_route, $route_params),
];
}
$bundle_entity_type = $this->entityTypeManager
->getDefinition('group_content_type');
$build['#cache']['tags'] = $bundle_entity_type
->getListCacheTags();
return $build;
}
}