简体   繁体   English

Drupal 8:基于父项的分类页面限制公开的过滤器选项

[英]Drupal 8: Taxonomy page limit exposed filter options based on parent term

In a Drupal 8 site, I have a taxonomy page with a view block on it. 在Drupal 8站点中,我有一个分类页面,上面有一个视图块。 The view lists articles tagged with the term of the page you are on. 该视图列出了标有您所在页面术语的文章。 It also shows articles tagged with the child terms of the current taxonomy page. 它还显示了用当前分类法页面的子条款标记的文章。

I am using the exposed filter "Content: Has taxonomy terms (with depth) (exposed)" to let a user filter the articles based on the child terms. 我正在使用暴露的过滤器“内容:具有分类术语(具有深度)(暴露)”,以便用户根据子术语过滤文章。 Currently that filter shows all of the terms regardless of which taxonomy you are currently on. 当前,该过滤器将显示所有术语,而与您当前所处的分类法无关。

Here is an example of the items listed in the exposed filter: 这是暴露的过滤器中列出的项目的示例:

Mammals
 - Cat
 - Dog
Reptiles
 - Lizard
 - Snake
Amphibians
 - Frog
 - Salamander

The URL for one of the parent terms would be: 父条款之一的URL为:

site.com/animal/mammals site.com/动物/哺乳动物

I need to limit the list of options within the exposed filter to only show the children of the term based on the URL. 我需要限制公开过滤器中的选项列表,以仅显示基于URL的术语的子级。 So on the URL above, only Cat and Dog would be listed in the exposed filter. 因此,在上面的URL上,公开的过滤器中只会列出猫和狗。

In Drupal 7 I could accomplish this with a hook_form_alter in my theme.module using the URL arg(2) to get the term name. 在Drupal 7中,我可以使用URL arg(2)在我的theme.module中使用hook_form_alter来获得术语名称。 I cannot find any documentation on how to do this in Drupal 8. 我在Drupal 8中找不到有关如何执行此操作的任何文档。

Here is what I have found so far: 到目前为止,这是我发现的内容:

function myTheme_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  if ($form_id == 'views_exposed_form' && $form['#id'] == 'views-exposed-form-article-tags-block-1') {

    $term = arg(2);
    //Need D8 code to load term, find it's children and alter the select box to show those children

  }
}

I am open to other options if this is not the way to accomplish my goal. 如果这不是实现我的目标的方式,我会接受其他选择。 Thank you in advance. 先感谢您。

hook_form_alter will still work, though arg() is removed in Drupal 8. 尽管在Drupal 8中删除了arg(),hook_form_alter仍然可以工作。

It is not clear to me what the general replacement for arg() is. 我不清楚arg()的一般替代品是什么。 The code below uses two techniques to get the taxonomy term id. 下面的代码使用两种技术来获取分类术语ID。

You probably want to turn caching off in the view during development. 您可能希望在开发期间关闭视图中的缓存。

function myTheme_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  if ($form_id == 'views_exposed_form' && $form['#id'] == 'views-exposed-form-article-tags-block-1') {

    // initial page load
    $parameters = \Drupal::routeMatch()->getRawParameters();
    $this_term_id = $parameters->get('taxonomy_term');

    // ajax refresh via apply button
    if (!isset($this_term_id)) {
      $this_term_id = $_REQUEST['view_args'];
    }

    // get children of $this_term_id
    $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('tags', $parent = $this_term_id, $max_depth = NULL, $load_entities = FALSE);

    // get rid of all options except default
    $all_option = $form['term_node_tid_depth']['#options']['All'];
    unset($form['term_node_tid_depth']['#options']);
    $form['term_node_tid_depth']['#options']['All'] = $all_option;

    // add child terms
    foreach ($tree as $term) {
      $option = new stdClass();
      $option->option[$term->tid]=$term->name;
      $form['term_node_tid_depth']['#options'][] = $option;
    }
  } 

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM