You are here

function social_group_social_user_account_header_create_links in Open Social 8.7

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/social_group.module \social_group_social_user_account_header_create_links()
  2. 8.4 modules/social_features/social_group/social_group.module \social_group_social_user_account_header_create_links()
  3. 8.5 modules/social_features/social_group/social_group.module \social_group_social_user_account_header_create_links()
  4. 8.6 modules/social_features/social_group/social_group.module \social_group_social_user_account_header_create_links()
  5. 8.8 modules/social_features/social_group/social_group.module \social_group_social_user_account_header_create_links()
  6. 10.3.x modules/social_features/social_group/social_group.module \social_group_social_user_account_header_create_links()
  7. 10.0.x modules/social_features/social_group/social_group.module \social_group_social_user_account_header_create_links()
  8. 10.1.x modules/social_features/social_group/social_group.module \social_group_social_user_account_header_create_links()
  9. 10.2.x modules/social_features/social_group/social_group.module \social_group_social_user_account_header_create_links()

Implements hook_social_user_account_header_create_links().

Adds the "Create Group" link to the content creation menu. When the user can only create groups of one type, the user skips the group type selection page.

File

modules/social_features/social_group/social_group.module, line 1767
The Social group module.

Code

function social_group_social_user_account_header_create_links($context) {

  // Create url to create a group.
  $route_add_group = Url::fromRoute('entity.group.add_page');

  // If we have a user to work with, we can check if we should redirect it to
  // the create group overview or a single group.
  // If we do not have a user we'll default to the overview.
  if (!empty($context['user'])) {

    /** @var \Drupal\Core\Session\AccountInterface $user */
    $account = $context['user'];
    $user_can_create_groups = [];

    // Get all available group types.
    foreach (GroupType::loadMultiple() as $group_type) {

      // When the user has permission to create a group of the current type, add
      // this to the create group array.
      if ($account
        ->hasPermission('create ' . $group_type
        ->id() . ' group')) {
        $user_can_create_groups[$group_type
          ->id()] = $group_type;
      }
      if (count($user_can_create_groups) > 1) {
        break;
      }
    }

    // There's just one group this user can create.
    if (count($user_can_create_groups) === 1) {

      // When there is only one group allowed, add create the url to create a
      // group of this type.

      /** @var \Drupal\group\Entity\Group $allowed_group_type */
      $allowed_group_type = reset($user_can_create_groups);
      $route_add_group = Url::fromRoute('entity.group.add_form', [
        'group_type' => $allowed_group_type
          ->id(),
      ]);
    }
  }
  return [
    'add_group' => [
      '#type' => 'link',
      '#attributes' => [
        'title' => new TranslatableMarkup('Create New Group'),
      ],
      '#title' => new TranslatableMarkup('New Group'),
      '#weight' => 500,
    ] + $route_add_group
      ->toRenderArray(),
  ];
}