ptalk-8.x-0.x-dev/modules/block_user/ptalk_block_user.module

modules/block_user/ptalk_block_user.module
<?php

/**
 * @file
 * Allows users to block other users from sending them any messages
 */

use Drupal\Core\Entity\EntityInterface;
use Drupal\views\ViewExecutable;

/**
 * Disallow blocking private messages from a user.
 */
define('PM_BLOCK_USER_DISALLOW_BLOCKING', 0);

/**
 * Disallow sending private messages to a user.
 */
define('PM_BLOCK_USER_DISALLOW_SENDING', 1);

/**
 * Implements hook_entity_type_build().
 */
function ptalk_block_user_entity_type_build(array &$entity_types) {
  // @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[]
  // Register block_author and unblock_author forms for the messages.
  $entity_types['ptalk_message']
    ->setFormClass('block_author', 'Drupal\ptalk_block_user\Form\BlockAuthorForm')
    ->setFormClass('unblock_author', 'Drupal\ptalk_block_user\Form\UnBlockAuthorForm')
    ->setLinkTemplate('block-author', '/private/message/{ptalk_message}/block-author')
    ->setLinkTemplate('unblock-author', '/private/message/{ptalk_message}/unblock-author');
}

/**
 * Implements hook_ptalk_message_links_alter().
 */
function ptalk_block_user_ptalk_message_links_alter(array &$links, EntityInterface $entity, array &$context) {
  // Show link block/unblock on the message where author is not current user
  // and where message is not deleted.
  if (!$entity->isCurrentUserOwner() && !$entity->isDeleted()) {
    if (is_null($entity->index->is_blocked)) {
      $links['ptalk_block_author_link'] = [
        '#theme' => 'links__message__ptalk',
        '#attributes' => ['class' => ['links', 'inline']],
        '#links' => [
          'ptalk-block-author-link' => [
            'title' => t('Block'),
            'attributes' => ['title' => t('Block author of this message.')],
            'url' => $entity->toUrl('block-author'),
          ],
        ],
      ];
    }
    else {
      $links['ptalk_unblock_author_link'] = [
        '#theme' => 'links__message__ptalk',
        '#attributes' => ['class' => ['links', 'inline']],
        '#links' => [
          'ptalk-unblock-author-link' => [
            'title' => t('Unblock'),
            'attributes' => ['title' => t('Unblock author of this message.')],
            'url' => $entity->toUrl('unblock-author'),
          ],
        ],
      ];
    }
  }
}

/**
 * Implements hook_query_ptalk_message_index_alter().
 */
function ptalk_block_user_query_ptalk_message_index_alter($query) {
  // Attachs to the message index if some recipients of the message is blocked by author.
  $query->leftJoin('ptalk_block_user', 'pbu', "pm.author = pbu.author AND pmi.recipient = pbu.recipient");
  $query->addField('pbu', 'recipient', 'is_blocked');
}

/**
 * Implements hook_query_ptalk_load_message_recipients_alter().
 */
function ptalk_block_user_query_ptalk_load_message_recipients_alter($query) {
  // Loads information about state of the author of the message -
  // is author blocked by some recipients of the message or not.
  // Author of the message is current user.
  $query->leftJoin('ptalk_block_user', 'pbu', ":current_user = pbu.author AND u.uid = pbu.recipient", [':current_user' => \Drupal::currentUser()->id()]);
  $query->addField('pbu', 'recipient', 'is_blocked');
}

/**
 * Implements hook_ptalk_block_message().
 */
function ptalk_block_user_ptalk_block_message($author, $recipients, $context = []) {
  $blocked = [];
  foreach ($recipients as $recipient) {
    if ($recipient->recipient_info->is_blocked) {
      if (!isset($blocked['recipients']['author_blocked'])) {
        $blocked['recipients']['author_blocked'] = [
          'ids' => [$recipient->id() => ptalk_participant_format($recipient)],
          'message' => [
            'type' => 'warning',
            'plural' => t('You do not have access to write these recipients:'),
            'singular' => t('has chosen to block messages from you.')
          ]
        ];
      }
      else {
        $blocked['recipients']['author_blocked']['ids'][$recipient->id()] = ptalk_participant_format($recipient);
      }
    }
  }

  return $blocked;
}

/**
 * Checks if author is blocked by the recipient.
 *
 * @param $author
 *   The user that would send a message.
 * @param $recipient
 *   The user that would receive the message.
 * @return
 *   TRUE if the recipient has blocked the author.
 */
function ptalk_block_user_author_is_blocked($author, $recipient) {
  $database = \Drupal::database();
  return (bool) $database->query('SELECT 1 FROM {ptalk_block_user} WHERE author = :author AND recipient = :recipient', [':author' => $author->id(), ':recipient' => $recipient->id()])->fetchField();
}

/**
 * Implements hook_query_ptalk_handle_autocomplete_alter().
 */
function ptalk_block_user_query_ptalk_handle_autocomplete_alter($query) {
  $database = \Drupal::database();
  // Gets all potential authors which are blocked by the current user.
  $blocked = $database->select('ptalk_block_user', 'pbu')
    ->fields('pbu', ['author'])
    ->condition('pbu.recipient', \Drupal::currentUser()->id());

  // Exclude these from the possible recipients.
  $query->condition('u.uid', $blocked, 'NOT IN');
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc