flat_comments.module in Flatcomments 8
Contains flat_comments.module.
File
flat_comments.moduleView source
<?php
/**
* @file
* Contains flat_comments.module.
*/
use Drupal\comment\CommentInterface;
use Drupal\Component\Utility\Number;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function flat_comments_help($route_name, RouteMatchInterface $route_match) {
$output = '';
switch ($route_name) {
// Main module help for the flat_comments module.
case 'help.page.flat_comments':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Make all comments replies to the main post, regardless of the reply link used.') . '</p>';
$output;
default:
}
return $output;
}
/**
* Implements hook_form_alter().
*/
function flat_comments_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var Drupal\field\Entity\FieldConfig $field */
$field = $form_state
->getFormObject()
->getEntity();
// This only applies for comment fields.
if ($field
->getType() !== 'comment') {
return;
}
$form['actions']['submit']['#submit'][] = 'flat_comments_save';
// Add note about flat_comments to comment display description.
$form['settings']['default_mode']['#description'] .= t(' <strong>flat_comments enabled:</strong> Leave this box unchecked to force replies to be to the main post instead of other comments.');
// Add option to remove reply link from comments.
$form['settings']['remove_reply_link'] = [
'#type' => 'checkbox',
'#title' => t('Do not show a reply link on comments'),
'#default_value' => $field
->getThirdPartySetting('flat_comments', 'remove_reply_link', FALSE),
];
}
function flat_comments_save(array &$form, FormStateInterface $form_state) {
/** @var Drupal\field\Entity\FieldConfig $field */
$field = $form_state
->getFormObject()
->getEntity();
$values = $form_state
->getValues();
$field
->setThirdPartySetting('flat_comments', 'remove_reply_link', $values['settings']['remove_reply_link']);
$field
->save();
}
/**
* Implementation of hook_comment_presave().
*/
function flat_comments_comment_presave(CommentInterface $comment) {
$entity = \Drupal::entityTypeManager()
->getStorage($comment
->getCommentedEntityTypeId())
->load($comment
->getCommentedEntityId());
$field_definition = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity
->getEntityTypeId(), $entity
->bundle());
$field_definition = $field_definition[$comment
->getFieldName()];
// Only affect new comments and comments set to be displayed flat.
if (!$comment
->id() && $field_definition
->getSetting('default_mode') === 0) {
// Set parent id to NULL to prevent threads.
$comment
->get('pid')
->setValue(NULL);
// Re-calculate the Thread.
$thread = flat_comments_calculate_thread($comment);
$comment
->setThread($thread);
}
}
/**
* By the time we reset the `pid` of the comment, the thread has been already
* calculated, so in order to make sure that the comments are completely
* flattened we need to recalculate the thread.
*
* This code was mainly taken from Drupal\comment\Entity\Comment::preSave();
*/
function flat_comments_calculate_thread($comment) {
$comment_storage = \Drupal::entityTypeManager()
->getStorage('comment');
// This is a comment with no parent comment (depth 0): we start
// by retrieving the maximum thread level.
$max = $comment_storage
->getMaxThread($comment);
// Strip the "/" from the end of the thread.
$max = rtrim($max, '/');
// We need to get the value at the correct depth.
$parts = explode('.', $max);
$n = Number::alphadecimalToInt($parts[0]);
return Number::intToAlphadecimal(++$n) . '/';
}
/**
* Implements hook_comments_links_alter().
*/
function flat_comments_comment_links_alter(array &$links, CommentInterface $entity, array &$context) {
// If there is not a "reply" link, no need to continue.
if (!isset($links['comment']['#links']['comment-reply'])) {
return;
}
$comment = $entity;
$entity = \Drupal::entityTypeManager()
->getStorage($comment
->getCommentedEntityTypeId())
->load($comment
->getCommentedEntityId());
$field_definition = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity
->getEntityTypeId(), $entity
->bundle());
$field_definition = $field_definition[$comment
->getFieldName()];
if ($field_definition
->getThirdPartySetting('flat_comments', 'remove_reply_link', FALSE)) {
unset($links['comment']['#links']['comment-reply']);
}
}
Functions
Name | Description |
---|---|
flat_comments_calculate_thread | By the time we reset the `pid` of the comment, the thread has been already calculated, so in order to make sure that the comments are completely flattened we need to recalculate the thread. |
flat_comments_comment_links_alter | Implements hook_comments_links_alter(). |
flat_comments_comment_presave | Implementation of hook_comment_presave(). |
flat_comments_form_field_config_edit_form_alter | Implements hook_form_alter(). |
flat_comments_help | Implements hook_help(). |
flat_comments_save |