Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupal\listing_actions\Plugin\Derivative;
- use Drupal\Component\Plugin\Derivative\DeriverBase;
- use Drupal\Core\Entity\EntityTypeManagerInterface;
- use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
- use Drupal\Core\StringTranslation\StringTranslationTrait;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- /**
- * Provides creation actions for content listings filtered to one node type.
- */
- class ContentLocalActions extends DeriverBase implements ContainerDeriverInterface {
- use StringTranslationTrait;
- /**
- * Constructs the deriver.
- *
- * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
- * The entity type manager.
- */
- public function __construct(protected EntityTypeManagerInterface $entityTypeManager) {}
- /**
- * {@inheritdoc}
- */
- public static function create(ContainerInterface $container, $base_plugin_id) {
- return new static($container->get('entity_type.manager'));
- }
- /**
- * {@inheritdoc}
- */
- public function getDerivativeDefinitions($base_plugin_definition): array {
- $this->derivatives = [];
- $view = $this->entityTypeManager->getStorage('view')->load('content');
- if (!$view || !$view->status()) {
- return $this->derivatives;
- }
- foreach ($view->get('display') as $display_id => $display) {
- if ($display['display_plugin'] !== 'page') {
- continue;
- }
- $options = $display['display_options'];
- if (($options['enabled'] ?? TRUE) === FALSE) {
- continue;
- }
- // Only use filters explicitly overridden for this display.
- if ($options['defaults']['filters'] ?? TRUE) {
- continue;
- }
- $filter = $options['filters']['type_1'] ?? [];
- $types = $filter['value'] ?? [];
- if (($filter['entity_type'] ?? NULL) !== 'node' || ($filter['field'] ?? NULL) !== 'type') {
- continue;
- }
- if (($filter['operator'] ?? NULL) !== 'in' || !empty($filter['exposed']) || count($types) !== 1) {
- continue;
- }
- $node_type = $this->entityTypeManager->getStorage('node_type')->load(reset($types));
- if (!$node_type) {
- continue;
- }
- $this->derivatives[$display_id] = [
- 'title' => $this->t('Add @label', [
- '@label' => $node_type->label(),
- ]),
- 'route_name' => 'node.add',
- 'route_parameters' => [
- 'node_type' => $node_type->id(),
- ],
- 'appears_on' => ['view.content.' . $display_id],
- ] + $base_plugin_definition;
- }
- return $this->derivatives;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment