/
SearchBundles.php
72 lines (58 loc) · 1.57 KB
/
SearchBundles.php
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* @file
* Contains \Drupal\exclude_bundles\Form\SearchBundles.
*/
namespace Drupal\exclude_bundles\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class SearchBundles.
*
* @package Drupal\exclude_bundles\Form
*/
class SearchBundles extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'search_bundles';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'exclude_bundles.settings',
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('exclude_bundles.settings');
$configured_bundles = $config->get('bundles') ? $config->get('bundles') : [];
// get list of current Content Types
$node_bundles = \Drupal::service('entity_type.manager')->getStorage('node_type')->loadMultiple();
$options = [];
foreach ($node_bundles as $bundle) {
$options[$bundle->id()] = $bundle->label();
}
$form['bundles'] = array(
'#type' => 'checkboxes',
'#options' => $options,
'#title' => t('Content types to exclude from search:'),
'#default_value' => $configured_bundles,
);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('exclude_bundles.settings')
->set('bundles', $form_state->getValue('bundles'))
->save();
parent::submitForm($form, $form_state);
}
}