From b397954edc97902eb468584b6dbfb71e7b8b8dae Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sat, 10 Aug 2013 01:04:59 +0100 Subject: [PATCH] Issue #2052325 by yanniboi, YesCT: Move search block form to new drupal 8 standard. --- .../Drupal/search/Form/SearchBlockForm.php | 90 +++++++++++++++++++ .../search/Plugin/Block/SearchBlock.php | 26 +++++- core/modules/search/search.module | 68 -------------- 3 files changed, 113 insertions(+), 71 deletions(-) create mode 100644 core/modules/search/lib/Drupal/search/Form/SearchBlockForm.php diff --git a/core/modules/search/lib/Drupal/search/Form/SearchBlockForm.php b/core/modules/search/lib/Drupal/search/Form/SearchBlockForm.php new file mode 100644 index 00000000000..8d0985abaf0 --- /dev/null +++ b/core/modules/search/lib/Drupal/search/Form/SearchBlockForm.php @@ -0,0 +1,90 @@ +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'); + } + } +} diff --git a/core/modules/search/lib/Drupal/search/Plugin/Block/SearchBlock.php b/core/modules/search/lib/Drupal/search/Plugin/Block/SearchBlock.php index 00f5b600381..b692e5e2937 100644 --- a/core/modules/search/lib/Drupal/search/Plugin/Block/SearchBlock.php +++ b/core/modules/search/lib/Drupal/search/Plugin/Block/SearchBlock.php @@ -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); } - } diff --git a/core/modules/search/search.module b/core/modules/search/search.module index b3f381c95cf..ac06f3eb2d3 100644 --- a/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -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; -}