Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Comments
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 create
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getTitle
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 getCacheMaxAge
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Drupal\crm\Plugin\Menu\LocalTask;
4
5use Drupal\Core\Menu\LocalTaskDefault;
6use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7use Drupal\Core\Routing\RouteMatchInterface;
8use Drupal\Core\StringTranslation\StringTranslationTrait;
9use Drupal\comment\CommentStorageInterface;
10use Drupal\crm\CrmContactInterface;
11use Symfony\Component\DependencyInjection\ContainerInterface;
12use Symfony\Component\HttpFoundation\Request;
13
14/**
15 * Provides a local task that shows the amount of comments.
16 */
17final class Comments extends LocalTaskDefault implements ContainerFactoryPluginInterface {
18  use StringTranslationTrait;
19
20  /**
21   * The comment storage service.
22   *
23   * @var \Drupal\comment\CommentStorageInterface
24   */
25  protected $commentStorage;
26
27  /**
28   * The route match service.
29   *
30   * @var \Drupal\Core\Routing\RouteMatchInterface
31   */
32  protected $routeMatch;
33
34  /**
35   * Construct the UnapprovedComments object.
36   *
37   * @param array $configuration
38   *   A configuration array containing information about the plugin instance.
39   * @param string $plugin_id
40   *   The plugin_id for the plugin instance.
41   * @param array $plugin_definition
42   *   The plugin implementation definition.
43   * @param \Drupal\comment\CommentStorageInterface $comment_storage
44   *   The comment storage service.
45   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
46   *   The route match service.
47   */
48  public function __construct(
49    array $configuration,
50    $plugin_id,
51    array $plugin_definition,
52    CommentStorageInterface $comment_storage,
53    RouteMatchInterface $route_match,
54  ) {
55
56    parent::__construct($configuration, $plugin_id, $plugin_definition);
57    $this->commentStorage = $comment_storage;
58    $this->routeMatch = $route_match;
59  }
60
61  /**
62   * {@inheritdoc}
63   */
64  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
65    return new self(
66      $configuration,
67      $plugin_id,
68      $plugin_definition,
69      $container->get('entity_type.manager')->getStorage('comment'),
70      $container->get('current_route_match')
71    );
72  }
73
74  /**
75   * {@inheritdoc}
76   */
77  public function getTitle(?Request $request = NULL) {
78    $contact_id = $this->routeMatch->getParameter('crm_contact');
79    if ($contact_id instanceof CrmContactInterface) {
80      $contact_id = $contact_id->id();
81    }
82    $count = $this->commentStorage->getQuery()
83      ->condition('entity_type', 'crm_contact')
84      ->condition('entity_id', $contact_id)
85      ->condition('status', 1)
86      ->condition('field_name', 'field_notes')
87      ->accessCheck(TRUE)
88      ->count()
89      ->execute();
90
91    return $this->formatPlural($count, 'Note (1)', 'Notes (@count)');
92  }
93
94  /**
95   * {@inheritdoc}
96   */
97  public function getCacheMaxAge() {
98    return 0;
99  }
100
101}