简体   繁体   English

当术语权重值 = 0 时,如何动态更改视图公开过滤器引用分类术语的默认值

[英]How can I dynamically change the default value for views exposed filter referencing taxonomy terms when the term weight value = 0

I have an exposed filter in a view is referencing taxonomy terms from specific vocabulary.我在一个视图中有一个暴露的过滤器正在引用特定词汇表中的分类术语。 The operator ( screenshot below ) allows selecting a term as the default value for that filter.运算符(下面的屏幕截图)允许选择一个术语作为该过滤器的默认值。 What I want to achieve is to dynamically select the default value when the term weight = 0.我想要实现的是动态 select 项权重 = 0 时的默认值。

This should allow less privileged roles like Content Contributors to set the default value for the exposed filter by changing the order (weight) of the term without the need to edit the views settings.这应该允许权限较低的角色(如内容贡献者)通过更改术语的顺序(权重)来设置公开过滤器的默认值,而无需编辑视图设置。

I tried to research this and so far, it seems the best way to achieve this is by using (hook_views_pre_build) but I just don't know how.我试图对此进行研究,到目前为止,实现这一目标的最佳方法似乎是使用 (hook_views_pre_build) 但我只是不知道如何。

After extensive search and experiment, this code worked for me:经过广泛的搜索和实验,这段代码对我有用:

use Drupal\taxonomy\Entity\Term;
use Drupal\views\ViewExecutable;

/**
 * Implements hook_views_pre_build().
 */

function YOUR_MODULE_NAME_views_pre_build(ViewExecutable $view) {
  if ($view->id() == 'YOUR_VIEW_ID' && $view->current_display == 'YOUR_DISPLAY_ID') {
    // Get the exposed filter for the taxonomy terms.
    $filter = $view->display_handler->getHandler('filter', 'YOUR_FILTER_ID');
    if ($filter) {
      // Query the database for taxonomy terms with a weight of 0.
      $query = \Drupal::entityQuery('taxonomy_term');
      $query->condition('weight', 0);
      $term_ids = $query->execute();
      if (!empty($term_ids)) {
        // Load the first term found with a weight of 0.
        $term = Term::load(array_shift($term_ids));
        if ($term) {
          // Set the default value for the exposed filter to the term's ID.
          $filter->options['value'] = $term->id();
        }
      }
    }
  }
}

This code will query the database for taxonomy terms with a weight of 0 when the specified view is pre-built and set the default value for the exposed filter to the ID of the first term it finds.当预先构建指定视图时,此代码将在数据库中查询权重为 0 的分类术语,并将公开过滤器的默认值设置为其找到的第一个术语的 ID。 If no terms are found with a weight of 0, it will not change the default value.如果没有找到权重为 0 的项,则不会更改默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Drupal Views中更改公开过滤器的默认值(-Any-)的标签? - How to change the label of the default value (-Any-) of an exposed filter in Drupal Views? D7视图公开的形式:以编程方式更改分类术语选择 - D7 views exposed form: programmatically change taxonomy terms selection 如何为整个Drupal 6网站设置用户选择的暴露位置“视图”过滤器的值? - How can I set the user selected value of an exposed location Views filter for the entire Drupal 6 website? Drupal视图具有带有多个术语的分类术语 - Drupal Views Has Taxonomy Term with multiple terms Drupal 7视图分类法简单分层暴露过滤器 - Drupal 7 views taxonomy Simple Hierarchical exposed filter Drupal视图-在不同的词汇页面上时,分类术语权重的排序顺序? - Drupal Views - Sort order of taxonomy term weight, when on a different vocabulary page? Drupal Views暴露的过滤器:分类选择列表? - Drupal VIews Exposed Filter: Taxonomy Select List? Drupal 7视图按节点的分类术语过滤 - Drupal 7 views filter by node's taxonomy term Drupal 7视图按动态分类术语过滤 - Drupal 7 views filter by dynamic taxonomy term Drupal 8 视图公开过滤器 > 带有复选框和自动视图更新的分类术语 - Drupal 8 Views exposed filters > Taxonomy terms with checkboxes and automatic view update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM