Guest User

Example code for a local actions deriver

a guest
Sep 23rd, 2026
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.59 KB | Source Code | 0 0
  1. <?php
  2.  
  3. namespace Drupal\listing_actions\Plugin\Derivative;
  4.  
  5. use Drupal\Component\Plugin\Derivative\DeriverBase;
  6. use Drupal\Core\Entity\EntityTypeManagerInterface;
  7. use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
  8. use Drupal\Core\StringTranslation\StringTranslationTrait;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10.  
  11. /**
  12.  * Provides creation actions for content listings filtered to one node type.
  13.  */
  14. class ContentLocalActions extends DeriverBase implements ContainerDeriverInterface {
  15.  
  16.   use StringTranslationTrait;
  17.  
  18.   /**
  19.    * Constructs the deriver.
  20.    *
  21.    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
  22.    *   The entity type manager.
  23.    */
  24.   public function __construct(protected EntityTypeManagerInterface $entityTypeManager) {}
  25.  
  26.   /**
  27.    * {@inheritdoc}
  28.    */
  29.   public static function create(ContainerInterface $container, $base_plugin_id) {
  30.     return new static($container->get('entity_type.manager'));
  31.   }
  32.  
  33.   /**
  34.    * {@inheritdoc}
  35.    */
  36.   public function getDerivativeDefinitions($base_plugin_definition): array {
  37.     $this->derivatives = [];
  38.     $view = $this->entityTypeManager->getStorage('view')->load('content');
  39.     if (!$view || !$view->status()) {
  40.       return $this->derivatives;
  41.     }
  42.  
  43.     foreach ($view->get('display') as $display_id => $display) {
  44.       if ($display['display_plugin'] !== 'page') {
  45.         continue;
  46.       }
  47.  
  48.       $options = $display['display_options'];
  49.       if (($options['enabled'] ?? TRUE) === FALSE) {
  50.         continue;
  51.       }
  52.  
  53.       // Only use filters explicitly overridden for this display.
  54.       if ($options['defaults']['filters'] ?? TRUE) {
  55.         continue;
  56.       }
  57.       $filter = $options['filters']['type_1'] ?? [];
  58.       $types = $filter['value'] ?? [];
  59.       if (($filter['entity_type'] ?? NULL) !== 'node' || ($filter['field'] ?? NULL) !== 'type') {
  60.         continue;
  61.       }
  62.       if (($filter['operator'] ?? NULL) !== 'in' || !empty($filter['exposed']) || count($types) !== 1) {
  63.         continue;
  64.       }
  65.  
  66.       $node_type = $this->entityTypeManager->getStorage('node_type')->load(reset($types));
  67.       if (!$node_type) {
  68.         continue;
  69.       }
  70.  
  71.       $this->derivatives[$display_id] = [
  72.         'title' => $this->t('Add @label', [
  73.           '@label' => $node_type->label(),
  74.         ]),
  75.         'route_name' => 'node.add',
  76.         'route_parameters' => [
  77.           'node_type' => $node_type->id(),
  78.         ],
  79.         'appears_on' => ['view.content.' . $display_id],
  80.       ] + $base_plugin_definition;
  81.     }
  82.  
  83.     return $this->derivatives;
  84.   }
  85.  
  86. }
  87.  
Tags: drupal
Advertisement
Add Comment
Please, Sign In to add comment