You are here

comment_delete.module in Comment Delete 8

Same filename and directory in other branches
  1. 6 comment_delete.module
  2. 7 comment_delete.module

File

comment_delete.module
View source
<?php

/**
 * @file
 * comment_delete.module
 */
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;

/**
 * Implements hook_ENTITY_TYPE_access().
 *
 * Give access to the comment delete operation.
 */
function comment_delete_comment_access(EntityInterface $entity, $operation, AccountInterface $account) {

  /** @var \Drupal\comment\Entity\Comment $entity */
  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;
    }

    // Is able to delete any comment?
    if ($account
      ->hasPermission('delete any comment anytime')) {
      return AccessResult::allowed();
    }
    elseif (!$expired && $account
      ->hasPermission('delete any comment')) {
      return AccessResult::allowed();
    }

    // Is able to delete own comment?
    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();
}

/**
 * Implements hook_form_alter().
 *
 * Alter the comment_*.delete_form for each entity.
 */
function comment_delete_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\Core\Routing\AdminContext $admin_context */
  $admin_context = \Drupal::service('router.admin_context');
  if ($admin_context
    ->isAdminRoute()) {

    // Do nothing on administrative routes.
    return;
  }
  if (preg_match('/comment_(.*)_delete_form/i', $form_id)) {

    /** @var \Drupal\comment_delete\CommentDeleteManager $comment_delete_manager */
    $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.');

    // Defines operation types.
    $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',
      ],
    ];
  }
}

/**
 * Implements hook_help().
 */
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;
}