You are here

public function SubgroupWizardController::addPage in Subgroup (Graph) 1.0.x

Provides the subgroup creation overview page.

Parameters

\Drupal\group\Entity\GroupInterface $group: The group to add the subgroup to.

Return value

array|\Symfony\Component\HttpFoundation\RedirectResponse The subgroup creation overview page or a redirect to the create form if we only have 1 bundle.

File

src/Controller/SubgroupWizardController.php, line 174

Class

SubgroupWizardController
Returns responses for 'subgroup' GroupContent routes.

Namespace

Drupal\ggroup\Controller

Code

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;
}