カスタムユーザータブを作成するにはどうすればよいですか?


9

entity。{entity_type} .canonicalの子孫であるすべてのルートに表示される新しいカスタムタブを作成しようとしています。DeriverBaseクラスを拡張して、特にgetDerivativeDefinitionsメソッドをオーバーライドしてみました。LocalTask​​Defaultを拡張し、getRouteParametersメソッドをオーバーライドして、タブ自体を作成しました。このタブは、www.mysite.com / user / 1 /やwww.mysite.com/user/1/editなどの標準のDrupalユーザーパスにアクセスすると表示されます。ただし、www.mysite.com / user / 1 / subscribeなどの新しいカスタムユーザールートを追加しても、タブは表示されません。カスタムルートでローカルメニュータスクを定義する特別な方法はありますか?コードのサンプル:

 $this->derivatives['recurly.subscription_tab'] = [
  'title' => $this->t('Subscription'),
  'weight' => 5,
  'route_name' => 'recurly.subscription_list',
  'base_route' => "entity.$entity_type.canonical",
];

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

助けてくれてありがとう。


/ devel route / localタスクでdevelが行っていることに非常に近いように聞こえますが、それがどのように実装されているかを確認することをお勧めします。
Berdir 2016年

@Berdirが出発点でしたが、まだ何かが足りないようです。
tflanagan

カスタムタブの設定を含む「yourmodule.links.task.yml」ファイルを追加しようとしましたか?
Andrew

回答:


7

Berdirによって提案されているように、Develモジュールと、それがどのように実装されているかを確認できます。次のコードは、Develから「抽出」されました

1)ルートを作成する

mymodule.routing.ymlファイルを作成し、内部でルートコールバックを定義します(動的ルートの作成に使用されます)。

route_callbacks:
  - '\Drupal\mymodule\Routing\MyModuleRoutes::routes'

MyModuleRoutesクラスを作成して、src / Routingに動的ルートを生成します。

<?php

namespace Drupal\mymodule\Routing;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class MyModuleRoutes implements ContainerInjectionInterface {

  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager')
    );
  }

  public function routes() {
    $collection = new RouteCollection();

    foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
      if ($entity_type->hasLinkTemplate('canonical')) {
        $route = new Route("/mymodule/$entity_type_id/{{$entity_type_id}}");
        $route
          ->addDefaults([
            '_controller' => '\Drupal\mymodule\Controller\MyModuleController::doStuff',
            '_title' => 'My module route title',
          ])
          ->addRequirements([
            '_permission' => 'access mymodule permission',
          ])
          ->setOption('_mymodule_entity_type_id', $entity_type_id)
          ->setOption('parameters', [
            $entity_type_id => ['type' => 'entity:' . $entity_type_id],
          ]);

        $collection->add("entity.$entity_type_id.mymodule", $route);
      }
    }

    return $collection;
  }

}

2)動的ローカルタスクを作成する

ファイルmymodule.links.task.ymlを作成し、内部で派生を定義します

mymodule.tasks:
  class: \Drupal\Core\Menu\LocalTaskDefault
  deriver: \Drupal\mymodule\Plugin\Derivative\MyModuleLocalTasks

MyModuleLocalTask​​sクラスを作成して、src / Plugin / Derivativeに動的ルートを生成します

<?php

namespace Drupal\mymodule\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class MyModuleLocalTasks extends DeriverBase implements ContainerDeriverInterface {

  protected $entityTypeManager;

  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static(
      $container->get('entity_type.manager')
    );
  }

  public function getDerivativeDefinitions($base_plugin_definition) {
    $this->derivatives = array();

    foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
      if ($entity_type->hasLinkTemplate('canonical')) {
        $this->derivatives["$entity_type_id.mymodule_tab"] = [
          'route_name' => "entity.$entity_type_id.mymodule",
          'title' => t('Mymodule title'),
          'base_route' => "entity.$entity_type_id.canonical",
          'weight' => 100,
        ] + $base_plugin_definition;
      }
    }

    return $this->derivatives;
  }

}

3)コントローラを作成する

MyModuleControllerクラスをsrc / Controllerに作成します。

namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;

class MyModuleController extends ControllerBase {

  public function doStuff(RouteMatchInterface $route_match) {
    $output = [];

    $parameter_name = $route_match->getRouteObject()->getOption('_mymodule_entity_type_id');
    $entity = $route_match->getParameter($parameter_name);

    if ($entity && $entity instanceof EntityInterface) {
      $output = ['#markup' => $entity->label()];
    }

    return $output;
  }

}

3
これは私が最終的に実装したものと非常に似ていました。RouteMatchInterface $ route_matchを渡すことは私の問題の解決でした。そこから、私のエンティティオブジェクトをコントローラーで使用できるようになりました。
tflanagan
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.