You are here

public function GroupContentMenuController::addPage in Group Content Menu 8

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.

Overrides GroupContentController::addPage

File

src/Controller/GroupContentMenuController.php, line 39

Class

GroupContentMenuController
Returns responses for 'group_content_menu' GroupContent routes.

Namespace

Drupal\group_content_menu\Controller

Code

public function addPage(GroupInterface $group, $create_mode = TRUE) {
  $bundle_names = $this
    ->addPageBundles($group, $create_mode);

  // Filter out the bundles the user doesn't have access to. Duplicated from
  // parent class so as to avoid information disclosure.
  foreach ($bundle_names as $plugin_id => $bundle_name) {
    if ($create_mode) {
      $plugin = $group
        ->getGroupType()
        ->getContentPlugin($plugin_id);
      $access = $plugin
        ->createEntityAccess($group, $this
        ->currentUser());
    }
    else {
      $access_control_handler = $this->entityTypeManager
        ->getAccessControlHandler('group_content');
      $access = $access_control_handler
        ->createAccess($bundle_name, NULL, [
        'group' => $group,
      ], TRUE);
    }
    if (!$access
      ->isAllowed()) {
      unset($bundle_names[$plugin_id]);
    }
  }

  // Disallow creating multiple menus of the same type.
  if (count($bundle_names) === 1) {
    reset($bundle_names);
    $plugin_id = key($bundle_names);
    if ($limitation = $this
      ->handleOneMenuLimitation($group, $plugin_id)) {
      return $limitation;
    }
  }
  $build = parent::addPage($group, $create_mode);

  // Do not interfere with redirects.
  if (!is_array($build)) {
    return $build;
  }

  // Overwrite the label and description for all of the displayed bundles.
  $storage_handler = $this->entityTypeManager
    ->getStorage('group_content_menu_type');
  foreach ($this
    ->addPageBundles($group, $create_mode) as $plugin_id => $bundle_name) {
    if (!empty($build['#bundles'][$bundle_name])) {
      $plugin = $group
        ->getGroupType()
        ->getContentPlugin($plugin_id);
      $label = $plugin
        ->getLabel();
      $bundle_label = $storage_handler
        ->load($plugin
        ->getEntityBundle())
        ->label();
      $description = $this
        ->t('Add new menu of type %bundle_label to the group.', [
        '%bundle_label' => $bundle_label,
      ]);
      $build['#bundles'][$bundle_name]['label'] = $bundle_label;
      $build['#bundles'][$bundle_name]['description'] = $description;
      $build['#bundles'][$bundle_name]['add_link'] = Link::createFromRoute($label, 'entity.group_content_menu.add_form', [
        'group' => $group
          ->id(),
        'plugin_id' => $plugin_id,
      ]);
    }
  }
  return $build;
}