comment_delete.module in Comment Delete 8
File
comment_delete.module
View source
<?php
use Drupal\comment_delete\CommentDeleteSubmitter;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Form\FormStateInterface;
function comment_delete_comment_access(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation === 'delete') {
$config = \Drupal::config('comment_delete.config');
$time = \Drupal::time();
$threshold = $config
->get('threshold');
if ($threshold && $time
->getRequestTime() - $entity
->get('created')
->getString() >= $threshold) {
$expired = TRUE;
}
else {
$expired = FALSE;
}
if ($account
->hasPermission('delete any comment anytime')) {
return AccessResult::allowed();
}
elseif (!$expired && $account
->hasPermission('delete any comment')) {
return AccessResult::allowed();
}
if ($entity
->getOwnerId() === $account
->id()) {
if ($account
->hasPermission('delete own comment anytime')) {
return AccessResult::allowed();
}
elseif (!$expired && $account
->hasPermission('delete own comment')) {
return AccessResult::allowed();
}
}
}
return AccessResult::neutral();
}
function comment_delete_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$admin_context = \Drupal::service('router.admin_context');
if ($admin_context
->isAdminRoute()) {
return;
}
if (preg_match('/comment_(.*)_delete_form/i', $form_id)) {
$comment_delete_manager = \Drupal::service('comment_delete.manager');
$config = \Drupal::config('comment_delete.config');
$default_selection = $config
->get('default_selection');
$form['description']['#markup'] = t('This action cannot be undone.');
$options = $comment_delete_manager
->getOperations();
$form['delete_operation'] = [
'#type' => 'radios',
'#title' => t('How should replies to this comment be handled?'),
'#options' => $options,
'#required' => TRUE,
'#default_value' => isset($options[$default_selection]) ? $default_selection : 2,
];
if (count($options) === 1) {
$form['delete_operation']['#disabled'] = TRUE;
}
$form['actions']['submit']['#submit'] = [
[
CommentDeleteSubmitter::class,
'submitForm',
],
];
}
}
function comment_delete_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.comment_delete':
$output = '<p>' . t('Provides enhanced options for deleting comments and handling reply threads.') . '</p>';
$output .= '<h3>' . t('Features') . '</h3>';
$output .= '<dl>';
$output .= ' <dt>' . t('Permission based comment delete operations suitable for any role.') . '</dt>';
$output .= ' <dt>' . t('Non-administrators can delete their own comments.') . '</dt>';
$output .= ' <dt>' . t('Configurable max allowable time to delete comment.') . '</dt>';
$output .= ' <dt>' . t('Maintains proper reply thread order after deleting parent comment.') . '</dt>';
$output .= ' <dt>' . t('Soft delete mode to unset values or set comment unpublished.') . '</dt>';
$output .= ' <dt>' . t('Translatable delete confirmation message.') . '</dt>';
$output .= '</dl>';
$output .= '<h3>' . t('Usage') . '</h3>';
$output .= '<dl>';
$output .= ' <dt>' . t('Install the module.') . '</dt>';
$output .= ' <dt>' . t('Set permissions: /admin/people/permissions#module-comment_delete') . '</dt>';
$output .= ' <dt>' . t('Configure settings: /admin/config/system/comment-delete') . '</dt>';
$output .= '</dl>';
return $output;
}
return NULL;
}