You are here

comment_ip.module in Comment IP 8

Same filename and directory in other branches
  1. 7 comment_ip.module

Comment IP primary module file.

File

comment_ip.module
View source
<?php

/**
 * @file
 * Comment IP primary module file.
 */
use Drupal\comment\Entity\Comment;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function comment_ip_form_comment_admin_overview_alter(&$form, FormStateInterface $form_state) {
  $user = \Drupal::currentUser();
  if ($user
    ->hasPermission('ban IP addresses')) {

    // Add commentors IP address to comment row.
    $form['options']['operation']['#options']['ban'] = t('Delete the selected comments and block their IPs');
  }
  $reorder = [
    'changed' => $form['comments']['#header']['changed'],
    'operations' => $form['comments']['#header']['operations'],
  ];
  unset($form['comments']['#header']['changed']);
  unset($form['comments']['#header']['operations']);
  $form['comments']['#header']['ip'] = [
    'data' => t('IP'),
    'specifier' => 'ip',
    'field' => 'hostname',
  ];
  $form['comments']['#header']['changed'] = $reorder['changed'];
  $form['comments']['#header']['operations'] = $reorder['operations'];
  foreach ($form['comments']['#options'] as $id => $option) {
    $comment = Comment::load($id);
    $form['comments']['#options'][$id]['ip'] = $comment
      ->getHostname();
  }
}