0

I'm trying to figure out how to apply multiple content type filters to the search results of the Search Block Form (not the normal search form). I've successfully filtered by current language and ONE content type. So far, here's what I have:

function mytheme_form_search_block_form_alter(&$form, &$form_state) {
  $manager = \Drupal::languageManager();

  $form['advanced-form'] = [
    '#type' => 'hidden',
    '#value' => 1,
  ];
  $form['f[0]'] = [
    '#type' => 'hidden',
    '#value' => 'language:' . $manager->getCurrentLanguage()->getId(),
  ];
  $form['f[1]'] = [
    '#type' => 'hidden',
    '#value' => 'type:Insight',
  ];
}

There doesn't appear to be any documentation about adding filters this way. But I did find this: NodeSearch::buildSearchUrlQuery. It appears that one should somehow be able to use OR and Negation in this, but I just can't figure it out.

I've tried this:

$form['f[1]'] = [
    '#type' => 'hidden',
    '#value' => 'type:Insight OR type:Event',
];

This just filters by the last one listed. I also tried this:

$form['f[1]'] = [
    '#type' => 'hidden',
    '#value' => 'type:Insight OR Event',
];

And this:

$form['f[1]'] = [
    '#type' => 'hidden',
    '#value' => 'type:["Insight","Event"]',
];

And none of these were working to filter in (or out) multiple content types.

Any help would be greatly appreciated. Or just pointing me to some documentation on using filters this way. Thanks.

1 Answer 1

0

So, I figured out how to make it work without actually finding any documentation. I just added a new filter for each content type I want to search:

$form['advanced-form'] = [
    '#type' => 'hidden',
    '#value' => 1,
];
// add filter for current language
$form['f[0]'] = [
    '#type' => 'hidden',
    '#value' => 'language:' . \Drupal::languageManager()->getCurrentLanguage()->getId(),
];
// add filter for each full-page content type
foreach (fullPageNodeList() as $key => $full_page_type) {
    $form['f['.($key + 1).']'] = [
        '#type' => 'hidden',
        '#value' => 'type:'.$full_page_type
    ];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.