You are here

public function DefaultTwigExtension::get_join_group_link in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 src/TwigExtension/DefaultTwigExtension.php \Drupal\opigno_learning_path\TwigExtension\DefaultTwigExtension::get_join_group_link()

Returns join group link.

Parameters

mixed $group: Group.

mixed $account: User account.

array $attributes: Attributes.

Return value

mixed|null|string Join group link or empty.

File

src/TwigExtension/DefaultTwigExtension.php, line 138

Class

DefaultTwigExtension
Class DefaultTwigExtension.

Namespace

Drupal\opigno_learning_path\TwigExtension

Code

public function get_join_group_link($group = NULL, $account = NULL, array $attributes = []) {
  $route = \Drupal::routeMatch();
  if (!isset($group)) {
    $group = $route
      ->getParameter('group');
  }
  if (!isset($account)) {
    $account = \Drupal::currentUser();
  }
  $route_name = $route
    ->getRouteName();
  $visibility = $group->field_learning_path_visibility->value;
  $access = isset($group) && $group
    ->access('view', $account) && ($group
    ->hasPermission('join group', $account) || $visibility == 'public' || $visibility == 'semiprivate');

  // If training is paid.
  $is_member = $group
    ->getMember($account) !== FALSE;
  $module_commerce_enabled = \Drupal::moduleHandler()
    ->moduleExists('opigno_commerce');
  if ($module_commerce_enabled && $group
    ->hasField('field_lp_price') && $group
    ->get('field_lp_price')->value != 0 && !$is_member) {
    return '';
  }
  if ($route_name == 'entity.group.canonical' && $access) {
    $link = NULL;
    $validation = LearningPathAccess::requiredValidation($group, $account);
    $is_anonymous = $account
      ->id() === 0;
    if ($visibility == 'semiprivate' && $validation) {
      $joinLabel = t('Request subscription to the training');
    }
    else {
      $joinLabel = t('Subscribe to training');
    }
    if ($is_anonymous) {
      if ($visibility === 'public') {
        $link = [
          'title' => t('Start'),
          'route' => 'opigno_learning_path.steps.start',
          'args' => [
            'group' => $group
              ->id(),
          ],
        ];
        $attributes['class'][] = 'use-ajax';
      }
      else {
        $url = Url::fromRoute('entity.group.canonical', [
          'group' => $group
            ->id(),
        ]);
        $link = [
          'title' => $joinLabel,
          'route' => 'user.login',
          'args' => [
            'destination' => render($url)
              ->toString(),
          ],
        ];
      }
    }
    elseif (!$is_member) {
      $link = [
        'title' => $joinLabel,
        'route' => 'entity.group.join',
        'args' => [
          'group' => $group
            ->id(),
        ],
      ];
    }
    if ($route_name == 'entity.group.canonical' && $is_anonymous && $visibility == 'semiprivate') {
      $url = Url::fromRoute('entity.group.canonical', [
        'group' => $group
          ->id(),
      ]);
      $link = [
        'title' => t('Create an account and subscribe'),
        'route' => 'user.login',
        'args' => [
          'prev_path' => render($url)
            ->toString(),
        ],
      ];
    }
    if ($link) {
      $url = Url::fromRoute($link['route'], $link['args'], [
        'attributes' => $attributes,
      ]);
      $l = Link::fromTextAndUrl($link['title'], $url)
        ->toRenderable();
      return render($l);
    }
  }
  return '';
}