answers-8.x-1.x-dev/modules/core/answers_core.module
modules/core/answers_core.module
<?php
/**
* @file
* Hook implementation code for the Answers core module.
*/
use Drupal\comment\CommentInterface;
use Drupal\node\NodeInterface;
/**
* 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_question' => [
'base hook' => 'comment',
],
'node__answers_question' => [
'base hook' => 'node',
],
'field__node__answers__answers_question' => [
'base hook' => 'field',
],
'field__node__answers_comments__answers_question' => [
'base hook' => 'field',
],
];
}
/**
* Implements hook_preprocess().
*/
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 ((\Drupal::time()->getRequestTime() - $date) < 86400) {
$variables['date'] = 'today';
}
else {
$variables['date'] = \Drupal::service('date.formatter')->formatInterval(\Drupal::time()->getRequestTime() - $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');
}
elseif ($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']);
}
}
/**
* Implements hook_module_implements_alter().
*/
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;
}
}
