Add a simple search block associated with an Advanced Search View Page

This commit is contained in:
Kyle Huynh 2021-09-17 00:55:40 -04:00
parent 2afe41ce79
commit 3168f18b6c
2 changed files with 156 additions and 0 deletions

66
src/Form/SearchForm.php Normal file
View file

@ -0,0 +1,66 @@
<?php
namespace Drupal\islandora_advanced_search\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use \Drupal\Core\Url;
class SearchForm extends FormBase
{
/**
* {@inheritdoc}
*/
public function getFormId()
{
return 'search_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$block = \Drupal\block\Entity\Block::load("search");
if ($block) {
$settings = $block->get('settings');
$view_machine_name = $settings['search_view_machine_name'];
}
$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;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
$block = \Drupal\block\Entity\Block::load("search");
if ($block) {
$settings = $block->get('settings');
$view_machine_name = $settings['search_view_machine_name'];
}
$url = Url::fromRoute($view_machine_name, [
'a[0][f]' => 'all',
'a[0][i]' => 'IS',
'a[0][v]' => $form_state->getValues()['search-textfield'],
]);
$form_state->setRedirectUrl($url);
}
}