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