Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
CommentController
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 commentsPage
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
2
 title
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 access
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\crm\Controller;
6
7use Drupal\Core\Controller\ControllerBase;
8use Drupal\Core\Extension\ModuleHandlerInterface;
9use Drupal\Core\Render\RendererInterface;
10use Drupal\crm\Entity\ContactInterface;
11use Drupal\Core\Access\AccessResult;
12use Symfony\Component\DependencyInjection\ContainerInterface;
13
14/**
15 * Controller for the comment page.
16 */
17class CommentController extends ControllerBase {
18
19  /**
20   * The renderer service.
21   *
22   * @var \Drupal\Core\Render\RendererInterface
23   */
24  protected $renderer;
25
26  /**
27   * The module handler service.
28   *
29   * @var \Drupal\Core\Extension\ModuleHandlerInterface
30   */
31  protected $moduleHandler;
32
33  /**
34   * Constructs a new CommentController.
35   *
36   * @param \Drupal\Core\Render\RendererInterface $renderer
37   *   The renderer service.
38   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
39   *   The module handler service.
40   */
41  public function __construct(RendererInterface $renderer, ModuleHandlerInterface $module_handler) {
42    $this->renderer      = $renderer;
43    $this->moduleHandler = $module_handler;
44  }
45
46  /**
47   * Creates a new CommentController.
48   *
49   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
50   *   The container.
51   *
52   * @return static
53   *   The created controller.
54   */
55  public static function create(ContainerInterface $container) {
56    return new static(
57      $container->get('renderer'),
58      $container->get('module_handler')
59    );
60  }
61
62  /**
63   * Builds the comments page.
64   *
65   * @param \Drupal\crm\ContactInterface $crm_contact
66   *   The contact.
67   *
68   * @return array
69   *   A render array.
70   */
71  public function commentsPage(ContactInterface $crm_contact) {
72    $contact_id = $crm_contact->id();
73    $build = [
74      '#cache' => [
75        'tags' => [
76          'crm_contact:' . $contact_id,
77        ],
78      ],
79      '#title' => $this->t('Comments about @label', ['@label' => $crm_contact->label()]),
80    ];
81
82    // Assumes the comment field is 'comment'.
83    $field_name = 'comment';
84
85    // Show comment list.
86    if ($crm_contact->hasField($field_name)) {
87      $build['comment'] = $this->entityTypeManager()
88        ->getViewBuilder('crm_contact')
89        ->viewField($crm_contact->get($field_name), 'comment');
90
91      $build['comment'][0]['comment_form']['#lazy_builder'] = [
92        'crm.comment.lazy_builders:renderForm',
93        [
94          $crm_contact->getEntityTypeId(),
95          $crm_contact->id(),
96          $field_name,
97          'crm_contact',
98        ],
99      ];
100    }
101
102    return $build;
103  }
104
105  /**
106   * The title of the comments page.
107   *
108   * @param \Drupal\crm\ContactInterface $crm_contact
109   *   The contact.
110   *
111   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
112   *   The title of the comments page.
113   */
114  public function title(ContactInterface $crm_contact) {
115    return $this->t('Comments for @label', ['@label' => $crm_contact->label()]);
116  }
117
118  /**
119   * Access check for the comments page.
120   *
121   * @param \Drupal\crm\ContactInterface $crm_contact
122   *   The contact.
123   *
124   * @return \Drupal\Core\Access\AccessResult
125   *   The access result.
126   */
127  public function access(ContactInterface $crm_contact) {
128    // Check if comment module is enabled.
129    if (!$this->moduleHandler->moduleExists('comment')) {
130      return AccessResult::forbidden('Comment module is not enabled.');
131    }
132
133    // Check if the contact has a comment field.
134    if (!$crm_contact->hasField('comment')) {
135      return AccessResult::forbidden('Contact type does not have a comment field.');
136    }
137
138    return AccessResult::allowedIf($crm_contact->access('view') && $this->currentUser()->hasPermission('access comments'));
139  }
140
141}