class SubgroupWizardController in Subgroup (Graph) 1.0.x
Returns responses for 'subgroup' GroupContent routes.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\ggroup\Controller\SubgroupWizardController
Expanded class hierarchy of SubgroupWizardController
File
- src/
Controller/ SubgroupWizardController.php, line 23
Namespace
Drupal\ggroup\ControllerView source
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(),
]);
}
}
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
SubgroupWizardController:: |
protected | property |
The entity form builder. Overrides ControllerBase:: |
|
SubgroupWizardController:: |
protected | property |
The entity type manager. Overrides ControllerBase:: |
|
SubgroupWizardController:: |
protected | property | The group content plugin manager. | |
SubgroupWizardController:: |
protected | property | The private store for temporary subgroups. | |
SubgroupWizardController:: |
protected | property | The renderer. | |
SubgroupWizardController:: |
public | function | Provides the form for creating a subgroup in a group. | |
SubgroupWizardController:: |
public | function | The _title_callback for the add group form route. | |
SubgroupWizardController:: |
public | function | Provides the subgroup creation overview page. | |
SubgroupWizardController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
SubgroupWizardController:: |
public | function | Constructs a new SubgroupWizardController. |