Issue #2052325 by yanniboi, YesCT: Move search block form to new drupal 8 standard.
parent
895bdf2294
commit
b397954edc
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\search\Form\SearchBlockForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\search\Form;
|
||||
|
||||
use Drupal\Core\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Builds the search form for the search block.
|
||||
*/
|
||||
class SearchBlockForm implements FormInterface {
|
||||
|
||||
/**
|
||||
* The current request.
|
||||
*
|
||||
* @var \Symfony\Component\HttpFoundation\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormID() {
|
||||
return 'search_block_form';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, array &$form_state, Request $request = NULL) {
|
||||
// Save the request variable for use in the submit method.
|
||||
$this->request = $request;
|
||||
|
||||
$form['search_block_form'] = array(
|
||||
'#type' => 'search',
|
||||
'#title' => t('Search'),
|
||||
'#title_display' => 'invisible',
|
||||
'#size' => 15,
|
||||
'#default_value' => '',
|
||||
'#attributes' => array('title' => t('Enter the terms you wish to search for.')),
|
||||
);
|
||||
$form['actions'] = array('#type' => 'actions');
|
||||
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Search'));
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateForm(array &$form, array &$form_state) {
|
||||
// No validation necessary at this time.
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, array &$form_state) {
|
||||
// The search form relies on control of the redirect destination for its
|
||||
// functionality, so we override any static destination set in the request.
|
||||
// See http://drupal.org/node/292565.
|
||||
if ($this->request->query->has('destination')) {
|
||||
$this->request->query->remove('destination');
|
||||
}
|
||||
|
||||
// Check to see if the form was submitted empty.
|
||||
// If it is empty, display an error message.
|
||||
// (This method is used instead of setting #required to TRUE for this field
|
||||
// because that results in a confusing error message. It would say a plain
|
||||
// "field is required" because the search keywords field has no title.
|
||||
// The error message would also complain about a missing #title field.)
|
||||
if ($form_state['values']['search_block_form'] == '') {
|
||||
form_set_error('keys', t('Please enter some keywords.'));
|
||||
}
|
||||
|
||||
$form_id = $form['form_id']['#value'];
|
||||
$info = search_get_default_module_info();
|
||||
if ($info) {
|
||||
$form_state['redirect'] = 'search/' . $info['path'] . '/' . trim($form_state['values'][$form_id]);
|
||||
}
|
||||
else {
|
||||
form_set_error(NULL, t('Search is currently disabled.'), 'error');
|
||||
}
|
||||
}
|
||||
}
|
|
@ -10,6 +10,10 @@ namespace Drupal\search\Plugin\Block;
|
|||
use Drupal\block\BlockBase;
|
||||
use Drupal\Component\Annotation\Plugin;
|
||||
use Drupal\Core\Annotation\Translation;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\search\Form\SearchBlockForm;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Provides a 'Search form' block.
|
||||
|
@ -20,7 +24,23 @@ use Drupal\Core\Annotation\Translation;
|
|||
* module = "search"
|
||||
* )
|
||||
*/
|
||||
class SearchBlock extends BlockBase {
|
||||
class SearchBlock extends BlockBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
|
||||
return new static($configuration, $plugin_id, $plugin_definition, $container->get('request'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a SearchBlock object.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Request $request) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides \Drupal\block\BlockBase::access().
|
||||
|
@ -33,7 +53,7 @@ class SearchBlock extends BlockBase {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
return array(drupal_get_form('search_block_form'));
|
||||
// Pass the current request to drupal_get_form for use in the buildForm.
|
||||
return drupal_get_form(new SearchBlockForm(), $this->request);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -990,63 +990,6 @@ function search_form($form, &$form_state, $action = '', $keys = '', $module = NU
|
|||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Form constructor for the search block's search box.
|
||||
*
|
||||
* @param $form_id
|
||||
* The unique string identifying the desired form.
|
||||
*
|
||||
* @see search_box_form_submit()
|
||||
*
|
||||
* @ingroup forms
|
||||
*/
|
||||
function search_box($form, &$form_state, $form_id) {
|
||||
$form[$form_id] = array(
|
||||
'#type' => 'search',
|
||||
'#title' => t('Search'),
|
||||
'#title_display' => 'invisible',
|
||||
'#size' => 15,
|
||||
'#default_value' => '',
|
||||
'#attributes' => array('title' => t('Enter the terms you wish to search for.')),
|
||||
);
|
||||
$form['actions'] = array('#type' => 'actions');
|
||||
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Search'));
|
||||
$form['#submit'][] = 'search_box_form_submit';
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Form submission handler for search_box().
|
||||
*/
|
||||
function search_box_form_submit($form, &$form_state) {
|
||||
// The search form relies on control of the redirect destination for its
|
||||
// functionality, so we override any static destination set in the request.
|
||||
// See http://drupal.org/node/292565.
|
||||
if (isset($_GET['destination'])) {
|
||||
unset($_GET['destination']);
|
||||
}
|
||||
|
||||
// Check to see if the form was submitted empty.
|
||||
// If it is empty, display an error message.
|
||||
// (This method is used instead of setting #required to TRUE for this field
|
||||
// because that results in a confusing error message. It would say a plain
|
||||
// "field is required" because the search keywords field has no title.
|
||||
// The error message would also complain about a missing #title field.)
|
||||
if ($form_state['values']['search_block_form'] == '') {
|
||||
form_set_error('keys', t('Please enter some keywords.'));
|
||||
}
|
||||
|
||||
$form_id = $form['form_id']['#value'];
|
||||
$info = search_get_default_module_info();
|
||||
if ($info) {
|
||||
$form_state['redirect'] = 'search/' . $info['path'] . '/' . trim($form_state['values'][$form_id]);
|
||||
}
|
||||
else {
|
||||
form_set_error(NULL, t('Search is currently disabled.'), 'error');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a search by calling hook_search_execute().
|
||||
*
|
||||
|
@ -1307,14 +1250,3 @@ function search_simplify_excerpt_match($key, $text, $offset, $boundary, $langcod
|
|||
function _search_excerpt_match_filter($var) {
|
||||
return strlen(trim($var[0]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_forms().
|
||||
*/
|
||||
function search_forms() {
|
||||
$forms['search_block_form']= array(
|
||||
'callback' => 'search_box',
|
||||
'callback arguments' => array('search_block_form'),
|
||||
);
|
||||
return $forms;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue