You are here

public function GroupContentController::addPage in Group 2.0.x

Same name and namespace in other branches
  1. 8 src/Entity/Controller/GroupContentController.php \Drupal\group\Entity\Controller\GroupContentController::addPage()

Provides the group content creation overview page.

Parameters

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

bool $create_mode: (optional) Whether the target entity still needs to be created. Defaults to FALSE, meaning the target entity is assumed to exist already.

Return value

array|\Symfony\Component\HttpFoundation\RedirectResponse The group content creation overview page or a redirect to the form for adding group content if there is only one group content type.

1 call to GroupContentController::addPage()
GroupNodeController::addPage in modules/gnode/src/Controller/GroupNodeController.php
Provides the group content creation overview page.
1 method overrides GroupContentController::addPage()
GroupNodeController::addPage in modules/gnode/src/Controller/GroupNodeController.php
Provides the group content creation overview page.

File

src/Entity/Controller/GroupContentController.php, line 96

Class

GroupContentController
Returns responses for GroupContent routes.

Namespace

Drupal\group\Entity\Controller

Code

public function addPage(GroupInterface $group, $create_mode = FALSE) {
  $build = [
    '#theme' => 'entity_add_list',
    '#bundles' => [],
  ];
  $form_route = $this
    ->addPageFormRoute($group, $create_mode);
  $bundle_names = $this
    ->addPageBundles($group, $create_mode);

  // Set the add bundle message if available.
  $add_bundle_message = $this
    ->addPageBundleMessage($group, $create_mode);
  if ($add_bundle_message !== FALSE) {
    $build['#add_bundle_message'] = $add_bundle_message;
  }

  // Filter out the bundles the user doesn't have access to.
  $access_control_handler = $this->entityTypeManager
    ->getAccessControlHandler('group_content');
  foreach ($bundle_names as $plugin_id => $bundle_name) {
    $access = $access_control_handler
      ->createAccess($bundle_name, NULL, [
      'group' => $group,
    ], TRUE);
    if (!$access
      ->isAllowed()) {
      unset($bundle_names[$plugin_id]);
    }
    $this->renderer
      ->addCacheableDependency($build, $access);
  }

  // Redirect if there's only one bundle available.
  if (count($bundle_names) == 1) {
    reset($bundle_names);
    $route_params = [
      'group' => $group
        ->id(),
      'plugin_id' => key($bundle_names),
    ];
    $url = Url::fromRoute($form_route, $route_params, [
      'absolute' => TRUE,
    ]);
    return new RedirectResponse($url
      ->toString());
  }

  // Set the info for all of the remaining bundles.
  foreach ($bundle_names as $plugin_id => $bundle_name) {
    $plugin = $group
      ->getGroupType()
      ->getContentPlugin($plugin_id);
    $label = $plugin
      ->getLabel();
    $build['#bundles'][$bundle_name] = [
      'label' => $label,
      'description' => $plugin
        ->getContentTypeDescription(),
      'add_link' => Link::createFromRoute($label, $form_route, [
        'group' => $group
          ->id(),
        'plugin_id' => $plugin_id,
      ]),
    ];
  }

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