Skip to content

Commit

Permalink
Validation for search index form and remove terms.
Browse files Browse the repository at this point in the history
  • Loading branch information
kepol committed Jan 20, 2024
1 parent 5af6ff1 commit bc65e0c
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 deletions modules/quant_search/src/Form/SearchIndexForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,54 +110,73 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#options' => $content_types,
];

$form['quant_search_index_entity_taxonomy_term'] = [
'#type' => 'checkbox',
'#title' => $this->t('Taxonomy terms'),
'#description' => $this->t('Exports taxonomy term pages.'),
];
// @todo Implement taxonomy terms.
//$form['quant_search_index_entity_taxonomy_term'] = [
//'#type' => 'checkbox',
//'#title' => $this->t('Taxonomy terms'),
//'#description' => $this->t('Exports taxonomy term pages.'),
//];

return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$process_nodes = $form_state->getValue('quant_search_index_entity_node');
$process_terms = $form_state->getValue('quant_search_index_entity_taxonomy_term');

if (empty($process_nodes) && empty($process_terms)) {
$form_state->setErrorByName('quant_search_index_entity_node', $this->t('Choose an option to update the index.'));
}
}

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

$query = \Drupal::entityQuery('node')
->accessCheck(TRUE)
->condition('status', 1);
$process_nodes = $form_state->getValue('quant_search_index_entity_node');
$process_terms = $form_state->getValue('quant_search_index_entity_taxonomy_term');

// @todo Handle taxonomy terms as well.
if ($process_nodes) {
$query = \Drupal::entityQuery('node')
->accessCheck(TRUE)
->condition('status', 1);

$bundles = $form_state->getValue('quant_search_index_entity_node_bundles');
$bundles = $form_state->getValue('quant_search_index_entity_node_bundles');

if (!empty($bundles)) {
$bundles = array_filter($bundles);
if (!empty($bundles)) {
$query->condition('type', array_keys($bundles), 'IN');
$bundles = array_filter($bundles);
if (!empty($bundles)) {
$query->condition('type', array_keys($bundles), 'IN');
}
}
}

$nids = $query->execute();
$nids = $query->execute();

// Chunk into a few batches.
$batches = array_chunk($nids, 50);
// Chunk into a few batches.
$batches = array_chunk($nids, 50);

$batch = [
'title' => $this->t('Exporting to Quant...'),
'operations' => [],
'init_message' => $this->t('Starting'),
'progress_message' => $this->t('Processed @current out of @total.'),
'error_message' => $this->t('An error occurred during processing.'),
];
$batch = [
'title' => $this->t('Exporting to Quant...'),
'operations' => [],
'init_message' => $this->t('Starting'),
'progress_message' => $this->t('Processed @current out of @total.'),
'error_message' => $this->t('An error occurred during processing.'),
];

// Filter by language.
$languages = $form_state->getValue('quant_search_index_entity_node_languages');
// Filter by language.
$languages = $form_state->getValue('quant_search_index_entity_node_languages');

foreach ($batches as $b) {
$batch['operations'][] = ['quant_search_run_index', [$b, $languages]];
}
foreach ($batches as $b) {
$batch['operations'][] = ['quant_search_run_index', [$b, $languages]];
}

batch_set($batch);
batch_set($batch);
}

parent::submitForm($form, $form_state);
}
Expand Down

0 comments on commit bc65e0c

Please sign in to comment.