You are here

public function PhotosImage::comments in Album Photos 8.4

Comments on single picture.

File

src/PhotosImage.php, line 354

Class

PhotosImage
Create images object.

Namespace

Drupal\photos

Code

public function comments($com_count, $node) {
  $fid = $this->fid;
  $output = [];
  if (\Drupal::moduleHandler()
    ->moduleExists('comment') && \Drupal::currentUser()
    ->hasPermission('access comments')) {

    // @todo get other comment form if needed?
    if ($com_count && ($node->comment && $node->comment->status != 0 || $node->comment_photos && $node->comment_photos->status != 0) || \Drupal::currentUser()
      ->hasPermission('administer comments')) {

      // @todo look up setting for photos comment field.
      $mode = CommentManagerInterface::COMMENT_MODE_THREADED;

      // @todo look up setting for photos comment field.
      $comments_per_page = 50;
      $db = \Drupal::database();
      $query = $db
        ->select('photos_comment', 'v')
        ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');
      $query
        ->join('comment_field_data', 'c', 'c.cid = v.cid');
      $query
        ->addField('v', 'cid');
      $query
        ->condition('v.fid', $fid)
        ->addTag('node_access')
        ->addTag('comment_filter')
        ->addMetaData('base_table', 'node')
        ->limit($comments_per_page);
      $db = \Drupal::database();
      $count_query = $db
        ->select('photos_comment', 'v');
      $count_query
        ->join('comment_field_data', 'c', 'c.cid = v.cid');
      $count_query
        ->addExpression('COUNT(*)');
      $count_query
        ->condition('v.fid', $fid)
        ->addTag('node_access')
        ->addTag('comment_filter')
        ->addMetaData('base_table', 'node');
      if (!\Drupal::currentUser()
        ->hasPermission('administer comments')) {
        $query
          ->condition('c.status', CommentInterface::PUBLISHED);
        $count_query
          ->condition('c.status', CommentInterface::PUBLISHED);
      }
      if ($mode === CommentManagerInterface::COMMENT_MODE_FLAT) {
        $query
          ->orderBy('c.cid', 'ASC');
      }
      else {
        $query
          ->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
        $query
          ->orderBy('torder', 'ASC');
      }
      $query
        ->setCountQuery($count_query);
      $cids = $query
        ->execute()
        ->fetchCol();
      if (!empty($cids)) {
        $comments = \Drupal::entityTypeManager()
          ->getStorage('comment')
          ->loadMultiple($cids);
        $build = \Drupal::entityTypeManager()
          ->getViewBuilder('comment')
          ->viewMultiple($comments);
        $build['pager']['#type'] = 'pager';
        $output['comments'] = $build;
      }
    }
    if ($node->comment && $node->comment->status == 2 || $node->comment_photos && $node->comment_photos->status == 2) {
      $field_name = 'comment';

      // @todo get other comment form if needed?
      if ($node->comment_photos) {
        $field_name = 'comment_photos';
      }
      if (\Drupal::currentUser()
        ->hasPermission('post comments') && \Drupal::config('photos.settings')
        ->get('photos_comment') || \Drupal::currentUser()
        ->hasPermission('administer comments') && \Drupal::config('photos.settings')
        ->get('photos_comment')) {

        // Prep comment form.
        $definition = \Drupal::entityTypeManager()
          ->getDefinition('comment');
        $bundle_key = $definition
          ->get('entity_keys')['bundle'];
        $values = [
          'entity_type' => 'node',
          'field_name' => $field_name,
          $bundle_key => 'comment',
          'entity_id' => [
            'target_id' => $node
              ->id(),
          ],
        ];

        // Build comment entity for form.
        $entity = \Drupal::entityTypeManager()
          ->getStorage($definition
          ->get('id'))
          ->create($values);

        // Get entity form.
        $build = \Drupal::service('entity.form_builder')
          ->getForm($entity);
        $output['comment_form'] = $build;
      }
    }
  }
  if ($output) {
    if (\Drupal::moduleHandler()
      ->moduleExists('ajax_comments')) {

      // Add support for ajax comments on image page.
      $output['comments']['#prefix'] = '<div id="comment-wrapper-nid-' . $node
        ->id() . '">';
      $output['comments']['#prefix'] .= '<div class="ajax-comment-wrapper"></div>';
      $output['comments']['#suffix'] = '</div>';
    }
  }
  return $output;
}