Skip to content
Snippets Groups Projects
Commit 1ee5f801 authored by Angie Byron's avatar Angie Byron
Browse files

Issue #2105609 by RoSk0, jhodgdon: Convert search_embedded_form_form to a Controller.

parent c48c9411
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -7,16 +7,52 @@
namespace Drupal\search_embedded_form\Form;
use Drupal\Core\Form\FormBase;
/**
* Temporary form controller for search_embedded_form module.
* Form controller for search_embedded_form form.
*/
class SearchEmbeddedForm {
class SearchEmbeddedForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'search_embedded_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$count = $this->config('search_embedded_form.settings')->get('submitted');
$form['name'] = array(
'#type' => 'textfield',
'#title' => $this->t('Your name'),
'#maxlength' => 255,
'#default_value' => '',
'#required' => TRUE,
'#description' => $this->t('Times form has been submitted: %count', array('%count' => $count)),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Send away'),
);
return $form;
}
/**
* @todo Remove search_embedded_form_form().
* {@inheritdoc}
*/
public function testEmbeddedForm() {
return drupal_get_form('search_embedded_form_form');
public function submitForm(array &$form, array &$form_state) {
$config = $this->config('search_embedded_form.settings');
$submit_count = (int) $config->get('submitted');
$config->set('submitted', $submit_count + 1)->save();
drupal_set_message($this->t('Test form was submitted'));
}
}
......@@ -9,50 +9,10 @@
* individual product (node) listed in the search results.
*/
/**
* Form constructor for embedding search results for testing.
*
* @see search_embedded_form_form_submit().
*
* @deprecated Use \Drupal\search_embedded_form\Form\SearchEmbeddedForm::testEmbeddedForm()
*/
function search_embedded_form_form($form, &$form_state) {
$count = \Drupal::config('search_embedded_form.settings')->get('submitted');
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#maxlength' => 255,
'#default_value' => '',
'#required' => TRUE,
'#description' => t('Times form has been submitted: %count', array('%count' => $count)),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Send away'),
);
$form['#submit'][] = 'search_embedded_form_form_submit';
return $form;
}
/**
* Submit handler for search_embedded_form_form().
*/
function search_embedded_form_form_submit($form, &$form_state) {
$config = \Drupal::config('search_embedded_form.settings');
$submit_count = (int) $config->get('submitted');
$config->set('submitted', $submit_count + 1)->save();
drupal_set_message(t('Test form was submitted'));
}
/**
* Adds the test form to search results.
*/
function search_embedded_form_preprocess_search_result(&$variables) {
$form = drupal_get_form('search_embedded_form_form');
$form = \Drupal::formBuilder()->getForm('Drupal\search_embedded_form\Form\SearchEmbeddedForm');
$variables['snippet'] .= drupal_render($form);
}
......@@ -2,6 +2,6 @@ search_embedded_form.test_embedded_form:
path: '/search_embedded_form'
defaults:
_title: 'Search_Embed_Form'
_content: '\Drupal\search_embedded_form\Form\SearchEmbeddedForm::testEmbeddedForm'
_form: '\Drupal\search_embedded_form\Form\SearchEmbeddedForm'
requirements:
_permission: 'search content'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment