Loading ggroup.module +12 −0 Original line number Diff line number Diff line Loading @@ -49,3 +49,15 @@ function ggroup_group_content_delete(GroupContentInterface $group_content) { \Drupal::service('ggroup.group_hierarchy_manager')->removeSubgroup($group_content); } /** * Implements hook_form_alter(). */ function ggroup_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { /** @var \Drupal\Core\Routing\RouteMatchInterface $route_matcher */ $route_matcher = \Drupal::service('current_route_match'); if ($route_matcher->getRouteName() == 'entity.group_content.create_form' && $route_matcher->getParameter('plugin_id') ) { $form['actions']['submit']['#value'] = t('Save'); } } ggroup.routing.yml +0 −10 Original line number Diff line number Diff line route_callbacks: - '\Drupal\ggroup\Routing\SubgroupRouteProvider::getRoutes' entity.group_content.subgroup_add_form: path: '/group/{group}/subgroup/create/{group_type}' defaults: _controller: '\Drupal\ggroup\Controller\SubgroupWizardController::addForm' _title_callback: '\Drupal\ggroup\Controller\SubgroupWizardController::addFormTitle' requirements: _subgroup_add_access: 'TRUE' options: _group_operation_route: 'TRUE' src/Controller/SubgroupController.php +57 −0 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ use Drupal\Core\Entity\EntityFormBuilderInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Render\RendererInterface; use Drupal\group\Entity\Controller\GroupContentController; use Drupal\group\Entity\GroupInterface; use Drupal\group\Plugin\GroupContentEnablerManagerInterface; use Drupal\Core\TempStore\PrivateTempStoreFactory; use Symfony\Component\DependencyInjection\ContainerInterface; Loading Loading @@ -54,4 +55,60 @@ class SubgroupController extends GroupContentController { ); } /** * {@inheritdoc} */ public function addPage(GroupInterface $group, $create_mode = FALSE) { $build = parent::addPage($group, $create_mode); // Do not interfere with redirects. if (!is_array($build)) { return $build; } // Overwrite the label and description for all displayed bundles. $storage_handler = $this->entityTypeManager->getStorage('group_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 = ['%group_type' => $bundle_label]; $description = $create_mode ? $this->t('Add a new subgroup of type %group_type to the group.', $t_args) : $this->t('Add existing subgroup of type %group_type to the group.', $t_args); $build['#bundles'][$bundle_name]['label'] = $bundle_label; $build['#bundles'][$bundle_name]['description'] = $description; } } return $build; } /** * {@inheritdoc} */ protected function addPageBundles(GroupInterface $group, $create_mode) { $bundles = []; // Retrieve all group_node plugins for the group's type. $plugin_ids = $this->pluginManager->getInstalledIds($group->getGroupType()); foreach ($plugin_ids as $key => $plugin_id) { if (strpos($plugin_id, 'subgroup:') !== 0) { unset($plugin_ids[$key]); } } // Retrieve all responsible group content types, keyed by plugin ID. $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) { /** @var \Drupal\group\Entity\GroupContentTypeInterface $group_content_type */ $bundles[$group_content_type->getContentPluginId()] = $bundle; } return $bundles; } } src/Controller/SubgroupWizardController.phpdeleted 100644 → 0 +0 −241 Original line number Diff line number Diff line <?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; /** * Returns responses for 'subgroup' GroupContent routes. */ class SubgroupWizardController extends ControllerBase { /** * The private store for temporary subgroups. * * @var \Drupal\user\privateTempStoreFactory */ protected $privateTempStoreFactory; /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * The entity form builder. * * @var \Drupal\Core\Entity\EntityFormBuilderInterface */ protected $entityFormBuilder; /** * The group content plugin manager. * * @var \Drupal\group\Plugin\GroupContentEnablerManagerInterface */ protected $pluginManager; /** * The renderer. * * @var \Drupal\Core\Render\RendererInterface */ protected $renderer; /** * Constructs a new SubgroupWizardController. * * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory * The factory for the temp store object. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder * The entity form builder. * @param \Drupal\group\Plugin\GroupContentEnablerManagerInterface $plugin_manager * The group content plugin manager. * @param \Drupal\Core\Render\RendererInterface $renderer * The 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; } /** * {@inheritdoc} */ 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') ); } /** * Provides the form for creating a subgroup in a group. * * @param \Drupal\group\Entity\GroupInterface $group * The group to create a subgroup in. * @param \Drupal\group\Entity\GroupTypeInterface $group_type * The subgroup type to create. * * @return array * The form array for either step 1 or 2 of the subgroup creation wizard. */ 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 we are on step one, we need to build a group form. if ($this->privateTempStoreFactory->get("$storage_id:step") !== 2) { $this->privateTempStoreFactory->set("$storage_id:step", 1); // Only create a new group if we have nothing stored. if (!$entity = $this->privateTempStoreFactory->get("$storage_id:group")) { $entity = Group::create(['type' => $group_type->id()]); } } // If we are on step two, we need to build a group content form. else { /** @var \Drupal\group\Plugin\GroupContentEnablerInterface $plugin */ $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); // We also clear the private store so we can start fresh next time // around. $this->privateTempStoreFactory->delete("$storage_id:step"); $this->privateTempStoreFactory->delete("$storage_id:group"); return $this->redirect('entity.group.canonical', ['group' => $entity->id()]); } } // Return the form with the group and storage ID added to the form state. $extra = [ 'group' => $group, 'storage_id' => $storage_id, 'wizard' => $creation_wizard, ]; return $this->entityFormBuilder()->getForm($entity, 'ggroup-form', $extra); } /** * The _title_callback for the add group form route. * * @param \Drupal\group\Entity\GroupInterface $group * The group to create a group in. * @param \Drupal\group\Entity\GroupTypeInterface $group_type * The group type to create. * * @return string * The page title. */ public function addFormTitle(GroupInterface $group, GroupTypeInterface $group_type) { return $this->t('Create %type in %label', ['%type' => $group_type->label(), '%label' => $group->label()]); } /** * Provides the subgroup creation overview page. * * @param \Drupal\group\Entity\GroupInterface $group * The group to add the subgroup to. * * @return array|\Symfony\Component\HttpFoundation\RedirectResponse * The subgroup creation overview page or a redirect to the create form if * we only have 1 bundle. */ public function addPage(GroupInterface $group) { // We do not set the "entity_add_list" template's "#add_bundle_message" key // because we deny access to the page if no bundle is available. $build = ['#theme' => 'entity_add_list', '#bundles' => []]; $add_form_route = 'entity.group_content.subgroup_add_form'; // Retrieve all subgroup plugins for the group's type. $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, ]; /** @var \Drupal\group\Entity\GroupContentTypeInterface[] $bundles */ $bundles = $storage->loadByProperties($properties); // Filter out the bundles the user doesn't have access to. $access_control_handler = $this->entityTypeManager->getAccessControlHandler('group_content'); foreach (array_keys($bundles) as $bundle) { // Check for access and add it as a cacheable dependency. $access = $access_control_handler->createAccess($bundle, NULL, ['group' => $group], TRUE); $this->renderer->addCacheableDependency($build, $access); // Remove inaccessible bundles from the list. if (!$access->isAllowed()) { unset($bundles[$bundle]); } } // Redirect if there's only one bundle available. 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()); } // Get the subgroup type storage handler. $storage_handler = $this->entityTypeManager->getStorage('group_type'); // Set the info for all of the remaining bundles. 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), ]; } // Add the list cache tags for the GroupContentType entity type. $bundle_entity_type = $this->entityTypeManager->getDefinition('group_content_type'); $build['#cache']['tags'] = $bundle_entity_type->getListCacheTags(); return $build; } } src/Form/SubgroupFormStep1.phpdeleted 100644 → 0 +0 −80 Original line number Diff line number Diff line <?php namespace Drupal\ggroup\Form; use Drupal\group\Entity\Form\GroupForm; use Drupal\Core\Url; use Drupal\Core\Form\FormStateInterface; /** * Provides a creating a group without it being saved yet. */ class SubgroupFormStep1 extends GroupForm { /** * {@inheritdoc} */ protected function actions(array $form, FormStateInterface $form_state) { $actions['submit'] = [ '#type' => 'submit', '#value' => $form_state->get('wizard') ? $this->t('Continue to final step') : $this->t('Create subgroup'), '#submit' => ['::submitForm', '::saveTemporary'], ]; $actions['cancel'] = [ '#type' => 'submit', '#value' => $this->t('Cancel'), '#submit' => ['::cancel'], '#limit_validation_errors' => [], ]; return $actions; } /** * Saves a temporary group and continues to step 2 of subgroup creation. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * * @see \Drupal\ggroup\Controller\SubgroupWizardController::add() * @see \Drupal\ggroup\Form\SubgroupFormStep2 */ public function saveTemporary(array &$form, FormStateInterface $form_state) { $storage_id = $form_state->get('storage_id'); $store = $this->privateTempStoreFactory->get('ggroup_add_temp'); $store->set("$storage_id:group", $this->entity); $store->set("$storage_id:step", 2); // Disable any URL-based redirect until the final step. $request = $this->getRequest(); $form_state->setRedirectUrl(Url::fromRoute('<current>', [], ['query' => $request->query->all()])); $request->query->remove('destination'); } /** * Cancels the group creation by emptying the temp store. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * * @see \Drupal\ggroup\Controller\SubgroupWizardController::add() */ public function cancel(array &$form, FormStateInterface $form_state) { /** @var \Drupal\group\Entity\GroupInterface $group */ $group = $form_state->get('group'); $storage_id = $form_state->get('storage_id'); $store = $this->privateTempStoreFactory->get('ggroup_add_temp'); $store->delete("$storage_id:group"); // Redirect to the group page if no destination was set in the URL. $form_state->setRedirect('entity.group.canonical', ['group' => $group->id()]); } } Loading
ggroup.module +12 −0 Original line number Diff line number Diff line Loading @@ -49,3 +49,15 @@ function ggroup_group_content_delete(GroupContentInterface $group_content) { \Drupal::service('ggroup.group_hierarchy_manager')->removeSubgroup($group_content); } /** * Implements hook_form_alter(). */ function ggroup_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { /** @var \Drupal\Core\Routing\RouteMatchInterface $route_matcher */ $route_matcher = \Drupal::service('current_route_match'); if ($route_matcher->getRouteName() == 'entity.group_content.create_form' && $route_matcher->getParameter('plugin_id') ) { $form['actions']['submit']['#value'] = t('Save'); } }
ggroup.routing.yml +0 −10 Original line number Diff line number Diff line route_callbacks: - '\Drupal\ggroup\Routing\SubgroupRouteProvider::getRoutes' entity.group_content.subgroup_add_form: path: '/group/{group}/subgroup/create/{group_type}' defaults: _controller: '\Drupal\ggroup\Controller\SubgroupWizardController::addForm' _title_callback: '\Drupal\ggroup\Controller\SubgroupWizardController::addFormTitle' requirements: _subgroup_add_access: 'TRUE' options: _group_operation_route: 'TRUE'
src/Controller/SubgroupController.php +57 −0 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ use Drupal\Core\Entity\EntityFormBuilderInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Render\RendererInterface; use Drupal\group\Entity\Controller\GroupContentController; use Drupal\group\Entity\GroupInterface; use Drupal\group\Plugin\GroupContentEnablerManagerInterface; use Drupal\Core\TempStore\PrivateTempStoreFactory; use Symfony\Component\DependencyInjection\ContainerInterface; Loading Loading @@ -54,4 +55,60 @@ class SubgroupController extends GroupContentController { ); } /** * {@inheritdoc} */ public function addPage(GroupInterface $group, $create_mode = FALSE) { $build = parent::addPage($group, $create_mode); // Do not interfere with redirects. if (!is_array($build)) { return $build; } // Overwrite the label and description for all displayed bundles. $storage_handler = $this->entityTypeManager->getStorage('group_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 = ['%group_type' => $bundle_label]; $description = $create_mode ? $this->t('Add a new subgroup of type %group_type to the group.', $t_args) : $this->t('Add existing subgroup of type %group_type to the group.', $t_args); $build['#bundles'][$bundle_name]['label'] = $bundle_label; $build['#bundles'][$bundle_name]['description'] = $description; } } return $build; } /** * {@inheritdoc} */ protected function addPageBundles(GroupInterface $group, $create_mode) { $bundles = []; // Retrieve all group_node plugins for the group's type. $plugin_ids = $this->pluginManager->getInstalledIds($group->getGroupType()); foreach ($plugin_ids as $key => $plugin_id) { if (strpos($plugin_id, 'subgroup:') !== 0) { unset($plugin_ids[$key]); } } // Retrieve all responsible group content types, keyed by plugin ID. $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) { /** @var \Drupal\group\Entity\GroupContentTypeInterface $group_content_type */ $bundles[$group_content_type->getContentPluginId()] = $bundle; } return $bundles; } }
src/Controller/SubgroupWizardController.phpdeleted 100644 → 0 +0 −241 Original line number Diff line number Diff line <?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; /** * Returns responses for 'subgroup' GroupContent routes. */ class SubgroupWizardController extends ControllerBase { /** * The private store for temporary subgroups. * * @var \Drupal\user\privateTempStoreFactory */ protected $privateTempStoreFactory; /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * The entity form builder. * * @var \Drupal\Core\Entity\EntityFormBuilderInterface */ protected $entityFormBuilder; /** * The group content plugin manager. * * @var \Drupal\group\Plugin\GroupContentEnablerManagerInterface */ protected $pluginManager; /** * The renderer. * * @var \Drupal\Core\Render\RendererInterface */ protected $renderer; /** * Constructs a new SubgroupWizardController. * * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory * The factory for the temp store object. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder * The entity form builder. * @param \Drupal\group\Plugin\GroupContentEnablerManagerInterface $plugin_manager * The group content plugin manager. * @param \Drupal\Core\Render\RendererInterface $renderer * The 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; } /** * {@inheritdoc} */ 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') ); } /** * Provides the form for creating a subgroup in a group. * * @param \Drupal\group\Entity\GroupInterface $group * The group to create a subgroup in. * @param \Drupal\group\Entity\GroupTypeInterface $group_type * The subgroup type to create. * * @return array * The form array for either step 1 or 2 of the subgroup creation wizard. */ 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 we are on step one, we need to build a group form. if ($this->privateTempStoreFactory->get("$storage_id:step") !== 2) { $this->privateTempStoreFactory->set("$storage_id:step", 1); // Only create a new group if we have nothing stored. if (!$entity = $this->privateTempStoreFactory->get("$storage_id:group")) { $entity = Group::create(['type' => $group_type->id()]); } } // If we are on step two, we need to build a group content form. else { /** @var \Drupal\group\Plugin\GroupContentEnablerInterface $plugin */ $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); // We also clear the private store so we can start fresh next time // around. $this->privateTempStoreFactory->delete("$storage_id:step"); $this->privateTempStoreFactory->delete("$storage_id:group"); return $this->redirect('entity.group.canonical', ['group' => $entity->id()]); } } // Return the form with the group and storage ID added to the form state. $extra = [ 'group' => $group, 'storage_id' => $storage_id, 'wizard' => $creation_wizard, ]; return $this->entityFormBuilder()->getForm($entity, 'ggroup-form', $extra); } /** * The _title_callback for the add group form route. * * @param \Drupal\group\Entity\GroupInterface $group * The group to create a group in. * @param \Drupal\group\Entity\GroupTypeInterface $group_type * The group type to create. * * @return string * The page title. */ public function addFormTitle(GroupInterface $group, GroupTypeInterface $group_type) { return $this->t('Create %type in %label', ['%type' => $group_type->label(), '%label' => $group->label()]); } /** * Provides the subgroup creation overview page. * * @param \Drupal\group\Entity\GroupInterface $group * The group to add the subgroup to. * * @return array|\Symfony\Component\HttpFoundation\RedirectResponse * The subgroup creation overview page or a redirect to the create form if * we only have 1 bundle. */ public function addPage(GroupInterface $group) { // We do not set the "entity_add_list" template's "#add_bundle_message" key // because we deny access to the page if no bundle is available. $build = ['#theme' => 'entity_add_list', '#bundles' => []]; $add_form_route = 'entity.group_content.subgroup_add_form'; // Retrieve all subgroup plugins for the group's type. $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, ]; /** @var \Drupal\group\Entity\GroupContentTypeInterface[] $bundles */ $bundles = $storage->loadByProperties($properties); // Filter out the bundles the user doesn't have access to. $access_control_handler = $this->entityTypeManager->getAccessControlHandler('group_content'); foreach (array_keys($bundles) as $bundle) { // Check for access and add it as a cacheable dependency. $access = $access_control_handler->createAccess($bundle, NULL, ['group' => $group], TRUE); $this->renderer->addCacheableDependency($build, $access); // Remove inaccessible bundles from the list. if (!$access->isAllowed()) { unset($bundles[$bundle]); } } // Redirect if there's only one bundle available. 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()); } // Get the subgroup type storage handler. $storage_handler = $this->entityTypeManager->getStorage('group_type'); // Set the info for all of the remaining bundles. 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), ]; } // Add the list cache tags for the GroupContentType entity type. $bundle_entity_type = $this->entityTypeManager->getDefinition('group_content_type'); $build['#cache']['tags'] = $bundle_entity_type->getListCacheTags(); return $build; } }
src/Form/SubgroupFormStep1.phpdeleted 100644 → 0 +0 −80 Original line number Diff line number Diff line <?php namespace Drupal\ggroup\Form; use Drupal\group\Entity\Form\GroupForm; use Drupal\Core\Url; use Drupal\Core\Form\FormStateInterface; /** * Provides a creating a group without it being saved yet. */ class SubgroupFormStep1 extends GroupForm { /** * {@inheritdoc} */ protected function actions(array $form, FormStateInterface $form_state) { $actions['submit'] = [ '#type' => 'submit', '#value' => $form_state->get('wizard') ? $this->t('Continue to final step') : $this->t('Create subgroup'), '#submit' => ['::submitForm', '::saveTemporary'], ]; $actions['cancel'] = [ '#type' => 'submit', '#value' => $this->t('Cancel'), '#submit' => ['::cancel'], '#limit_validation_errors' => [], ]; return $actions; } /** * Saves a temporary group and continues to step 2 of subgroup creation. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * * @see \Drupal\ggroup\Controller\SubgroupWizardController::add() * @see \Drupal\ggroup\Form\SubgroupFormStep2 */ public function saveTemporary(array &$form, FormStateInterface $form_state) { $storage_id = $form_state->get('storage_id'); $store = $this->privateTempStoreFactory->get('ggroup_add_temp'); $store->set("$storage_id:group", $this->entity); $store->set("$storage_id:step", 2); // Disable any URL-based redirect until the final step. $request = $this->getRequest(); $form_state->setRedirectUrl(Url::fromRoute('<current>', [], ['query' => $request->query->all()])); $request->query->remove('destination'); } /** * Cancels the group creation by emptying the temp store. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * * @see \Drupal\ggroup\Controller\SubgroupWizardController::add() */ public function cancel(array &$form, FormStateInterface $form_state) { /** @var \Drupal\group\Entity\GroupInterface $group */ $group = $form_state->get('group'); $storage_id = $form_state->get('storage_id'); $store = $this->privateTempStoreFactory->get('ggroup_add_temp'); $store->delete("$storage_id:group"); // Redirect to the group page if no destination was set in the URL. $form_state->setRedirect('entity.group.canonical', ['group' => $group->id()]); } }