Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| CommentTask | |
0.00% |
0 / 36 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| create | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| getTitle | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
42 | |||
| getCacheTags | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Drupal\crm\Plugin\Menu\LocalTask; |
| 4 | |
| 5 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 6 | use Drupal\Core\Extension\ModuleHandlerInterface; |
| 7 | use Drupal\Core\Menu\LocalTaskDefault; |
| 8 | use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
| 9 | use Drupal\Core\Routing\RouteMatchInterface; |
| 10 | use Drupal\Core\StringTranslation\StringTranslationTrait; |
| 11 | use Drupal\crm\Entity\ContactInterface; |
| 12 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 13 | use Symfony\Component\HttpFoundation\Request; |
| 14 | |
| 15 | /** |
| 16 | * Provides a local task that shows the amount of comments. |
| 17 | */ |
| 18 | class CommentTask extends LocalTaskDefault implements ContainerFactoryPluginInterface { |
| 19 | use StringTranslationTrait; |
| 20 | |
| 21 | /** |
| 22 | * The entity type manager service. |
| 23 | * |
| 24 | * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
| 25 | */ |
| 26 | protected $entityTypeManager; |
| 27 | |
| 28 | /** |
| 29 | * The route match service. |
| 30 | * |
| 31 | * @var \Drupal\Core\Routing\RouteMatchInterface |
| 32 | */ |
| 33 | protected $routeMatch; |
| 34 | |
| 35 | /** |
| 36 | * The module handler service. |
| 37 | * |
| 38 | * @var \Drupal\Core\Extension\ModuleHandlerInterface |
| 39 | */ |
| 40 | protected $moduleHandler; |
| 41 | |
| 42 | /** |
| 43 | * Construct the Comment object. |
| 44 | * |
| 45 | * @param array $configuration |
| 46 | * A configuration array containing information about the plugin instance. |
| 47 | * @param string $plugin_id |
| 48 | * The plugin_id for the plugin instance. |
| 49 | * @param array $plugin_definition |
| 50 | * The plugin implementation definition. |
| 51 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
| 52 | * The entity type manager service. |
| 53 | * @param \Drupal\Core\Routing\RouteMatchInterface $route_match |
| 54 | * The route match service. |
| 55 | * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler |
| 56 | * The module handler service. |
| 57 | */ |
| 58 | public function __construct( |
| 59 | array $configuration, |
| 60 | $plugin_id, |
| 61 | array $plugin_definition, |
| 62 | EntityTypeManagerInterface $entity_type_manager, |
| 63 | RouteMatchInterface $route_match, |
| 64 | ModuleHandlerInterface $module_handler, |
| 65 | ) { |
| 66 | |
| 67 | parent::__construct($configuration, $plugin_id, $plugin_definition); |
| 68 | $this->entityTypeManager = $entity_type_manager; |
| 69 | $this->routeMatch = $route_match; |
| 70 | $this->moduleHandler = $module_handler; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * {@inheritdoc} |
| 75 | */ |
| 76 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
| 77 | return new self( |
| 78 | $configuration, |
| 79 | $plugin_id, |
| 80 | $plugin_definition, |
| 81 | $container->get('entity_type.manager'), |
| 82 | $container->get('current_route_match'), |
| 83 | $container->get('module_handler') |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * {@inheritdoc} |
| 89 | */ |
| 90 | public function getTitle(?Request $request = NULL) { |
| 91 | // Return default title if comment module is not enabled. |
| 92 | if (!$this->moduleHandler->moduleExists('comment')) { |
| 93 | return $this->t('Comments'); |
| 94 | } |
| 95 | |
| 96 | $contact = $this->routeMatch->getParameter('crm_contact'); |
| 97 | // Check if contact has comment field. |
| 98 | if ($contact instanceof ContactInterface && !$contact->hasField('comment')) { |
| 99 | return $this->t('Comments'); |
| 100 | } |
| 101 | |
| 102 | $contact_id = $contact; |
| 103 | if ($contact_id instanceof ContactInterface) { |
| 104 | $contact_id = $contact_id->id(); |
| 105 | } |
| 106 | |
| 107 | try { |
| 108 | $storage = $this->entityTypeManager->getStorage('comment'); |
| 109 | $count = $storage->getQuery() |
| 110 | ->condition('entity_type', 'crm_contact') |
| 111 | ->condition('entity_id', $contact_id) |
| 112 | ->condition('status', 1) |
| 113 | ->condition('field_name', 'comment') |
| 114 | ->accessCheck(TRUE) |
| 115 | ->count() |
| 116 | ->execute(); |
| 117 | |
| 118 | return $this->formatPlural($count, 'Comment (1)', 'Comments (@count)'); |
| 119 | } |
| 120 | catch (\Exception $e) { |
| 121 | return $this->t('Comments'); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * {@inheritdoc} |
| 127 | */ |
| 128 | public function getCacheTags() { |
| 129 | |
| 130 | $contact_id = $this->routeMatch->getParameter('crm_contact'); |
| 131 | if ($contact_id instanceof ContactInterface) { |
| 132 | $contact_id = $contact_id->id(); |
| 133 | } |
| 134 | |
| 135 | return ['crm_contact:' . $contact_id]; |
| 136 | } |
| 137 | |
| 138 | } |