annotate core/modules/search/src/SearchPageListBuilder.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\search;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 6 use Drupal\Core\Config\Entity\DraggableListBuilder;
Chris@0 7 use Drupal\Core\Entity\EntityInterface;
Chris@0 8 use Drupal\Core\Entity\EntityStorageInterface;
Chris@0 9 use Drupal\Core\Entity\EntityTypeInterface;
Chris@0 10 use Drupal\Core\Form\ConfigFormBaseTrait;
Chris@0 11 use Drupal\Core\Form\FormInterface;
Chris@0 12 use Drupal\Core\Form\FormStateInterface;
Chris@17 13 use Drupal\Core\Messenger\MessengerInterface;
Chris@0 14 use Drupal\Core\Url;
Chris@0 15 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Defines a class to build a listing of search page entities.
Chris@0 19 *
Chris@0 20 * @see \Drupal\search\Entity\SearchPage
Chris@0 21 */
Chris@0 22 class SearchPageListBuilder extends DraggableListBuilder implements FormInterface {
Chris@0 23 use ConfigFormBaseTrait;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * The entities being listed.
Chris@0 27 *
Chris@0 28 * @var \Drupal\search\SearchPageInterface[]
Chris@0 29 */
Chris@0 30 protected $entities = [];
Chris@0 31
Chris@0 32 /**
Chris@0 33 * Stores the configuration factory.
Chris@0 34 *
Chris@0 35 * @var \Drupal\Core\Config\ConfigFactoryInterface
Chris@0 36 */
Chris@0 37 protected $configFactory;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * The search manager.
Chris@0 41 *
Chris@0 42 * @var \Drupal\search\SearchPluginManager
Chris@0 43 */
Chris@0 44 protected $searchManager;
Chris@0 45
Chris@0 46 /**
Chris@17 47 * The messenger.
Chris@17 48 *
Chris@17 49 * @var \Drupal\Core\Messenger\MessengerInterface
Chris@17 50 */
Chris@17 51 protected $messenger;
Chris@17 52
Chris@17 53 /**
Chris@0 54 * Constructs a new SearchPageListBuilder object.
Chris@0 55 *
Chris@0 56 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 57 * The entity type definition.
Chris@0 58 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
Chris@0 59 * The entity storage class.
Chris@0 60 * @param \Drupal\search\SearchPluginManager $search_manager
Chris@0 61 * The search plugin manager.
Chris@0 62 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 63 * The factory for configuration objects.
Chris@17 64 * @param \Drupal\Core\Messenger\MessengerInterface $messenger
Chris@17 65 * The messenger.
Chris@0 66 */
Chris@17 67 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, SearchPluginManager $search_manager, ConfigFactoryInterface $config_factory, MessengerInterface $messenger) {
Chris@0 68 parent::__construct($entity_type, $storage);
Chris@0 69 $this->configFactory = $config_factory;
Chris@0 70 $this->searchManager = $search_manager;
Chris@17 71 $this->messenger = $messenger;
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * {@inheritdoc}
Chris@0 76 */
Chris@0 77 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
Chris@0 78 return new static(
Chris@0 79 $entity_type,
Chris@18 80 $container->get('entity_type.manager')->getStorage($entity_type->id()),
Chris@0 81 $container->get('plugin.manager.search'),
Chris@17 82 $container->get('config.factory'),
Chris@17 83 $container->get('messenger')
Chris@0 84 );
Chris@0 85 }
Chris@0 86
Chris@0 87 /**
Chris@0 88 * {@inheritdoc}
Chris@0 89 */
Chris@0 90 public function getFormId() {
Chris@0 91 return 'search_admin_settings';
Chris@0 92 }
Chris@0 93
Chris@0 94 /**
Chris@0 95 * {@inheritdoc}
Chris@0 96 */
Chris@0 97 protected function getEditableConfigNames() {
Chris@0 98 return ['search.settings'];
Chris@0 99 }
Chris@0 100
Chris@0 101 /**
Chris@0 102 * {@inheritdoc}
Chris@0 103 */
Chris@0 104 public function buildHeader() {
Chris@0 105 $header['label'] = [
Chris@0 106 'data' => $this->t('Label'),
Chris@0 107 ];
Chris@0 108 $header['url'] = [
Chris@0 109 'data' => $this->t('URL'),
Chris@0 110 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 111 ];
Chris@0 112 $header['plugin'] = [
Chris@0 113 'data' => $this->t('Type'),
Chris@0 114 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 115 ];
Chris@0 116 $header['status'] = [
Chris@0 117 'data' => $this->t('Status'),
Chris@0 118 ];
Chris@0 119 $header['progress'] = [
Chris@0 120 'data' => $this->t('Indexing progress'),
Chris@0 121 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
Chris@0 122 ];
Chris@0 123 return $header + parent::buildHeader();
Chris@0 124 }
Chris@0 125
Chris@0 126 /**
Chris@0 127 * {@inheritdoc}
Chris@0 128 */
Chris@0 129 public function buildRow(EntityInterface $entity) {
Chris@0 130 /** @var $entity \Drupal\search\SearchPageInterface */
Chris@0 131 $row['label'] = $entity->label();
Chris@0 132 $row['url']['#markup'] = 'search/' . $entity->getPath();
Chris@0 133 // If the search page is active, link to it.
Chris@0 134 if ($entity->status()) {
Chris@0 135 $row['url'] = [
Chris@0 136 '#type' => 'link',
Chris@0 137 '#title' => $row['url'],
Chris@0 138 '#url' => Url::fromRoute('search.view_' . $entity->id()),
Chris@0 139 ];
Chris@0 140 }
Chris@0 141
Chris@0 142 $definition = $entity->getPlugin()->getPluginDefinition();
Chris@0 143 $row['plugin']['#markup'] = $definition['title'];
Chris@0 144
Chris@0 145 if ($entity->isDefaultSearch()) {
Chris@0 146 $status = $this->t('Default');
Chris@0 147 }
Chris@0 148 elseif ($entity->status()) {
Chris@0 149 $status = $this->t('Enabled');
Chris@0 150 }
Chris@0 151 else {
Chris@0 152 $status = $this->t('Disabled');
Chris@0 153 }
Chris@0 154 $row['status']['#markup'] = $status;
Chris@0 155
Chris@0 156 if ($entity->isIndexable()) {
Chris@0 157 $status = $entity->getPlugin()->indexStatus();
Chris@0 158 $row['progress']['#markup'] = $this->t('%num_indexed of %num_total indexed', [
Chris@0 159 '%num_indexed' => $status['total'] - $status['remaining'],
Chris@17 160 '%num_total' => $status['total'],
Chris@0 161 ]);
Chris@0 162 }
Chris@0 163 else {
Chris@0 164 $row['progress']['#markup'] = $this->t('Does not use index');
Chris@0 165 }
Chris@0 166
Chris@0 167 return $row + parent::buildRow($entity);
Chris@0 168 }
Chris@0 169
Chris@0 170 /**
Chris@0 171 * {@inheritdoc}
Chris@0 172 */
Chris@0 173 public function buildForm(array $form, FormStateInterface $form_state) {
Chris@0 174 $form = parent::buildForm($form, $form_state);
Chris@0 175 $search_settings = $this->config('search.settings');
Chris@0 176 // Collect some stats.
Chris@0 177 $remaining = 0;
Chris@0 178 $total = 0;
Chris@0 179 foreach ($this->entities as $entity) {
Chris@0 180 if ($entity->isIndexable() && $status = $entity->getPlugin()->indexStatus()) {
Chris@0 181 $remaining += $status['remaining'];
Chris@0 182 $total += $status['total'];
Chris@0 183 }
Chris@0 184 }
Chris@0 185
Chris@0 186 $this->moduleHandler->loadAllIncludes('admin.inc');
Chris@0 187 $count = $this->formatPlural($remaining, 'There is 1 item left to index.', 'There are @count items left to index.');
Chris@0 188 $done = $total - $remaining;
Chris@0 189 // Use floor() to calculate the percentage, so if it is not quite 100%, it
Chris@0 190 // will show as 99%, to indicate "almost done".
Chris@0 191 $percentage = $total > 0 ? floor(100 * $done / $total) : 100;
Chris@0 192 $percentage .= '%';
Chris@0 193 $status = '<p><strong>' . $this->t('%percentage of the site has been indexed.', ['%percentage' => $percentage]) . ' ' . $count . '</strong></p>';
Chris@0 194 $form['status'] = [
Chris@0 195 '#type' => 'details',
Chris@0 196 '#title' => $this->t('Indexing progress'),
Chris@0 197 '#open' => TRUE,
Chris@18 198 '#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' => Url::fromRoute('system.cron_settings')->toString()]),
Chris@0 199 ];
Chris@0 200 $form['status']['status'] = ['#markup' => $status];
Chris@0 201 $form['status']['wipe'] = [
Chris@0 202 '#type' => 'submit',
Chris@0 203 '#value' => $this->t('Re-index site'),
Chris@0 204 '#submit' => ['::searchAdminReindexSubmit'],
Chris@0 205 ];
Chris@0 206
Chris@0 207 $items = [10, 20, 50, 100, 200, 500];
Chris@0 208 $items = array_combine($items, $items);
Chris@0 209
Chris@0 210 // Indexing throttle:
Chris@0 211 $form['indexing_throttle'] = [
Chris@0 212 '#type' => 'details',
Chris@0 213 '#title' => $this->t('Indexing throttle'),
Chris@0 214 '#open' => TRUE,
Chris@0 215 ];
Chris@0 216 $form['indexing_throttle']['cron_limit'] = [
Chris@0 217 '#type' => 'select',
Chris@0 218 '#title' => $this->t('Number of items to index per cron run'),
Chris@0 219 '#default_value' => $search_settings->get('index.cron_limit'),
Chris@0 220 '#options' => $items,
Chris@18 221 '#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' => Url::fromRoute('system.cron_settings')->toString()]),
Chris@0 222 ];
Chris@0 223 // Indexing settings:
Chris@0 224 $form['indexing_settings'] = [
Chris@0 225 '#type' => 'details',
Chris@0 226 '#title' => $this->t('Default indexing settings'),
Chris@0 227 '#open' => TRUE,
Chris@0 228 ];
Chris@0 229 $form['indexing_settings']['info'] = [
Chris@17 230 '#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>"),
Chris@0 231 ];
Chris@0 232 $form['indexing_settings']['minimum_word_size'] = [
Chris@0 233 '#type' => 'number',
Chris@0 234 '#title' => $this->t('Minimum word length to index'),
Chris@0 235 '#default_value' => $search_settings->get('index.minimum_word_size'),
Chris@0 236 '#min' => 1,
Chris@0 237 '#max' => 1000,
Chris@0 238 '#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.'),
Chris@0 239 ];
Chris@0 240 $form['indexing_settings']['overlap_cjk'] = [
Chris@0 241 '#type' => 'checkbox',
Chris@0 242 '#title' => $this->t('Simple CJK handling'),
Chris@0 243 '#default_value' => $search_settings->get('index.overlap_cjk'),
Chris@17 244 '#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.'),
Chris@0 245 ];
Chris@0 246
Chris@0 247 // Indexing settings:
Chris@0 248 $form['logging'] = [
Chris@0 249 '#type' => 'details',
Chris@0 250 '#title' => $this->t('Logging'),
Chris@0 251 '#open' => TRUE,
Chris@0 252 ];
Chris@0 253
Chris@0 254 $form['logging']['logging'] = [
Chris@0 255 '#type' => 'checkbox',
Chris@0 256 '#title' => $this->t('Log searches'),
Chris@0 257 '#default_value' => $search_settings->get('logging'),
Chris@0 258 '#description' => $this->t('If checked, all searches will be logged. Uncheck to skip logging. Logging may affect performance.'),
Chris@0 259 ];
Chris@0 260
Chris@0 261 $form['search_pages'] = [
Chris@0 262 '#type' => 'details',
Chris@0 263 '#title' => $this->t('Search pages'),
Chris@0 264 '#open' => TRUE,
Chris@0 265 ];
Chris@0 266 $form['search_pages']['add_page'] = [
Chris@0 267 '#type' => 'container',
Chris@0 268 '#attributes' => [
Chris@0 269 'class' => ['container-inline'],
Chris@0 270 ],
Chris@0 271 ];
Chris@0 272 // In order to prevent validation errors for the parent form, this cannot be
Chris@0 273 // required, see self::validateAddSearchPage().
Chris@0 274 $form['search_pages']['add_page']['search_type'] = [
Chris@0 275 '#type' => 'select',
Chris@0 276 '#title' => $this->t('Search page type'),
Chris@0 277 '#empty_option' => $this->t('- Choose page type -'),
Chris@0 278 '#options' => array_map(function ($definition) {
Chris@0 279 return $definition['title'];
Chris@0 280 }, $this->searchManager->getDefinitions()),
Chris@0 281 ];
Chris@0 282 $form['search_pages']['add_page']['add_search_submit'] = [
Chris@0 283 '#type' => 'submit',
Chris@0 284 '#value' => $this->t('Add search page'),
Chris@0 285 '#validate' => ['::validateAddSearchPage'],
Chris@0 286 '#submit' => ['::submitAddSearchPage'],
Chris@0 287 '#limit_validation_errors' => [['search_type']],
Chris@0 288 ];
Chris@0 289
Chris@0 290 // Move the listing into the search_pages element.
Chris@0 291 $form['search_pages'][$this->entitiesKey] = $form[$this->entitiesKey];
Chris@0 292 $form['search_pages'][$this->entitiesKey]['#empty'] = $this->t('No search pages have been configured.');
Chris@0 293 unset($form[$this->entitiesKey]);
Chris@0 294
Chris@0 295 $form['actions']['#type'] = 'actions';
Chris@0 296 $form['actions']['submit'] = [
Chris@0 297 '#type' => 'submit',
Chris@0 298 '#value' => $this->t('Save configuration'),
Chris@0 299 '#button_type' => 'primary',
Chris@0 300 ];
Chris@0 301
Chris@0 302 return $form;
Chris@0 303 }
Chris@0 304
Chris@0 305 /**
Chris@0 306 * {@inheritdoc}
Chris@0 307 */
Chris@0 308 public function getDefaultOperations(EntityInterface $entity) {
Chris@0 309 /** @var $entity \Drupal\search\SearchPageInterface */
Chris@0 310 $operations = parent::getDefaultOperations($entity);
Chris@0 311
Chris@0 312 // Prevent the default search from being disabled or deleted.
Chris@0 313 if ($entity->isDefaultSearch()) {
Chris@0 314 unset($operations['disable'], $operations['delete']);
Chris@0 315 }
Chris@0 316 else {
Chris@0 317 $operations['default'] = [
Chris@0 318 'title' => $this->t('Set as default'),
Chris@0 319 'url' => Url::fromRoute('entity.search_page.set_default', [
Chris@0 320 'search_page' => $entity->id(),
Chris@0 321 ]),
Chris@0 322 'weight' => 50,
Chris@0 323 ];
Chris@0 324 }
Chris@0 325
Chris@0 326 return $operations;
Chris@0 327 }
Chris@0 328
Chris@0 329 /**
Chris@0 330 * {@inheritdoc}
Chris@0 331 */
Chris@0 332 public function validateForm(array &$form, FormStateInterface $form_state) {
Chris@0 333 }
Chris@0 334
Chris@0 335 /**
Chris@0 336 * {@inheritdoc}
Chris@0 337 */
Chris@0 338 public function submitForm(array &$form, FormStateInterface $form_state) {
Chris@0 339 parent::submitForm($form, $form_state);
Chris@0 340
Chris@0 341 $search_settings = $this->config('search.settings');
Chris@0 342 // If these settings change, the default index needs to be rebuilt.
Chris@0 343 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'))) {
Chris@0 344 $search_settings->set('index.minimum_word_size', $form_state->getValue('minimum_word_size'));
Chris@0 345 $search_settings->set('index.overlap_cjk', $form_state->getValue('overlap_cjk'));
Chris@0 346 // Specifically mark items in the default index for reindexing, since
Chris@0 347 // these settings are used in the search_index() function.
Chris@17 348 $this->messenger->addStatus($this->t('The default search index will be rebuilt.'));
Chris@0 349 search_mark_for_reindex();
Chris@0 350 }
Chris@0 351
Chris@0 352 $search_settings
Chris@0 353 ->set('index.cron_limit', $form_state->getValue('cron_limit'))
Chris@0 354 ->set('logging', $form_state->getValue('logging'))
Chris@0 355 ->save();
Chris@0 356
Chris@17 357 $this->messenger->addStatus($this->t('The configuration options have been saved.'));
Chris@0 358 }
Chris@0 359
Chris@0 360 /**
Chris@0 361 * Form submission handler for the reindex button on the search admin settings
Chris@0 362 * form.
Chris@0 363 */
Chris@0 364 public function searchAdminReindexSubmit(array &$form, FormStateInterface $form_state) {
Chris@0 365 // Send the user to the confirmation page.
Chris@0 366 $form_state->setRedirect('search.reindex_confirm');
Chris@0 367 }
Chris@0 368
Chris@0 369 /**
Chris@0 370 * Form validation handler for adding a new search page.
Chris@0 371 */
Chris@0 372 public function validateAddSearchPage(array &$form, FormStateInterface $form_state) {
Chris@0 373 if ($form_state->isValueEmpty('search_type')) {
Chris@0 374 $form_state->setErrorByName('search_type', $this->t('You must select the new search page type.'));
Chris@0 375 }
Chris@0 376 }
Chris@0 377
Chris@0 378 /**
Chris@0 379 * Form submission handler for adding a new search page.
Chris@0 380 */
Chris@0 381 public function submitAddSearchPage(array &$form, FormStateInterface $form_state) {
Chris@0 382 $form_state->setRedirect(
Chris@0 383 'search.add_type',
Chris@0 384 ['search_plugin_id' => $form_state->getValue('search_type')]
Chris@0 385 );
Chris@0 386 }
Chris@0 387
Chris@0 388 }