最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Drupal 11: Module to provide a contextual filter for views - Stack Overflow

programmeradmin3浏览0评论

I have programmed a Drupal 11 module. It's about a Views plugin to be able to use a contextual filter in the GUI. The module is intended to provide two context filters in one View (Views module) under Drupal 11. I want to use the NodeID and taxonomy term of a displayed node as a context filter in a View (Views module) named "zviewspez". I have activated the module. In the zviewspez view, I now want to select my programmed filter in the "Advanced" area under "Contextual filters". But it is not available to choose. The module is located on the server in the directory "/web/modules/custom/wissensbasis_views".

I have a content type. Machine name: "wissensbasis". The content type includes a field with a machine name: "field_wb_themenbereich_ref". The field_wb_themenbereich_ref field type is an Entity reference, Reference type: Taxonomy term. The field_wb_themenbereich_ref field can contain only one value, one term.

I tested with Drush if the plugin is known:

drush php:eval "print_r(Drupal::service('plugin.manager.views.argument')->getDefinitions());"

Result:
    [wissensbasis_views_argument] => Array
        (
            [plugin_type] => argument
            [id] => wissensbasis_views_argument
            [class] => Drupal\wissensbasis_views\Plugin\views\argument\WissensbasisViewsArgument
            [provider] => wissensbasis_views
        )

The functions and the class seem to be loaded. This is my PHP code.

Path for the Views plugin: /web/modules/custom/wissensbasis_views/src/Plugin/views/argument File Name: WissensbasisViewsArgument.php

namespace Drupal\wissensbasis_views\Plugin\views\argument;

use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Drupal\views\Plugin\views\argument\ArgumentInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;

/**
 * Provides a Wissensbasis Views Argument.
 *
 * @ViewsArgument("wissensbasis_views_argument")
 */
class WissensbasisViewsArgument extends ArgumentPluginBase implements ContainerFactoryPluginInterface {

  /**
   * {@inheritdoc}
   */
  public function summaryTitle() {
    return $this->t('Wissensbasis-Themenbereich');
  }

  /**
   * {@inheritdoc}
   */
  public function title() {
    return $this->t('Wissensbasis-Themenbereich');
  }

  /**
   * Retrieves the contextual argument.
   *
   * This method retrieves the current node from the route and extracts the
   * target_id from the field "field_wb_themenbereich_ref".
   *
   * @return mixed
   *   The taxonomy term target_id or NULL if no node is present.
   */
  public function getArgument() {
    \Drupal::logger('wissensbasis_views')->notice('Methode getArgument() wurde aufgerufen.');
    $node = \Drupal::routeMatch()->getParameter('node');
    if ($node && $node->hasField('field_wb_themenbereich_ref')) {
            $themenbereich = $node->get('field_wb_themenbereich_ref')->target_id;
            \Drupal::logger('wissensbasis_views')->notice('Themenbereich: @value', ['@value' => $themenbereich]);
            return $themenbereich;
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition);
  }

}

This the code in wissensbasis_views.module

/**
 * Implements hook_views_data_alter().
 */
function wissensbasis_views_views_data_alter(array &$data) {
  \Drupal::logger('wissensbasis_views')->notice('hook_views_data_alter wurde ausgeführt.');
  if (isset($data['node_field_data'])) {
    $data['node_field_data']['arguments']['wissensbasis_views_argument'] = [
      'title' => t('Wissensbasis-Themenbereich'),
      'help' => t('Filtert Inhalte anhand des zugeordneten Wissensbasis-Themenbereichs.'),
     // Verweist auf den Plugin-Handler anhand der definierten Plugin-ID.
      'id' => 'wissensbasis_views_argument',
    ];
    // Debug-Ausgabe: z. B. mittels Devel:
    if (function_exists('dpm')) {
      dpm($data['node_field_data']['arguments'], 'node_field_data arguments');
    }
  }
}

What is wrong? Can you help pleas?

发布评论

评论列表(0)

  1. 暂无评论