og-8.x-1.x-dev/src/Plugin/Derivative/OgActionLink.php

src/Plugin/Derivative/OgActionLink.php
<?php

declare(strict_types=1);

namespace Drupal\og\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\og\GroupTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides action link definitions for all entity bundles.
 */
class OgActionLink extends DeriverBase implements ContainerDeriverInterface {

  use StringTranslationTrait;

  public function __construct(
    protected readonly GroupTypeManagerInterface $groupTypeManager,
    protected readonly RouteProviderInterface $routeProvider,
  ) {}

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static(
      $container->get('og.group_type_manager'),
      $container->get('router.route_provider')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $derivatives = [];

    foreach (array_keys($this->groupTypeManager->getGroupMap()) as $entity_type_id) {
      $route_name = "entity.$entity_type_id.og_admin_routes.add_membership_page";

      if (!$this->routeProvider->getRoutesByNames([$route_name])) {
        // Route not found.
        continue;
      }

      $derivatives["og_membership.$entity_type_id.add"] = [
        'route_name' => $route_name,
        'title' => $this->t('Add a member'),
        'appears_on' => ["entity.$entity_type_id.og_admin_routes.members"],
      ];
    }

    foreach ($derivatives as &$entry) {
      $entry += $base_plugin_definition;
    }

    return $derivatives;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc