আমি কীভাবে কাস্টম ব্যবহারকারী ট্যাব তৈরি করব?


9

আমি এমন একটি নতুন কাস্টম ট্যাব তৈরি করার চেষ্টা করছি যা সত্তার বংশধর সমস্ত রুটে উপস্থিত হয়। {अस्तित्व_প্রকার}। ক্যানোনিকাল। আমি ডেরিভারবেস ক্লাসটি প্রসারিত করার চেষ্টা করেছি, বিশেষত getDerivativeDefinitions পদ্ধতি ওভাররাইড করে। আমি লোকালটাস্কডেফল্ট প্রসারিত করে এবং গেটরউটপ্যারামিটার পদ্ধতিটিকে ওভাররাইড করে ট্যাবটি নিজেই তৈরি করেছি। আপনি যখন www.mysite.com/user/1/ বা www.mysite.com/user/1/edit এর মতো কোনও মানক ড্রুপাল ব্যবহারকারীর পাথটি ঘুরে দেখেন তখন ট্যাবটি উপস্থিত হয়। যাইহোক, আমরা যখন আমাদের নতুন কাস্টম ব্যবহারকারী রুটগুলি যেমন www.mysite.com/user/1/sbscribe যোগ করি তখন কোনও ট্যাব উপস্থিত হয় না। কাস্টম রুটে স্থানীয় মেনু টাস্কগুলি সংজ্ঞায়িত করার কোনও বিশেষ উপায় আছে? কোডের একটি নমুনা:

 $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;
}

কোন সাহায্যের জন্য অগ্রিম ধন্যবাদ।


ডিভেল এটি / ডেভেল রুট / লোকাল টাস্কটি কী করছে তার খুব কাছাকাছি মনে হচ্ছে, আমি প্রস্তাব দিচ্ছি যে এটি কীভাবে বাস্তবায়ন করছে সে সম্পর্কে আপনার নজর দেওয়া উচিত।
বেরদির

@ বারডির এটিই সূচনা পয়েন্ট ছিল তবে আমি এখনও কিছু অনুপস্থিত বলে মনে করি।
tflanagan

আপনি কি আপনার কাস্টম ট্যাবের সেটিংসের সাথে 'yourmodule.links.task.yml' ফাইল যুক্ত করার চেষ্টা করেছিলেন?
অ্যান্ড্রু

উত্তর:


7

বার্ডিরের পরামর্শ অনুসারে আপনি ডেভেল মডিউল এবং এটি কীভাবে এটি বাস্তবায়ন করছে তা দেখতে পারেন। নিম্নলিখিত কোডটি দেভেল থেকে "নিষ্কাশিত" হয়েছিল

1) রুট তৈরি করুন

Mymodule.routing.yml ফাইলটি তৈরি করুন এবং এর ভিতরে একটি রুট কলব্যাক সংজ্ঞায়িত করুন (যা গতিশীল রুটগুলি তৈরি করতে ব্যবহৃত হয়)

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

এসআরসি / রাউটিংয়ে আপনার গতিশীল রুটগুলি উত্পন্ন করার জন্য ক্লাস মাইমডিউলআউটগুলি তৈরি করুন

<?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

এসআরসি / প্লাগইন / ডেরাইভেটিভে আপনার গতিশীল রুটগুলি তৈরি করার জন্য ক্লাস মাইমডিউললকাল টাস্ক তৈরি করুন

<?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) নিয়ামক তৈরি করুন

এসআরসি / কন্ট্রোলারে ক্লাস মাইমডুলকন্ট্রোলার তৈরি করুন

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
এটি আমি বাস্তবায়ন শেষ পর্যন্ত কি খুব অনুরূপ ছিল। রুটম্যাচআইনটারফেস $ রুট_ম্যাচ পাস করা আমার সমস্যার সমাধান ছিল। সেখান থেকে আমার সত্তার অবজেক্টটি আমার নিয়ামকের কাছে উপলব্ধ ছিল।
tflanagan
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.