ฉันจะสร้างแท็บผู้ใช้ที่กำหนดเองได้อย่างไร


9

ฉันกำลังพยายามสร้างแท็บที่กำหนดเองใหม่ที่ปรากฏบนเส้นทางทั้งหมดที่เป็นลูกหลานของเอนทิตี {entity_type} .canonical ฉันได้ลองขยายคลาส DeriverBase โดยเฉพาะการเอาชนะเมธอด getDerivativeDefinitions ฉันสร้างแท็บเองโดยขยาย LocalTaskDefault และแทนที่เมธอด getRouteParameters แท็บจะปรากฏขึ้นเมื่อคุณเยี่ยมชมเส้นทางผู้ใช้ Drupal มาตรฐานเช่น www.mysite.com/user/1/ หรือ www.mysite.com/user/1/edit อย่างไรก็ตามเมื่อเราเพิ่มเส้นทางผู้ใช้ที่กำหนดเองใหม่ของเราเช่น 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 กำลังทำอยู่กับ / devel route / local task ฉันขอแนะนำให้คุณดูว่ามันใช้งานอย่างไร
Berdir

@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 และภายในกำหนด deriver

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

สร้างคลาส MyModuleLocalTasks เพื่อสร้างเส้นทางแบบไดนามิกของคุณใน 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.