Commit 33d8bc1c authored by dpi's avatar dpi
Browse files

Issue #3310577 by dpi: Drupal 10 readiness

parent c0c45b47
Loading
Loading
Loading
Loading
+8 −7
Original line number Diff line number Diff line
Entity Route Context

https://www.drupal.org/project/entity_route_context
Copyright Daniel Phin (@dpi) https://www.drupal.org/u/dpi 2019

Copyright Daniel Phin (@dpi) https://www.drupal.org/u/dpi 2022

This project provides a service and context to determine if the current route
match is owned by a particular entity type, by way of link templates.
+4 −4
Original line number Diff line number Diff line
@@ -3,15 +3,15 @@
  "type": "drupal-module",
  "description": "Entity route context.",
  "keywords": ["Drupal"],
  "license": "GPL-2.0+",
  "license": "GPL-2.0-or-later",
  "homepage": "https://www.drupal.org/project/entity_route_context",
  "minimum-stability": "dev",
  "support": {
    "issues": "https://www.drupal.org/project/issues/entity_route_context",
    "source": "http://cgit.drupalcode.org/entity_route_context"
    "source": "https://git.drupalcode.org/project/entity_route_context"
  },
  "require": {
    "php": ">=7.3",
    "drupal/core": "^9"
    "php": ">=8.1",
    "drupal/core": ">=10"
  }
}
+2 −4
Original line number Diff line number Diff line
name: Entity Route Context
type: module
core_version_requirement: ^9
php: 7.3
core_version_requirement: '>=10'
php: 8.1
package: Utility
dependencies:
  - drupal:system (>=9.0.x)
+8 −2
Original line number Diff line number Diff line
services:
  entity_route_context.route_helper:
    class: Drupal\entity_route_context\EntityRouteContextRouteHelper
    arguments: ['@entity_type.manager', '@router.route_provider', '@cache.discovery']
    arguments:
      - '@entity_type.manager'
      - '@router.route_provider'
      - '@cache.discovery'

  entity_route_context.entity_route_context:
    class: Drupal\entity_route_context\ContextProvider\EntityRouteContext
    arguments: ['@entity_type.manager', '@current_route_match', '@entity_route_context.route_helper']
    arguments:
      - '@entity_type.manager'
      - '@current_route_match'
      - '@entity_route_context.route_helper'
    tags:
      - { name: 'context_provider' }
+10 −37
Original line number Diff line number Diff line
@@ -13,43 +13,22 @@ use Drupal\Core\Plugin\Context\ContextProviderInterface;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\entity_route_context\EntityRouteContextRouteHelperInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\entity_route_context\EntityRouteContextRouteHelperInterface;

/**
 * Determines if the route is owned by an entities link template.
 */
class EntityRouteContext implements ContextProviderInterface {
final class EntityRouteContext implements ContextProviderInterface {

  use StringTranslationTrait;

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

  /**
   * Entity route helper.
   *
   * @var \Drupal\entity_route_context\EntityRouteContextRouteHelperInterface
   */
  protected $helper;

  /**
   * Map of route matches to entity keyed by route name.
   *
   * @var \Drupal\Core\Entity\EntityInterface[]
   */
  protected $routeMatchedEntity = [];
  protected array $routeMatchedEntity = [];

  /**
   * Name of context variable.
@@ -63,18 +42,12 @@ class EntityRouteContext implements ContextProviderInterface {

  /**
   * Constructs a new EntityRouteContext.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match object.
   * @param \Drupal\entity_route_context\EntityRouteContextRouteHelperInterface $helper
   *   Entity route helper.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, RouteMatchInterface $route_match, EntityRouteContextRouteHelperInterface $helper) {
    $this->routeMatch = $route_match;
    $this->helper = $helper;
    $this->entityTypeManager = $entityTypeManager;
  public function __construct(
    protected EntityTypeManagerInterface $entityTypeManager,
    protected RouteMatchInterface $routeMatch,
    protected EntityRouteContextRouteHelperInterface $helper,
  ) {
  }

  /**
@@ -122,7 +95,7 @@ class EntityRouteContext implements ContextProviderInterface {
    // \Drupal\Core\Plugin\Context\ContextDefinition::dataTypeMatches allows us
    // to provide a generic 'entity', it will match on both 'entity' and more
    // specific types like 'entity:node'.
    $contextDefinition = new ContextDefinition('entity', $this->t('Entity from route'));
    $contextDefinition = new ContextDefinition('entity', (string) $this->t('Entity from route'));
    $context = new Context($contextDefinition);
    $contexts[static::CANONICAL_ENTITY] = $context;

@@ -135,7 +108,7 @@ class EntityRouteContext implements ContextProviderInterface {
    asort($entityTypes);

    foreach ($entityTypes as $entityTypeId => $entityTypeLabel) {
      $context = EntityContext::fromEntityTypeId($entityTypeId, $this->t('@entity_type from route', [
      $context = EntityContext::fromEntityTypeId($entityTypeId, (string) $this->t('@entity_type from route', [
        '@entity_type' => $entityTypeLabel,
      ]));
      $contexts[static::CANONICAL_ENTITY_PREFIX . $entityTypeId] = $context;
Loading