You are here

class RedhenContactRouteContext in RedHen CRM 8

Sets the current contact as context on redhen contact routes.

@todo Remove once core gets a generic EntityRouteContext.

Hierarchy

Expanded class hierarchy of RedhenContactRouteContext

1 string reference to 'RedhenContactRouteContext'
redhen_contact.services.yml in modules/redhen_contact/redhen_contact.services.yml
modules/redhen_contact/redhen_contact.services.yml
1 service uses RedhenContactRouteContext
redhen_contact.contact_route_context in modules/redhen_contact/redhen_contact.services.yml
Drupal\redhen_contact\ContextProvider\RedhenContactRouteContext

File

modules/redhen_contact/src/ContextProvider/RedhenContactRouteContext.php, line 19

Namespace

Drupal\redhen_contact\ContextProvider
View source
class RedhenContactRouteContext implements ContextProviderInterface {
  use StringTranslationTrait;

  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs a new RedhenContactRouteContext object.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   */
  public function __construct(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public function getRuntimeContexts(array $unqualified_context_ids) {
    $context_definition = new ContextDefinition('entity:redhen_contact', NULL, FALSE);
    $value = NULL;
    if ($contact = $this->routeMatch
      ->getParameter('redhen_contact')) {
      $value = $contact;
    }
    elseif ($this->routeMatch
      ->getRouteName() == 'entity.redhen_contact.add_form') {
      $contact_type = $this->routeMatch
        ->getParameter('redhen_contact_type');
      $value = Contact::create([
        'type' => $contact_type
          ->id(),
      ]);
    }
    $cacheability = new CacheableMetadata();
    $cacheability
      ->setCacheContexts([
      'route',
    ]);
    $context = new Context($context_definition, $value);
    $context
      ->addCacheableDependency($cacheability);
    return [
      'redhen_contact' => $context,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableContexts() {
    $context = EntityContext::fromEntityTypeId('redhen_contact', $this
      ->t('Contact from URL'));
    return [
      'redhen_contact' => $context,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RedhenContactRouteContext::$routeMatch protected property The route match.
RedhenContactRouteContext::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface::getAvailableContexts
RedhenContactRouteContext::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextProviderInterface::getRuntimeContexts
RedhenContactRouteContext::__construct public function Constructs a new RedhenContactRouteContext object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.