improvements-2.x-dev/modules/improvements_comment/improvements_comment.module
modules/improvements_comment/improvements_comment.module
<?php
use Drupal\comment\CommentInterface;
use Drupal\comment\CommentTypeInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
use Drupal\druhels\UserHelper;
/**
* Implements hook_page_ROUTE_NAME_result_alter(): comment.reply.
*/
function improvements_comment_page_comment_reply_result_alter(array &$result): void {
// Hide commented entity on comment form page
if (isset($result['commented_entity'])) {
$result['commented_entity']['#access'] = FALSE;
}
}
/**
* Implements hook_entity_base_field_info_alter().
*
* @param BaseFieldDefinition[] $fields
*
* @see improvements_entity_base_field_info_alter()
*/
function improvements_comment_entity_base_field_info_alter(array &$fields, EntityTypeInterface $entity_type): void {
if ($entity_type->id() == 'comment') {
// Add comment author and date on comment view display
/** @see \Drupal\comment\Entity\Comment::baseFieldDefinitions() */
$fields['uid']->setDisplayConfigurable('view', TRUE);
$fields['name']->setDisplayConfigurable('view', TRUE)->setLabel(t('Author name'));
$fields['mail']->setDisplayConfigurable('view', TRUE);
$fields['created']->setDisplayConfigurable('view', TRUE);
$fields['entity_id']->setDisplayConfigurable('view', TRUE)->setLabel(t('Commented entity'));
$fields['created']->setLabel(t('Date of creation'));
$fields['changed']->setLabel(t('Date of change'));
}
}
/**
* Implements hook_comment_links_alter().
*/
function improvements_comment_comment_links_alter(array &$links, CommentInterface $comment, array &$context): void {
static $show_ban_link;
if ($show_ban_link === NULL) {
$show_ban_link = \Drupal::service('module_handler')->moduleExists('ban') && \Drupal::currentUser()->hasPermission('ban IP addresses');
}
if ($show_ban_link) {
$links['comment']['#links']['comment-ban-author'] = [
'title' => t('To ban'),
'url' => Url::fromRoute('ban.admin_page', ['default_ip' => $comment->getHostname()], ['query' => \Drupal::destination()->getAsArray()]),
];
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter(): comment_type_form.
*/
function improvements_comment_form_comment_type_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
$comment_type = $form_state->getFormObject()->getEntity(); /** @var CommentTypeInterface $comment_type */
$form['new_comment_notify'] = [
'#type' => 'checkbox',
'#title' => t('Notify of new comments'),
'#default_value' => $comment_type->getThirdPartySetting('improvements', 'new_comment_notify', FALSE),
];
$form['new_comment_notify_email'] = [
'#type' => 'textfield',
'#title' => t('E-mail for notify'),
'#description' => t('Support tokens.'),
'#default_value' => $comment_type->getThirdPartySetting('improvements', 'new_comment_notify_email', '[site:mail]'),
'#states' => [
'visible' => [
'input[name="new_comment_notify"]' => [
'checked' => TRUE,
],
],
],
];
$form['new_comment_notify_view_mode'] = [
'#type' => 'select',
'#title' => t('View mode for notify'),
'#options' => \Drupal::service('entity_display.repository')->getViewModeOptions('comment'),
'#default_value' => $comment_type->getThirdPartySetting('improvements', 'new_comment_notify_view_mode', 'default'),
'#states' => $form['new_comment_notify_email']['#states'],
];
$form['#entity_builders'][] = 'improvements_comment_form_comment_type_form_entity_builder';
}
/**
* Custom submit callback for comment_type_form form.
*
* @see improvements_form_comment_type_form_alter()
*/
function improvements_comment_form_comment_type_form_entity_builder(string $comment_type_id, CommentTypeInterface $comment_type, array &$form, FormStateInterface $form_state): void {
$comment_type->setThirdPartySetting('improvements', 'new_comment_notify', (bool)$form_state->getValue('new_comment_notify'));
$comment_type->setThirdPartySetting('improvements', 'new_comment_notify_email', $form_state->getValue('new_comment_notify_email'));
$comment_type->setThirdPartySetting('improvements', 'new_comment_notify_view_mode', $form_state->getValue('new_comment_notify_view_mode'));
}
/**
* Implements hook_ENTITY_TYPE_insert(): comment.
*/
function improvements_comment_comment_insert(CommentInterface $comment): void {
$comment_type = $comment->get('comment_type')->entity; /** @var CommentTypeInterface $comment_type */
$comment_improvements_settings = $comment_type->getThirdPartySettings('improvements');
if (
!empty($comment_improvements_settings['new_comment_notify']) &&
!empty($comment_improvements_settings['new_comment_notify_email'])
) {
\Drupal::service('plugin.manager.mail')->mail(
'improvements',
'new_comment_notify',
\Drupal::token()->replace($comment_improvements_settings['new_comment_notify_email']),
\Drupal::languageManager()->getCurrentLanguage()->getId(),
['comment' => $comment]
);
}
}
/**
* Implements hook_mail().
*/
function improvements_comment_mail(string $key, array &$message, array $params): void {
if ($key == 'new_comment_notify') {
$comment = $params['comment']; /** @var CommentInterface $comment */
$comment_type = $comment->get('comment_type')->entity; /** @var CommentTypeInterface $comment_type */
$comment_view_mode = $comment_type->getThirdPartySetting('improvements', 'new_comment_notify_view_mode', 'default');
$message['subject'] = t('New comment on @site in "@label"', [
'@site' => \Drupal::request()->getHost(),
'@label' => $comment->getCommentedEntity()->label(),
]);
$comment_build = \Drupal::entityTypeManager()->getViewBuilder('comment')->view($comment, $comment_view_mode);
// Render as admin
$account_switcher = \Drupal::service('account_switcher');
$account_switcher->switchTo(UserHelper::getAdminUser());
$message['body'][] = Markup::create(\Drupal::service('renderer')->renderPlain($comment_build));
$account_switcher->switchBack();
}
}
