forked from Islandora/advanced_search
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple search block associated with an Advanced Search View Page
- Loading branch information
1 parent
2afe41c
commit 3168f18
Showing
2 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace Drupal\islandora_advanced_search\Plugin\Block; | ||
|
||
use Drupal\Core\Block\BlockBase; | ||
use Drupal\Core\Form\FormStateInterface; | ||
|
||
/** | ||
* Provides a 'SearchBlock' block. | ||
* | ||
* @Block( | ||
* id = "search_block", | ||
* admin_label = @Translation("Search"), | ||
* ) | ||
*/ | ||
class SearchBlock extends BlockBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultConfiguration() { | ||
return [ | ||
] + parent::defaultConfiguration(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function blockForm($form, FormStateInterface $form_state) { | ||
$form['search-attributes'] = [ | ||
'#type' => 'fieldset', | ||
'#title' => $this->t('Configure Search Block'), | ||
]; | ||
$views = \Drupal::EntityTypeManager()->getStorage('view')->loadMultiple(); | ||
$options = []; | ||
foreach ($views as $view_name => $view) { | ||
$displays = $view->get("display"); | ||
foreach ($displays as $display) { | ||
if ($display['display_plugin'] === "page") { | ||
$options["view.$view_name". "." . $display['id']] = "view.$view_name". "." . $display['id']; | ||
} | ||
} | ||
} | ||
$form['search-attributes']['view_machine_name'] = [ | ||
'#type' => 'select', | ||
'#title' => $this->t('Select Search Results Page\'s Machine Name:'), | ||
'#default_value' => $this->configuration['search_view_machine_name'], | ||
'#options' => $options | ||
]; | ||
$form['search-attributes']['search_textfield'] = [ | ||
'#type' => 'textfield', | ||
'#title' => $this->t('Search Keyword Textfield Label:'), | ||
'#default_value' => $this->configuration['search_textfield_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; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function blockSubmit($form, FormStateInterface $form_state) { | ||
$this->configuration['search_view_machine_name'] = $form_state->getValues()['search-attributes']['view_machine_name']; | ||
$this->configuration['search_textfield_label'] = $form_state->getValues()['search-attributes']['search_textfield']; | ||
$this->configuration['search_placeholder'] = $form_state->getValues()['search-attributes']['search_placeholder_textfield']; | ||
$this->configuration['search_submit_label'] = $form_state->getValues()['search-attributes']['search_submit']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function build() { | ||
return \Drupal::formBuilder()->getForm('Drupal\islandora_advanced_search\Form\SearchForm'); | ||
} | ||
|
||
} |