You are here

function template_preprocess_opigno_certificate in Opigno certificate 3.x

Same name and namespace in other branches
  1. 8 opigno_certificate.module \template_preprocess_opigno_certificate()

Implements template_preprocess_HOOK().

File

./opigno_certificate.module, line 226
Contains opigno_certificate.module.

Code

function template_preprocess_opigno_certificate(array &$variables) {

  // Get the actual user.
  $user = NULL;
  if (\Drupal::routeMatch()
    ->getRouteName() === 'certificate.entity.pdf') {
    $referer = \Drupal::request()->server
      ->get('HTTP_REFERER');
    $router = \Drupal::service('router');
    if ($router instanceof AccessAwareRouterInterface) {
      try {
        $route_info = $router
          ->match($referer);
        $user = $route_info['user'] ?? $user;
      } catch (AccessDeniedHttpException $e) {
        watchdog_exception('opigno_social_exception', $e);
      }
    }
  }
  $user = $user instanceof UserInterface ? $user : \Drupal::currentUser();

  /** @var \Drupal\opigno_certificate\OpignoCertificateInterface $opigno_certificate */
  $opigno_certificate = $variables['elements']['#opigno_certificate'];

  /** @var \Drupal\Core\Entity\ContentEntityInterface $referencing_entity */
  $referencing_entity = $opigno_certificate->referencing_entity->entity;

  // Provide the referencing entity context.
  if ($referencing_entity) {
    $variables['referencing_entity'] = $referencing_entity;
    if ($opigno_certificate
      ->getViewModeSelectorField()) {
      $variables['content']['referencing_entity'] = [
        '#markup' => $referencing_entity
          ->label(),
      ];
    }

    // Get the completion date.

    /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
    $date_formatter = \Drupal::service('date.formatter');
    $gid = (int) $referencing_entity
      ->id();
    $uid = (int) $user
      ->id();
    try {
      $completed = opigno_learning_path_completed_on($gid, $uid, TRUE);
    } catch (InvalidPluginDefinitionException|PluginNotFoundException|PluginException $e) {
      $completed = NULL;
      watchdog_exception('opigno_certificate_exception', $e);
    }
    if ($completed) {
      $variables['completed_on'] = $date_formatter
        ->format($completed, 'certificate_date');
    }

    // Get the expiration date.
    $expire = LPStatus::getCertificateExpireTimestamp($gid, $uid);
    if ($expire) {
      $variables['expire_on'] = $date_formatter
        ->format($expire, 'certificate_date');
    }
  }

  // Add the background image and logo.
  $bg_img = drupal_get_path('theme', 'aristotle') . '/assets/Logo-Opigno-circle.svg';
  $variables['bg_image'] = file_exists($bg_img) ? file_url_transform_relative(base_path() . $bg_img) : '';
  $logo = drupal_get_path('theme', 'aristotle') . '/assets/Logo-Opigno-3-dark.svg';
  $variables['logo'] = file_exists($logo) ? file_url_transform_relative(base_path() . $logo) : '';
  $variables['awarded_to'] = $user
    ->getDisplayName();

  // Helpful $content variable for templates.
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }
  $variables['style_certificate_edit_form'] = drupal_get_path('module', 'opigno_certificate') . '/css/opigno_certificate_edit_form.css';
}