You are here

function opigno_learning_path_preprocess_page in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x opigno_learning_path.module \opigno_learning_path_preprocess_page()

Implements hook_preprocess_page().

File

./opigno_learning_path.module, line 461
Contains opigno_learning_path.module.

Code

function opigno_learning_path_preprocess_page(&$variables) {
  $route = \Drupal::routeMatch()
    ->getRouteName();
  $account = \Drupal::currentUser();
  $group = \Drupal::routeMatch()
    ->getParameter('group');

  // Hide local tasks.
  if (opigno_learning_path_is_lp_route()) {
    unset($variables['page']['content']['platon_local_tasks']);
  }

  // Check if there is required trainings.
  if ($route == 'entity.group.canonical' && $group && $group
    ->getGroupType()
    ->id() == 'learning_path' && ($required_trainings = LearningPathAccess::hasUncompletedRequiredTrainings($group, $account))) {

    // Show for user required trainings.
    $links = [];
    foreach ($required_trainings as $gid) {
      $training = Group::load($gid);
      $url = Url::fromRoute($route, [
        'group' => $training
          ->id(),
      ]);
      $link = Link::fromTextAndUrl($training
        ->label(), $url)
        ->toString();
      array_push($links, render($link));
    }
    $messenger = \Drupal::messenger();
    $messenger
      ->addWarning(Markup::create(t('You need to complete first some prerequisite trainings:') . ' ' . implode(', ', $links)));
  }

  // Get join form.
  if ($route == 'entity.group.canonical' && $group) {
    $form_builder = \Drupal::service('opigno_learning_path.join_form');
    $form = $form_builder
      ->getForm($group);
    if ($group
      ->hasField('field_learning_path_visibility')) {
      $visibility = $group->field_learning_path_visibility->value;
      if ($visibility === 'semiprivate' && !$account
        ->isAuthenticated() || !$group
        ->hasPermission('join group', $account)) {
        return;
      }
    }
    if (!$group
      ->getMember($account)) {
      $variables['join_group_form'] = [
        '#type' => 'container',
        '#attributes' => [
          'id' => 'join-group-form-overlay',
          'style' => 'display: none;',
        ],
        [
          '#type' => 'container',
          '#attributes' => [
            'id' => 'join-group-form-wrapper',
          ],
          [
            '#type' => 'container',
            '#attributes' => [
              'class' => [
                'text-right',
              ],
            ],
            [
              '#type' => 'html_tag',
              '#tag' => 'button',
              '#attributes' => [
                'class' => [
                  'close-overlay',
                ],
              ],
              '#value' => t('close'),
            ],
          ],
          [
            '#type' => 'container',
            'form' => $form,
          ],
        ],
      ];
    }
  }
  if ($route == 'entity.group.edit_form' && ($group = \Drupal::routeMatch()
    ->getParameter('group'))) {
    $form_builder = \Drupal::service('entity.form_builder');
    $form = $form_builder
      ->getForm($group, 'delete', []);

    // Change default link to current page.
    $url = Url::fromRoute('entity.group.edit_form', [
      'group' => $group
        ->id(),
    ], [
      'attributes' => [
        'class' => [
          'button',
          'close-overlay',
        ],
      ],
    ]);
    $link = Link::fromTextAndUrl(t('Cancel'), $url)
      ->toRenderable();
    $form['actions']['cancel']['#url'] = $url;
    $variables['delete_lp_form'] = [
      '#type' => 'container',
      '#attributes' => [
        'id' => 'delete-lp-form-overlay',
        'style' => 'display: none;',
      ],
      [
        '#type' => 'container',
        '#attributes' => [
          'id' => 'delete-lp-form-wrapper',
        ],
        [
          '#type' => 'container',
          '#attributes' => [
            'class' => [
              'text-right',
            ],
          ],
          [
            '#type' => 'html_tag',
            '#tag' => 'button',
            '#attributes' => [
              'class' => [
                'close-overlay',
              ],
            ],
            '#value' => t('close'),
          ],
        ],
        [
          '#type' => 'container',
          'form' => $form,
        ],
      ],
    ];
  }
}