Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContactRouteContext
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRuntimeContexts
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
56
 getAvailableContexts
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Drupal\crm\ContextProvider;
4
5use Drupal\Core\Cache\CacheableMetadata;
6use Drupal\Core\Plugin\Context\Context;
7use Drupal\Core\Plugin\Context\ContextProviderInterface;
8use Drupal\Core\Plugin\Context\EntityContext;
9use Drupal\Core\Plugin\Context\EntityContextDefinition;
10use Drupal\Core\Routing\RouteMatchInterface;
11use Drupal\crm\Entity\Contact;
12use Drupal\Core\StringTranslation\StringTranslationTrait;
13
14/**
15 * Sets the current contact as a context on crm_contact routes.
16 */
17class ContactRouteContext implements ContextProviderInterface {
18
19  use StringTranslationTrait;
20
21  /**
22   * The route match object.
23   *
24   * @var \Drupal\Core\Routing\RouteMatchInterface
25   */
26  protected $routeMatch;
27
28  /**
29   * Constructs a new ContactRouteContext.
30   *
31   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
32   *   The route match object.
33   */
34  public function __construct(RouteMatchInterface $route_match) {
35    $this->routeMatch = $route_match;
36  }
37
38  /**
39   * {@inheritdoc}
40   */
41  public function getRuntimeContexts(array $unqualified_context_ids) {
42    $result = [];
43    $context_definition = EntityContextDefinition::create('crm_contact')->setRequired(FALSE);
44    $value = NULL;
45    if (($route_object = $this->routeMatch->getRouteObject())) {
46      $route_contexts = $route_object->getOption('parameters');
47      // Check for a crm_contact revision parameter first.
48      if (isset($route_contexts['crm_contact_revision']) && $revision = $this->routeMatch->getParameter('crm_contact_revision')) {
49        $value = $revision;
50      }
51      elseif (isset($route_contexts['crm_contact']) && $contact = $this->routeMatch->getParameter('crm_contact')) {
52        $value = $contact;
53      }
54      elseif ($this->routeMatch->getRouteName() == 'crm_contact.add') {
55        $contact_type = $this->routeMatch->getParameter('crm_contact_type');
56        $value = Contact::create(['bundle' => $contact_type->id()]);
57      }
58    }
59
60    $cacheability = new CacheableMetadata();
61    $cacheability->setCacheContexts(['route']);
62
63    $context = new Context($context_definition, $value);
64    $context->addCacheableDependency($cacheability);
65    $result['crm_contact'] = $context;
66
67    return $result;
68  }
69
70  /**
71   * {@inheritdoc}
72   */
73  public function getAvailableContexts() {
74    $context = EntityContext::fromEntityTypeId('crm_contact', $this->t('Contact from URL'));
75    return ['crm_contact' => $context];
76  }
77
78}