You are here

quote.module in Quote 8.2

Allows users to quote posts or comments.

File

quote.module
View source
<?php

/**
 * @file
 * Allows users to quote posts or comments.
 */
use Drupal\node\NodeInterface;
use Drupal\comment\CommentInterface;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;

/**
 * Implements hook_help().
 */
function quote_help($path, $arg) {
  return '';
}

/**
 * Implements hook_node_links_alter().
 */
function quote_node_links_alter(array &$links, NodeInterface $entity, array &$context) {
  $config = \Drupal::config('quote.settings');

  // Get comments field name.
  $comment_field_name = '';
  $comment_fields = \Drupal::service('comment.manager')
    ->getFields('node');
  foreach ($comment_fields as $key => $value) {
    $entity
      ->hasField($key) ? $comment_field_name = $key : '';
  }
  if ($comment_field_name) {
    if (\Drupal::currentUser()
      ->hasPermission('post comments') && \Drupal::currentUser()
      ->hasPermission('use quote') && in_array($entity
      ->bundle(), array_filter($config
      ->get('quote_allow_types'))) && $entity
      ->get($comment_field_name)->status == CommentItemInterface::OPEN) {
      if ($config
        ->get('quote_modes_quote_sel')) {
        $link['node-quote-sel'] = [
          'title' => t('quote selected'),
          'url' => Url::fromUserInput('#', [
            'fragment' => '#',
          ]),
        ];
        $links['node']['#links']['node-quote-sel'] = $link['node-quote-sel'];
      }
      if ($config
        ->get('quote_modes_quote_all')) {
        $link['node-quote-all'] = [
          'title' => t('quote all'),
          'url' => Url::fromUserInput('#', [
            'fragment' => '#',
          ]),
        ];
        $links['node']['#links']['node-quote-all'] = $link['node-quote-all'];
      }
    }
  }
}

/**
 * Implements hook_comment_links_alter().
 */
function quote_comment_links_alter(array &$links, CommentInterface $entity, array &$context) {
  $config = \Drupal::config('quote.settings');
  if (\Drupal::currentUser()
    ->hasPermission('post comments') && \Drupal::currentUser()
    ->hasPermission('use quote') && $config
    ->get('quote_allow_comments') && in_array($entity
    ->getCommentedEntity()
    ->bundle(), array_filter($config
    ->get('quote_allow_types')))) {
    if ($config
      ->get('quote_modes_quote_sel')) {
      $link['comment-quote-sel'] = [
        'title' => t('quote selected'),
        'url' => Url::fromUserInput('#', [
          'fragment' => '#',
        ]),
      ];
      $links['comment']['#links']['comment-quote-sel'] = $link['comment-quote-sel'];
    }
    if ($config
      ->get('quote_modes_quote_all')) {
      $link['comment-quote-all'] = [
        'title' => t('quote all'),
        'url' => Url::fromUserInput('#', [
          'fragment' => '#',
        ]),
      ];
      $links['comment']['#links']['comment-quote-all'] = $link['comment-quote-all'];
    }
    if ($config
      ->get('quote_modes_quote_reply_all')) {
      $nid = $entity
        ->getCommentedEntityId();
      $cid = $entity
        ->id();
      $comment_field_name = $entity
        ->getFieldName();
      $link['comment-quote-all-reply'] = [
        'title' => t('reply and quote all'),
        'url' => Url::fromUserInput('/comment/reply/node/' . $nid . '/' . $comment_field_name . '/' . $cid),
        'query' => [
          'comment-quote-all-reply' => $cid,
        ],
      ];
      $links['comment']['#links']['comment-quote-all-reply'] = $link['comment-quote-all-reply'];
    }
  }
}

/**
 * Implements hook_page_attachments().
 */
function quote_page_attachments(array &$attachments) {
  $attachments['#attached']['library'][] = 'quote/quote';
  $config = \Drupal::config('quote.settings');
  $attachments['#attached']['drupalSettings']['quote']['quote_selector'] = $config
    ->get('quote_selector');
  $attachments['#attached']['drupalSettings']['quote']['quote_limit'] = $config
    ->get('quote_limit');
  $attachments['#attached']['drupalSettings']['quote']['quote_selector_comment_quote_all'] = $config
    ->get('quote_selector_comment_quote_all');
  $attachments['#attached']['drupalSettings']['quote']['quote_selector_node_quote_all'] = $config
    ->get('quote_selector_node_quote_all');
  $attachments['#attached']['drupalSettings']['quote']['quote_ckeditor_support'] = $config
    ->get('quote_ckeditor_support');
}

/**
 * Implements hook_form_alter().
 */
function quote_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (strpos($form_id, 'comment_') == 0 && isset($_GET['comment-quote-all-reply'])) {
    $comment = \Drupal::entityTypeManager()
      ->getStorage('comment')
      ->load($_GET['comment-quote-all-reply']);
    $config = \Drupal::config('quote.settings');
    $limit = $config
      ->get('quote_limit');
    $comment_limit = substr($comment
      ->get('comment_body')->value, 0, $limit);
    $form['comment_body']['widget']['0']['#default_value'] = '<blockquote><strong>' . t('@author wrote:', [
      '@author' => $comment
        ->getAuthorName(),
    ]) . '</strong> ' . $comment_limit . '</blockquote><p><br/></p>';
  }
}