Disclaimer: This is my first attempt at contributing to core, so this issue may be a duplication or in the wrong place, but I'm going to give it a go.

Having just learnt about the new drupal 8 way of doing forms (using FormInterface) I noticed that the search form that is rendered in the search block is still using the drupal 7 way of creating forms so I am going to post a patch to move it to the new way.

The change notice https://drupal.org/node/1932058 Use FormInterface to group form building, validation, and submission
helped here.

Comments

yanniboi’s picture

Status: Active » Needs review
StatusFileSize
new6.22 KB

I've tested this patch against the 8.x branch of drupal and it seems to work well.

As to performance questions, I have included a validate function in the FormInterface implementation, but since at this time no validation is done, I was wondering if it would be better to just leave it out?

Status: Needs review » Needs work

The last submitted patch, move_search_block_form_drupal_8_standard-2052325-1.patch, failed testing.

yanniboi’s picture

Status: Needs work » Needs review
yesct’s picture

I think test coverage for this is provided in core/modules/search/lib/Drupal/search/Tests/SearchBlockTest.php

yesct’s picture

+++ b/core/modules/search/lib/Drupal/search/Form/SearchBlockForm.phpundefined
@@ -0,0 +1,78 @@
+   * Implements \Drupal\Core\Form\FormInterface::getFormID().

these can be inheritdoc now #1906474: [policy adopted] Stop documenting methods with "Overrides …"

+++ b/core/modules/search/lib/Drupal/search/Form/SearchBlockForm.phpundefined
@@ -0,0 +1,78 @@
+ * Search form for the Search Block.

classes get third person verbs also. https://drupal.org/coding-standards/docs#classes

and some newlines: https://drupal.org/node/608152#indenting

and took out some trailing white space.

I'll test it next and take a look at the actual conversion.

yesct’s picture

Status: Needs review » Needs work

I compared the

878 $ diff !$ ../patch-garbage/search-interface-buildform.php
diff ../patch-garbage/search-interface-search_box.php ../patch-garbage/search-interface-buildform.php
1,2c1,2
< function search_box($form, &$form_state, $form_id) {
<   $form[$form_id] = array(
---
> public function buildForm(array $form, array &$form_state) {
>   $form['search_block_form'] = array(
12d11
<   $form['#submit'][] = 'search_box_form_submit';

[~/foo/d8]
07:49 PM [YesCT]  (drush-iq-move-search-block-form-to-new-drupal-2052325-#1) 
881 $ diff ../patch-garbage/search-interface-search_box_form_submit.php ../patch-garbage/search-interface-submitForm.php
1c1
< function search_box_form_submit($form, &$form_state) {
---
> public function submitForm(array &$form, array &$form_state) {

and the copy is very straight forward. so I think it is ok.

while looking I noticed $_GET

----

+++ b/core/modules/search/lib/Drupal/search/Form/SearchBlockForm.phpundefined
@@ -0,0 +1,81 @@
+    if (isset($_GET['destination'])) {

I think these $_GET should be replaced with getting from the request.

ending up with something like $this->request->query->get('...

yanniboi’s picture

Status: Needs work » Needs review
StatusFileSize
new6.46 KB

OK, so I did some research on request (http://symfony.com/doc/2.0/components/http_foundation/introduction.html) and had a look at some examples on dependency injection. Then @dawehner pointed out that you can get the request in the formBuild() by doing:

 public function buildForm(array $form, array &$form_state, Request $request = NULL) {

So I added the protected $request in order to set it to the object in the build function and use it in the submit function.

I have tested this on a vanilla install, but I was unsure how to make sure the destination was set to test its removal.

Status: Needs review » Needs work

The last submitted patch, move_search_block_form_drupal_8_standard-2052325-7.patch, failed testing.

yanniboi’s picture

StatusFileSize
new1.99 KB

Due to popular demand, here is an interdiff between 5 and 7.

yanniboi’s picture

Status: Needs work » Needs review
dawehner’s picture

Status: Needs review » Needs work
+++ b/core/modules/search/lib/Drupal/search/Form/SearchBlockForm.phpundefined
@@ -0,0 +1,92 @@
+    if (isset($this->request) && $this->request->query->has('destination')) {

I was stumbling upon that isset. How can request not be set here. We figured it out that the request object is actually not injected from drupal_get_form. For form pages you have the controller resolver which cares about the injection but here we need to inject the request object into the SearchBlock plugin and then put it into the form arguments via drupal_get_form

+++ b/core/modules/search/lib/Drupal/search/Plugin/Block/SearchBlock.phpundefined
@@ -33,7 +34,7 @@ public function access() {
-    return array(drupal_get_form('search_block_form'));
+    return array(drupal_get_form(new SearchBlockForm()));

This should not be an array, as drupal_get_form is already a render array.

+++ b/core/modules/search/search.moduleundefined
@@ -1309,14 +1252,3 @@ function search_simplify_excerpt_match($key, $text, $offset, $boundary, $langcod
-/**
- * Implements hook_forms().
- */
-function search_forms() {
-  $forms['search_block_form']= array(
-    'callback' => 'search_box',
-    'callback arguments' => array('search_block_form'),
-  );
-  return $forms;

Back in the days there has been two implementations ('search_block_form', 'search_theme_form'), so it is safe to remove now.

yanniboi’s picture

Status: Needs work » Needs review
StatusFileSize
new7.44 KB
new2.53 KB

@dawehner is a genius!! I am learning loads, thanks!

Ok, so I implemented ContainerFactoryPluginInterface to then use create() and __construct() in the SearchBlock class in order to get hold of $request, and then I passed it as an argument to drupal_get_form(). This seems to have worked and I have even tested using the search block in a drupal install with the destination set in the query. Everthing works as expected.

tstoeckler’s picture

Status: Needs review » Needs work

Wow, this is an awesome patch. You removed search_box()!!! Nice...

Also, quite impressive that there's so little to complain after only ~10 comments on your first patch. Way to go!

I just found some small nitpicks, and then this should be ready to go.

+++ b/core/modules/search/lib/Drupal/search/Form/SearchBlockForm.php
@@ -0,0 +1,92 @@
+use Drupal\Core\Controller\ControllerInterface;
...
+use Symfony\Component\DependencyInjection\ContainerInterface;

I might be missing something, but I think these are not actually used in this file.

+++ b/core/modules/search/lib/Drupal/search/Form/SearchBlockForm.php
@@ -0,0 +1,92 @@
+ * Builds the search form for the Search Block.

"Search" and "Block" should be lowercase.

yanniboi’s picture

Status: Needs work » Needs review
StatusFileSize
new7.33 KB
new720 bytes

@tstoeckler, you are allowed to be nitpicky, this is core after all...

I followed your advice and made the changes as you described. Thanks for the feedback.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

That is ready to fly now.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed b397954 and pushed to 8.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

adding link to doc used to make the first patch.