You are here

answers_core.module in Answers 1.0.x

Same filename and directory in other branches
  1. 2.0.x modules/core/answers_core.module

File

modules/core/answers_core.module
View source
<?php

use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\comment\CommentInterface;

/**
 * Implements hook_theme().
 */
function answers_core_theme() {
  return [
    'comment__answers_comments__answers_answer' => [
      'base hook' => 'comment',
    ],
    'comment__answers__answers_question' => [
      'base hook' => 'comment',
    ],
    'comment__answers_comments__answers_answer' => [
      'base hook' => 'comment',
    ],
    'comment__answers_comments__answers_question' => [
      'base hook' => 'comment',
    ],
    'node__answers_question' => [
      'base hook' => 'node',
    ],
    'field__node__answers__answers_question' => [
      'base hook' => 'field',
    ],
    // 'field__node__answers_comments__answers_answer' => [
    //   'base hook' => 'field',
    // ],
    'field__node__answers_comments__answers_question' => [
      'base hook' => 'field',
    ],
  ];
}

/**
 * Undocumented function
 *
 * @param [type] $variables
 * @param [type] $hook
 * @return void
 */
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']);
  }
}

/**
 * Implements hook_node_links_alter().
 */
function answers_core_node_links_alter(array &$links, NodeInterface $node, array &$context) {
  $is_question = $node->type->target_id == 'answers_question';
  if ($is_question && array_key_exists('statistics', $links)) {
    unset($links['statistics']);
  }
}

/**
 * Implements hook_comment_links_alter().
 */
function answers_core_comment_links_alter(array &$links, CommentInterface $comment, array &$context) {
  $comment_type = $comment
    ->bundle();
  $answers_types = [
    'answers_answer',
    'answers_question_comment',
    'answers_comment',
  ];
  if (in_array($comment_type, $answers_types)) {
    unset($links['comment']['#links']['comment-reply']);
  }
}
function answers_core_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'node_links_alter') {

    // Move my_module_form_alter() to the end of the list.
    // \Drupal::moduleHandler()->getImplementations()
    // iterates through $implementations with a foreach loop which PHP iterates
    // in the order that the items were added, so to move an item to the end of
    // the array, we remove it and then add it.
    $group = $implementations['answers_core'];
    unset($implementations['answers_core']);
    $implementations['answers_core'] = $group;
  }
}

// function answers_core_form_alter($form, $form_state, $form_id) {
//   switch ($form_id) {
//     case 'comment_answers_question_comment_form':
//       // $form['#attributes']['class'][] = 'hidden';
//       break;
//     }
// }