-
Notifications
You must be signed in to change notification settings - Fork 3
/
exclude_bundles.module
56 lines (47 loc) · 1.68 KB
/
exclude_bundles.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
use Drupal\Core\Database\Query\AlterableInterface;
/**
* Implements hook_query_TAG_alter
*/
function exclude_bundles_query_search_node_search_alter(AlterableInterface $query) {
// ideas taken from:
// https://www.phase2technology.com/blog/restricting-search-results-in-drupal-7/
// see the config file exclude_bundles.settings.yml
$config = \Drupal::config('exclude_bundles.settings');
$bundles_config = $config->get('bundles');
$hidden_bundles = [];
if (!empty($bundles_config)) {
foreach ($bundles_config as $k => $v) {
// if the value is not 0, add it to the array
if ($v) {
$hidden_bundles[] = $k;
}
}
}
if (!empty($hidden_bundles)) {
// add the extra table to join based on config logic
$query->join('node', 'n2', 'n.nid = n2.nid');
$query->condition('n2.type', $hidden_bundles, 'NOT IN');
}
}
function exclude_bundles_form_search_form_alter(&$form, &$form_state, $form_id) {
$config = \Drupal::config('exclude_bundles.settings');
$bundles_config = $config->get('bundles');
$hidden_bundles = [];
if (!empty($bundles_config)) {
foreach ($bundles_config as $k => $v) {
// if the value is not 0, add it to the array
if ($v) {
$hidden_bundles[$k] = $k;
}
}
}
// get the original array of all types from the form
$array_of_all_bundles = $form['advanced']['types-fieldset']['type']['#options'];
// remove the hidden bundles from the list of all content types
$array_of_non_excluded_bundles = array_diff_key($array_of_all_bundles, $hidden_bundles);
$form['advanced']['types-fieldset']['type'] = array(
'#type' => 'checkboxes',
'#options' => $array_of_non_excluded_bundles
);
}