Added a check for Simple Search block to work only if "Enable Search Fields" is enable.

This commit is contained in:
Kyle Huynh 2023-02-07 10:21:42 -05:00
parent 2f3826fac1
commit b1b0065c2a
2 changed files with 76 additions and 50 deletions

View file

@ -23,25 +23,38 @@ class SearchForm extends FormBase
*/ */
public function buildForm(array $form, FormStateInterface $form_state) public function buildForm(array $form, FormStateInterface $form_state)
{ {
$block = \Drupal\block\Entity\Block::load("search"); $config = \Drupal::config(SettingsForm::CONFIG_NAME);
if ($block) {
$settings = $block->get('settings');
$view_machine_name = $settings['search_view_machine_name'];
if (!$config->get(SettingsForm::SEARCH_ALL_FIELDS_FLAG)) {
$form['search-attributes'][SettingsForm::SEARCH_ALL_FIELDS_FLAG] = [
'#markup' => $this
->t('<strong>This block is required to enable searching all fields for the Advanced Search.
To proceed, please enable the Search All fields in
<a href="/admin/config/search/advanced" target="_blank">Advanced Seach Configuration</a></strong>.'),
];
} }
$form['search-textfield'] = array( else {
'#type' => 'textfield', $block = \Drupal\block\Entity\Block::load("search");
'#title' => (!empty($settings['search_textfield_label']) ? $settings['search_textfield_label'] : ''),
'#attributes' => ['placeholder' => $settings['search_placeholder']]
);
$form['actions']['#type'] = 'actions'; if ($block) {
$form['actions']['submit'] = array( $settings = $block->get('settings');
'#type' => 'submit', $view_machine_name = $settings['search_view_machine_name'];
'#value' => (!empty($settings['search_submit_label']) ? $settings['search_submit_label'] : 'Search'),
'#button_type' => 'primary', }
); $form['search-textfield'] = array(
'#type' => 'textfield',
'#title' => (!empty($settings['search_textfield_label']) ? $settings['search_textfield_label'] : ''),
'#attributes' => ['placeholder' => $settings['search_placeholder']]
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => (!empty($settings['search_submit_label']) ? $settings['search_submit_label'] : 'Search'),
'#button_type' => 'primary',
);
}
return $form; return $form;
} }

View file

@ -4,6 +4,7 @@ namespace Drupal\advanced_search\Plugin\Block;
use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\advanced_search\Form\SettingsForm;
/** /**
* Provides a 'SearchBlock' block. * Provides a 'SearchBlock' block.
@ -27,46 +28,58 @@ class SearchBlock extends BlockBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function blockForm($form, FormStateInterface $form_state) { public function blockForm($form, FormStateInterface $form_state) {
$config = \Drupal::config(SettingsForm::CONFIG_NAME);
$form['search-attributes'] = [ $form['search-attributes'] = [
'#type' => 'fieldset', '#type' => 'fieldset',
'#title' => $this->t('Configure Search Block'), '#title' => $this->t('Configure Search Block'),
]; ];
$views = \Drupal::EntityTypeManager()->getStorage('view')->loadMultiple();
$options = []; if (!$config->get(SettingsForm::SEARCH_ALL_FIELDS_FLAG)) {
foreach ($views as $view_name => $view) { $form['search-attributes'][SettingsForm::SEARCH_ALL_FIELDS_FLAG] = [
$displays = $view->get("display"); '#markup' => $this
foreach ($displays as $display) { ->t('<strong>This block is required to enable searching all fields for the Advanced Search.
if ($display['display_plugin'] === "page") { To proceed, please enable the Search All fields in
$options["view.$view_name". "." . $display['id']] = "view.$view_name". "." . $display['id']; <a href="/admin/config/search/advanced" target="_blank">Advanced Seach Configuration</a></strong>.'),
} ];
}
} }
$form['search-attributes']['view_machine_name'] = [ else {
'#type' => 'select', $views = \Drupal::EntityTypeManager()->getStorage('view')->loadMultiple();
'#title' => $this->t('Select Search Results Page\'s Machine Name:'), $options = [];
'#default_value' => $this->configuration['search_view_machine_name'], foreach ($views as $view_name => $view) {
'#options' => $options $displays = $view->get("display");
]; foreach ($displays as $display) {
$form['search-attributes']['search_textfield'] = [ if ($display['display_plugin'] === "page") {
'#type' => 'textfield', $options["view.$view_name". "." . $display['id']] = "view.$view_name". "." . $display['id'];
'#title' => $this->t('Search Keyword Textfield Label:'), }
'#default_value' => $this->configuration['search_textfield_label'], }
'#maxlength' => 255, }
]; $form['search-attributes']['view_machine_name'] = [
$form['search-attributes']['search_placeholder_textfield'] = [ '#type' => 'select',
'#type' => 'textfield', '#title' => $this->t('Select Search Results Page\'s Machine Name:'),
'#title' => $this->t('Search Keyword Textfield Placeholder:'), '#default_value' => $this->configuration['search_view_machine_name'],
'#default_value' => $this->configuration['search_placeholder'], '#options' => $options
'#maxlength' => 255, ];
]; $form['search-attributes']['search_textfield'] = [
'#type' => 'textfield',
$form['search-attributes']['search_submit'] = [ '#title' => $this->t('Search Keyword Textfield Label:'),
'#type' => 'textfield', '#default_value' => $this->configuration['search_textfield_label'],
'#title' => $this->t('Search Button Label:'), '#maxlength' => 255,
'#default_value' => $this->configuration['search_submit_label'], ];
'#maxlength' => 255, $form['search-attributes']['search_placeholder_textfield'] = [
]; '#type' => 'textfield',
'#title' => $this->t('Search Keyword Textfield Placeholder:'),
'#default_value' => $this->configuration['search_placeholder'],
'#maxlength' => 255,
];
$form['search-attributes']['search_submit'] = [
'#type' => 'textfield',
'#title' => $this->t('Search Button Label:'),
'#default_value' => $this->configuration['search_submit_label'],
'#maxlength' => 255,
];
}
return $form; return $form;
} }