3 namespace Drupal\search;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Config\Entity\DraggableListBuilder;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Form\ConfigFormBaseTrait;
11 use Drupal\Core\Form\FormInterface;
12 use Drupal\Core\Form\FormStateInterface;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * Defines a class to build a listing of search page entities.
19 * @see \Drupal\search\Entity\SearchPage
21 class SearchPageListBuilder extends DraggableListBuilder implements FormInterface {
22 use ConfigFormBaseTrait;
25 * The entities being listed.
27 * @var \Drupal\search\SearchPageInterface[]
29 protected $entities = [];
32 * Stores the configuration factory.
34 * @var \Drupal\Core\Config\ConfigFactoryInterface
36 protected $configFactory;
41 * @var \Drupal\search\SearchPluginManager
43 protected $searchManager;
46 * Constructs a new SearchPageListBuilder object.
48 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
49 * The entity type definition.
50 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
51 * The entity storage class.
52 * @param \Drupal\search\SearchPluginManager $search_manager
53 * The search plugin manager.
54 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
55 * The factory for configuration objects.
57 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, SearchPluginManager $search_manager, ConfigFactoryInterface $config_factory) {
58 parent::__construct($entity_type, $storage);
59 $this->configFactory = $config_factory;
60 $this->searchManager = $search_manager;
66 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
69 $container->get('entity.manager')->getStorage($entity_type->id()),
70 $container->get('plugin.manager.search'),
71 $container->get('config.factory')
78 public function getFormId() {
79 return 'search_admin_settings';
85 protected function getEditableConfigNames() {
86 return ['search.settings'];
92 public function buildHeader() {
94 'data' => $this->t('Label'),
97 'data' => $this->t('URL'),
98 'class' => [RESPONSIVE_PRIORITY_LOW],
100 $header['plugin'] = [
101 'data' => $this->t('Type'),
102 'class' => [RESPONSIVE_PRIORITY_LOW],
104 $header['status'] = [
105 'data' => $this->t('Status'),
107 $header['progress'] = [
108 'data' => $this->t('Indexing progress'),
109 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
111 return $header + parent::buildHeader();
117 public function buildRow(EntityInterface $entity) {
118 /** @var $entity \Drupal\search\SearchPageInterface */
119 $row['label'] = $entity->label();
120 $row['url']['#markup'] = 'search/' . $entity->getPath();
121 // If the search page is active, link to it.
122 if ($entity->status()) {
125 '#title' => $row['url'],
126 '#url' => Url::fromRoute('search.view_' . $entity->id()),
130 $definition = $entity->getPlugin()->getPluginDefinition();
131 $row['plugin']['#markup'] = $definition['title'];
133 if ($entity->isDefaultSearch()) {
134 $status = $this->t('Default');
136 elseif ($entity->status()) {
137 $status = $this->t('Enabled');
140 $status = $this->t('Disabled');
142 $row['status']['#markup'] = $status;
144 if ($entity->isIndexable()) {
145 $status = $entity->getPlugin()->indexStatus();
146 $row['progress']['#markup'] = $this->t('%num_indexed of %num_total indexed', [
147 '%num_indexed' => $status['total'] - $status['remaining'],
148 '%num_total' => $status['total']
152 $row['progress']['#markup'] = $this->t('Does not use index');
155 return $row + parent::buildRow($entity);
161 public function buildForm(array $form, FormStateInterface $form_state) {
162 $form = parent::buildForm($form, $form_state);
163 $search_settings = $this->config('search.settings');
164 // Collect some stats.
167 foreach ($this->entities as $entity) {
168 if ($entity->isIndexable() && $status = $entity->getPlugin()->indexStatus()) {
169 $remaining += $status['remaining'];
170 $total += $status['total'];
174 $this->moduleHandler->loadAllIncludes('admin.inc');
175 $count = $this->formatPlural($remaining, 'There is 1 item left to index.', 'There are @count items left to index.');
176 $done = $total - $remaining;
177 // Use floor() to calculate the percentage, so if it is not quite 100%, it
178 // will show as 99%, to indicate "almost done".
179 $percentage = $total > 0 ? floor(100 * $done / $total) : 100;
181 $status = '<p><strong>' . $this->t('%percentage of the site has been indexed.', ['%percentage' => $percentage]) . ' ' . $count . '</strong></p>';
183 '#type' => 'details',
184 '#title' => $this->t('Indexing progress'),
186 '#description' => $this->t('Only items in the index will appear in search results. To build and maintain the index, a correctly configured <a href=":cron">cron maintenance task</a> is required.', [':cron' => \Drupal::url('system.cron_settings')]),
188 $form['status']['status'] = ['#markup' => $status];
189 $form['status']['wipe'] = [
191 '#value' => $this->t('Re-index site'),
192 '#submit' => ['::searchAdminReindexSubmit'],
195 $items = [10, 20, 50, 100, 200, 500];
196 $items = array_combine($items, $items);
198 // Indexing throttle:
199 $form['indexing_throttle'] = [
200 '#type' => 'details',
201 '#title' => $this->t('Indexing throttle'),
204 $form['indexing_throttle']['cron_limit'] = [
206 '#title' => $this->t('Number of items to index per cron run'),
207 '#default_value' => $search_settings->get('index.cron_limit'),
208 '#options' => $items,
209 '#description' => $this->t('The maximum number of items indexed in each run of the <a href=":cron">cron maintenance task</a>. If necessary, reduce the number of items to prevent timeouts and memory errors while indexing. Some search page types may have their own setting for this.', [':cron' => \Drupal::url('system.cron_settings')]),
211 // Indexing settings:
212 $form['indexing_settings'] = [
213 '#type' => 'details',
214 '#title' => $this->t('Default indexing settings'),
217 $form['indexing_settings']['info'] = [
218 '#markup' => $this->t("<p>Search pages that use an index may use the default index provided by the Search module, or they may use a different indexing mechanism. These settings are for the default index. <em>Changing these settings will cause the default search index to be rebuilt to reflect the new settings. Searching will continue to work, based on the existing index, but new content won't be indexed until all existing content has been re-indexed.</em></p><p><em>The default settings should be appropriate for the majority of sites.</em></p>")
220 $form['indexing_settings']['minimum_word_size'] = [
222 '#title' => $this->t('Minimum word length to index'),
223 '#default_value' => $search_settings->get('index.minimum_word_size'),
226 '#description' => $this->t('The minimum character length for a word to be added to the index. Searches must include a keyword of at least this length.'),
228 $form['indexing_settings']['overlap_cjk'] = [
229 '#type' => 'checkbox',
230 '#title' => $this->t('Simple CJK handling'),
231 '#default_value' => $search_settings->get('index.overlap_cjk'),
232 '#description' => $this->t('Whether to apply a simple Chinese/Japanese/Korean tokenizer based on overlapping sequences. Turn this off if you want to use an external preprocessor for this instead. Does not affect other languages.')
235 // Indexing settings:
237 '#type' => 'details',
238 '#title' => $this->t('Logging'),
242 $form['logging']['logging'] = [
243 '#type' => 'checkbox',
244 '#title' => $this->t('Log searches'),
245 '#default_value' => $search_settings->get('logging'),
246 '#description' => $this->t('If checked, all searches will be logged. Uncheck to skip logging. Logging may affect performance.'),
249 $form['search_pages'] = [
250 '#type' => 'details',
251 '#title' => $this->t('Search pages'),
254 $form['search_pages']['add_page'] = [
255 '#type' => 'container',
257 'class' => ['container-inline'],
260 // In order to prevent validation errors for the parent form, this cannot be
261 // required, see self::validateAddSearchPage().
262 $form['search_pages']['add_page']['search_type'] = [
264 '#title' => $this->t('Search page type'),
265 '#empty_option' => $this->t('- Choose page type -'),
266 '#options' => array_map(function ($definition) {
267 return $definition['title'];
268 }, $this->searchManager->getDefinitions()),
270 $form['search_pages']['add_page']['add_search_submit'] = [
272 '#value' => $this->t('Add search page'),
273 '#validate' => ['::validateAddSearchPage'],
274 '#submit' => ['::submitAddSearchPage'],
275 '#limit_validation_errors' => [['search_type']],
278 // Move the listing into the search_pages element.
279 $form['search_pages'][$this->entitiesKey] = $form[$this->entitiesKey];
280 $form['search_pages'][$this->entitiesKey]['#empty'] = $this->t('No search pages have been configured.');
281 unset($form[$this->entitiesKey]);
283 $form['actions']['#type'] = 'actions';
284 $form['actions']['submit'] = [
286 '#value' => $this->t('Save configuration'),
287 '#button_type' => 'primary',
296 public function getDefaultOperations(EntityInterface $entity) {
297 /** @var $entity \Drupal\search\SearchPageInterface */
298 $operations = parent::getDefaultOperations($entity);
300 // Prevent the default search from being disabled or deleted.
301 if ($entity->isDefaultSearch()) {
302 unset($operations['disable'], $operations['delete']);
305 $operations['default'] = [
306 'title' => $this->t('Set as default'),
307 'url' => Url::fromRoute('entity.search_page.set_default', [
308 'search_page' => $entity->id(),
320 public function validateForm(array &$form, FormStateInterface $form_state) {
326 public function submitForm(array &$form, FormStateInterface $form_state) {
327 parent::submitForm($form, $form_state);
329 $search_settings = $this->config('search.settings');
330 // If these settings change, the default index needs to be rebuilt.
331 if (($search_settings->get('index.minimum_word_size') != $form_state->getValue('minimum_word_size')) || ($search_settings->get('index.overlap_cjk') != $form_state->getValue('overlap_cjk'))) {
332 $search_settings->set('index.minimum_word_size', $form_state->getValue('minimum_word_size'));
333 $search_settings->set('index.overlap_cjk', $form_state->getValue('overlap_cjk'));
334 // Specifically mark items in the default index for reindexing, since
335 // these settings are used in the search_index() function.
336 drupal_set_message($this->t('The default search index will be rebuilt.'));
337 search_mark_for_reindex();
341 ->set('index.cron_limit', $form_state->getValue('cron_limit'))
342 ->set('logging', $form_state->getValue('logging'))
345 drupal_set_message($this->t('The configuration options have been saved.'));
349 * Form submission handler for the reindex button on the search admin settings
352 public function searchAdminReindexSubmit(array &$form, FormStateInterface $form_state) {
353 // Send the user to the confirmation page.
354 $form_state->setRedirect('search.reindex_confirm');
358 * Form validation handler for adding a new search page.
360 public function validateAddSearchPage(array &$form, FormStateInterface $form_state) {
361 if ($form_state->isValueEmpty('search_type')) {
362 $form_state->setErrorByName('search_type', $this->t('You must select the new search page type.'));
367 * Form submission handler for adding a new search page.
369 public function submitAddSearchPage(array &$form, FormStateInterface $form_state) {
370 $form_state->setRedirect(
372 ['search_plugin_id' => $form_state->getValue('search_type')]