You are here

class SearchPageListBuilder in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/search/src/SearchPageListBuilder.php \Drupal\search\SearchPageListBuilder

Defines a class to build a listing of search page entities.

Hierarchy

Expanded class hierarchy of SearchPageListBuilder

See also

\Drupal\search\Entity\SearchPage

File

core/modules/search/src/SearchPageListBuilder.php, line 22

Namespace

Drupal\search
View source
class SearchPageListBuilder extends DraggableListBuilder implements FormInterface {
  use ConfigFormBaseTrait;

  /**
   * The entities being listed.
   *
   * @var \Drupal\search\SearchPageInterface[]
   */
  protected $entities = [];

  /**
   * Stores the configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The search manager.
   *
   * @var \Drupal\search\SearchPluginManager
   */
  protected $searchManager;

  /**
   * The search index.
   *
   * @var \Drupal\search\SearchIndexInterface
   */
  protected $searchIndex;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Constructs a new SearchPageListBuilder object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
   * @param \Drupal\search\SearchPluginManager $search_manager
   *   The search plugin manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   * @param \Drupal\search\SearchIndexInterface $search_index
   *   The search index.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, SearchPluginManager $search_manager, ConfigFactoryInterface $config_factory, MessengerInterface $messenger, SearchIndexInterface $search_index = NULL) {
    parent::__construct($entity_type, $storage);
    $this->configFactory = $config_factory;
    $this->searchManager = $search_manager;
    $this->messenger = $messenger;
    if (!$search_index) {
      @trigger_error('Calling SearchPageListBuilder::__construct() without the $search_index argument is deprecated in drupal:8.8.0 and is required in drupal:9.0.0. See https://www.drupal.org/node/3075696', E_USER_DEPRECATED);
      $search_index = \Drupal::service('search.index');
    }
    $this->searchIndex = $search_index;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('entity_type.manager')
      ->getStorage($entity_type
      ->id()), $container
      ->get('plugin.manager.search'), $container
      ->get('config.factory'), $container
      ->get('messenger'), $container
      ->get('search.index'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'search_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'search.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['label'] = [
      'data' => $this
        ->t('Label'),
    ];
    $header['url'] = [
      'data' => $this
        ->t('URL'),
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ];
    $header['plugin'] = [
      'data' => $this
        ->t('Type'),
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ];
    $header['status'] = [
      'data' => $this
        ->t('Status'),
    ];
    $header['progress'] = [
      'data' => $this
        ->t('Indexing progress'),
      'class' => [
        RESPONSIVE_PRIORITY_MEDIUM,
      ],
    ];
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {

    /** @var $entity \Drupal\search\SearchPageInterface */
    $row['label'] = $entity
      ->label();
    $row['url']['#markup'] = 'search/' . $entity
      ->getPath();

    // If the search page is active, link to it.
    if ($entity
      ->status()) {
      $row['url'] = [
        '#type' => 'link',
        '#title' => $row['url'],
        '#url' => Url::fromRoute('search.view_' . $entity
          ->id()),
      ];
    }
    $definition = $entity
      ->getPlugin()
      ->getPluginDefinition();
    $row['plugin']['#markup'] = $definition['title'];
    if ($entity
      ->isDefaultSearch()) {
      $status = $this
        ->t('Default');
    }
    elseif ($entity
      ->status()) {
      $status = $this
        ->t('Enabled');
    }
    else {
      $status = $this
        ->t('Disabled');
    }
    $row['status']['#markup'] = $status;
    if ($entity
      ->isIndexable()) {
      $status = $entity
        ->getPlugin()
        ->indexStatus();
      $row['progress']['#markup'] = $this
        ->t('%num_indexed of %num_total indexed', [
        '%num_indexed' => $status['total'] - $status['remaining'],
        '%num_total' => $status['total'],
      ]);
    }
    else {
      $row['progress']['#markup'] = $this
        ->t('Does not use index');
    }
    return $row + parent::buildRow($entity);
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $search_settings = $this
      ->config('search.settings');

    // Collect some stats.
    $remaining = 0;
    $total = 0;
    foreach ($this->entities as $entity) {
      if ($entity
        ->isIndexable() && ($status = $entity
        ->getPlugin()
        ->indexStatus())) {
        $remaining += $status['remaining'];
        $total += $status['total'];
      }
    }
    $this->moduleHandler
      ->loadAllIncludes('admin.inc');
    $count = $this
      ->formatPlural($remaining, 'There is 1 item left to index.', 'There are @count items left to index.');
    $done = $total - $remaining;

    // Use floor() to calculate the percentage, so if it is not quite 100%, it
    // will show as 99%, to indicate "almost done".
    $percentage = $total > 0 ? floor(100 * $done / $total) : 100;
    $percentage .= '%';
    $status = '<p><strong>' . $this
      ->t('%percentage of the site has been indexed.', [
      '%percentage' => $percentage,
    ]) . ' ' . $count . '</strong></p>';
    $form['status'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Indexing progress'),
      '#open' => TRUE,
      '#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(),
      ]),
    ];
    $form['status']['status'] = [
      '#markup' => $status,
    ];
    $form['status']['wipe'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Re-index site'),
      '#submit' => [
        '::searchAdminReindexSubmit',
      ],
    ];
    $items = [
      10,
      20,
      50,
      100,
      200,
      500,
    ];
    $items = array_combine($items, $items);

    // Indexing throttle:
    $form['indexing_throttle'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Indexing throttle'),
      '#open' => TRUE,
    ];
    $form['indexing_throttle']['cron_limit'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Number of items to index per cron run'),
      '#default_value' => $search_settings
        ->get('index.cron_limit'),
      '#options' => $items,
      '#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(),
      ]),
    ];

    // Indexing settings:
    $form['indexing_settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Default indexing settings'),
      '#open' => TRUE,
    ];
    $form['indexing_settings']['info'] = [
      '#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>"),
    ];
    $form['indexing_settings']['minimum_word_size'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Minimum word length to index'),
      '#default_value' => $search_settings
        ->get('index.minimum_word_size'),
      '#min' => 1,
      '#max' => 1000,
      '#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.'),
    ];
    $form['indexing_settings']['overlap_cjk'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Simple CJK handling'),
      '#default_value' => $search_settings
        ->get('index.overlap_cjk'),
      '#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.'),
    ];

    // Indexing settings:
    $form['logging'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Logging'),
      '#open' => TRUE,
    ];
    $form['logging']['logging'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Log searches'),
      '#default_value' => $search_settings
        ->get('logging'),
      '#description' => $this
        ->t('If checked, all searches will be logged. Uncheck to skip logging. Logging may affect performance.'),
    ];
    $form['search_pages'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Search pages'),
      '#open' => TRUE,
    ];
    $form['search_pages']['add_page'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'container-inline',
        ],
      ],
    ];

    // In order to prevent validation errors for the parent form, this cannot be
    // required, see self::validateAddSearchPage().
    $form['search_pages']['add_page']['search_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Search page type'),
      '#empty_option' => $this
        ->t('- Choose page type -'),
      '#options' => array_map(function ($definition) {
        return $definition['title'];
      }, $this->searchManager
        ->getDefinitions()),
    ];
    $form['search_pages']['add_page']['add_search_submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Add search page'),
      '#validate' => [
        '::validateAddSearchPage',
      ],
      '#submit' => [
        '::submitAddSearchPage',
      ],
      '#limit_validation_errors' => [
        [
          'search_type',
        ],
      ],
    ];

    // Move the listing into the search_pages element.
    $form['search_pages'][$this->entitiesKey] = $form[$this->entitiesKey];
    $form['search_pages'][$this->entitiesKey]['#empty'] = $this
      ->t('No search pages have been configured.');
    unset($form[$this->entitiesKey]);
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save configuration'),
      '#button_type' => 'primary',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultOperations(EntityInterface $entity) {

    /** @var $entity \Drupal\search\SearchPageInterface */
    $operations = parent::getDefaultOperations($entity);

    // Prevent the default search from being disabled or deleted.
    if ($entity
      ->isDefaultSearch()) {
      unset($operations['disable'], $operations['delete']);
    }
    else {
      $operations['default'] = [
        'title' => $this
          ->t('Set as default'),
        'url' => Url::fromRoute('entity.search_page.set_default', [
          'search_page' => $entity
            ->id(),
        ]),
        'weight' => 50,
      ];
    }
    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $search_settings = $this
      ->config('search.settings');

    // If these settings change, the default index needs to be rebuilt.
    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')) {
      $search_settings
        ->set('index.minimum_word_size', $form_state
        ->getValue('minimum_word_size'));
      $search_settings
        ->set('index.overlap_cjk', $form_state
        ->getValue('overlap_cjk'));

      // Specifically mark items in the default index for reindexing, since
      // these settings are used in the SearchIndex::index() function.
      $this->messenger
        ->addStatus($this
        ->t('The default search index will be rebuilt.'));
      $this->searchIndex
        ->markForReindex();
    }
    $search_settings
      ->set('index.cron_limit', $form_state
      ->getValue('cron_limit'))
      ->set('logging', $form_state
      ->getValue('logging'))
      ->save();
    $this->messenger
      ->addStatus($this
      ->t('The configuration options have been saved.'));
  }

  /**
   * Form submission handler for the reindex button on the search admin settings
   * form.
   */
  public function searchAdminReindexSubmit(array &$form, FormStateInterface $form_state) {

    // Send the user to the confirmation page.
    $form_state
      ->setRedirect('search.reindex_confirm');
  }

  /**
   * Form validation handler for adding a new search page.
   */
  public function validateAddSearchPage(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->isValueEmpty('search_type')) {
      $form_state
        ->setErrorByName('search_type', $this
        ->t('You must select the new search page type.'));
    }
  }

  /**
   * Form submission handler for adding a new search page.
   */
  public function submitAddSearchPage(array &$form, FormStateInterface $form_state) {
    $form_state
      ->setRedirect('search.add_type', [
      'search_plugin_id' => $form_state
        ->getValue('search_type'),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigEntityListBuilder::load public function Loads entities of this type from storage for listing. Overrides EntityListBuilder::load 7
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DraggableListBuilder::$entitiesKey protected property The key to use for the form element containing the entities. 3
DraggableListBuilder::$formBuilder protected property The form builder.
DraggableListBuilder::$limit protected property The number of entities to list per page, or FALSE to list all entities. Overrides EntityListBuilder::$limit
DraggableListBuilder::$weightKey protected property Name of the entity's weight field or FALSE if no field is provided.
DraggableListBuilder::formBuilder protected function Returns the form builder.
DraggableListBuilder::render public function Builds the entity listing as renderable array for table.html.twig. Overrides EntityListBuilder::render 1
EntityHandlerBase::$moduleHandler protected property The module handler to invoke hooks on. 2
EntityHandlerBase::moduleHandler protected function Gets the module handler. 2
EntityHandlerBase::setModuleHandler public function Sets the module handler for this handler.
EntityListBuilder::$entityType protected property Information about the entity type.
EntityListBuilder::$entityTypeId protected property The entity type ID.
EntityListBuilder::$storage protected property The entity storage class. 1
EntityListBuilder::buildOperations public function Builds a renderable list of operation links for the entity. 2
EntityListBuilder::ensureDestination protected function Ensures that a destination is present on the given URL.
EntityListBuilder::getEntityIds protected function Loads entity IDs using a pager sorted by the entity id. 4
EntityListBuilder::getLabel Deprecated protected function Gets the label of an entity.
EntityListBuilder::getOperations public function Provides an array of information to build a list of operation links. Overrides EntityListBuilderInterface::getOperations 2
EntityListBuilder::getStorage public function Gets the entity storage. Overrides EntityListBuilderInterface::getStorage
EntityListBuilder::getTitle protected function Gets the title of the page. 1
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SearchPageListBuilder::$configFactory protected property Stores the configuration factory.
SearchPageListBuilder::$entities protected property The entities being listed. Overrides DraggableListBuilder::$entities
SearchPageListBuilder::$messenger protected property The messenger. Overrides MessengerTrait::$messenger
SearchPageListBuilder::$searchIndex protected property The search index.
SearchPageListBuilder::$searchManager protected property The search manager.
SearchPageListBuilder::buildForm public function Form constructor. Overrides DraggableListBuilder::buildForm
SearchPageListBuilder::buildHeader public function Builds the header row for the entity listing. Overrides DraggableListBuilder::buildHeader
SearchPageListBuilder::buildRow public function Builds a row for an entity in the entity listing. Overrides DraggableListBuilder::buildRow
SearchPageListBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityListBuilder::createInstance
SearchPageListBuilder::getDefaultOperations public function Gets this list's default operations. Overrides ConfigEntityListBuilder::getDefaultOperations
SearchPageListBuilder::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SearchPageListBuilder::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SearchPageListBuilder::searchAdminReindexSubmit public function Form submission handler for the reindex button on the search admin settings form.
SearchPageListBuilder::submitAddSearchPage public function Form submission handler for adding a new search page.
SearchPageListBuilder::submitForm public function Form submission handler. Overrides DraggableListBuilder::submitForm
SearchPageListBuilder::validateAddSearchPage public function Form validation handler for adding a new search page.
SearchPageListBuilder::validateForm public function Form validation handler. Overrides DraggableListBuilder::validateForm
SearchPageListBuilder::__construct public function Constructs a new SearchPageListBuilder object. Overrides DraggableListBuilder::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.