annotate core/modules/search/src/SearchPageListBuilder.php @ 0:4c8ae668cc8c

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