View source
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\comment\CommentInterface;
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_question' => [
'base hook' => 'field',
],
];
}
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') {
}
if ($hook == 'form') {
}
}
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']);
}
}
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') {
$group = $implementations['answers_core'];
unset($implementations['answers_core']);
$implementations['answers_core'] = $group;
}
}