You are here

function answers_core_preprocess in Answers 1.0.x

Same name and namespace in other branches
  1. 2.0.x modules/core/answers_core.module \answers_core_preprocess()

Undocumented function

Parameters

[type] $variables:

[type] $hook:

Return value

void

File

modules/core/answers_core.module, line 47

Code

function answers_core_preprocess(&$variables, $hook) {
  if ($hook == 'node' && ($node = \Drupal::routeMatch()
    ->getParameter('node'))) {
    $variables['#attached']['library'][] = 'answers_core/node';
    $node_type = $node->type[0]->target_id;
    if ($node_type == 'answers_question') {
      $moduleHandler = \Drupal::service('module_handler');
      $date = $node->created[0]->value;
      if (REQUEST_TIME - $date < 86400) {
        $variables['date'] = 'today';
      }
      else {
        $variables['date'] = \Drupal::service('date.formatter')
          ->formatInterval(REQUEST_TIME - $date) . 'ago';
      }
      $answer_count = 0;
      if (!$variables['content']['answers']['#object']->answers
        ->isEmpty()) {
        $answer_count = $variables['content']['answers']['#object']->answers[0]->comment_count;
      }
      if ($answer_count == 0) {
        $variables['content']['answers']['#title'] = t('No Answers');
      }
      else {
        if ($answer_count == 1) {
          $variables['content']['answers']['#title'] = t('@count Answer', [
            '@count' => $answer_count,
          ]);
        }
        else {
          $variables['content']['answers']['#title'] = t('@count Answers', [
            '@count' => $answer_count,
          ]);
        }
      }
      if ($moduleHandler
        ->moduleExists('statistics')) {
        if ($node
          ->id()) {
          $statistics = \Drupal::service('statistics.storage.node')
            ->fetchView($node
            ->id());
          if ($statistics) {
            $viewed = \Drupal::translation()
              ->formatPlural($statistics
              ->getTotalCount(), 'Viewed 1 time', 'Viewed @count times');
            $variables['viewed'] = [
              '#markup' => $viewed,
            ];
          }
        }
      }
    }
  }
  if ($hook == 'comment') {

    // dpm(array_keys($variables));
    // unset($variables['title']);
  }
  if ($hook == 'form') {

    // dpm(array_keys($variables));
    // unset($variables['title']);
  }
}