diff --git a/examples.module b/examples.module index e866557..799542e 100644 --- a/examples.module +++ b/examples.module @@ -41,6 +41,7 @@ function examples_toolbar() { 'dbtng_example' => 'dbtng_example', 'email_example' => 'email_example.description', 'field_example' => 'field_example.description', + 'form_example' => 'form_example.description', 'js_example' => 'js_example.info', 'node_type_example' => 'config_node_type_example.description', 'page_example' => 'page_example_description', diff --git a/form_example/form_example.info.yml b/form_example/form_example.info.yml new file mode 100644 index 0000000..fa3e8ce --- /dev/null +++ b/form_example/form_example.info.yml @@ -0,0 +1,7 @@ +name: Form Example +type: module +description: Sample implementations that demonstrate the Drupal Forms API +package: Example modules +core: 8.x +dependencies: + - examples diff --git a/form_example/form_example.links.menu.yml b/form_example/form_example.links.menu.yml new file mode 100644 index 0000000..680fb6c --- /dev/null +++ b/form_example/form_example.links.menu.yml @@ -0,0 +1,9 @@ +# Define default links for this module. +form_example.description: + title: Form Examples + description: Form examples using Drupal Form API. + route_name: form_example.description +form_example.simple_form: + title: Simple Form Example + description: A simple form example with submit processing. + route_name: form_example.simple_form diff --git a/form_example/form_example.module b/form_example/form_example.module new file mode 100644 index 0000000..9e7f225 --- /dev/null +++ b/form_example/form_example.module @@ -0,0 +1,19 @@ + t('

Form examples to demonstrate comment UI solutions using the Drupal Form API.

'), + ); + $content['links'] = array( + '#theme' => 'item_list', + '#items' => array( + $this->l(t('Simple Form'), new Url('form_example.simple_form')), + ), + ); + return $content; + } + +} diff --git a/form_example/src/Form/SimpleForm.php b/form_example/src/Form/SimpleForm.php new file mode 100644 index 0000000..2b56445 --- /dev/null +++ b/form_example/src/Form/SimpleForm.php @@ -0,0 +1,114 @@ + 'textfield', + '#title' => t('Title'), + '#required' => TRUE, + ); + + // Group submit handlers in an actions element with a key of "actions" so + // that it gets styled correctly, and so that other modules may add actions + // to the form. + $form['actions'] = array( + '#type' => 'actions', + ); + + // Add a submit button that handles the submission of the form. + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit'), + ); + + return $form; + } + + /** + * Getter method for Form ID. + * + * The form ID is used in implementations of hook_form_alter() to allow other + * modules to alter the render array built by this form controller. it must + * be unique site wide. It normally starts with the providing module's name. + * + * @return string + * The unique ID of the form defined by this class. + */ + public function getFormId() { + return 'form_example_simple_form'; + } + + /** + * Implements form validation. + * + * The validateForm method is the default method called to validate input on + * a form. + * + * @param array $form + * The render array of the currently built form. + * @param FormStateInterface $form_state + * Object describing the current state of the form. + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + $title = $form_state->getValue('title'); + if (strlen($title) < 5) { + // Set an error for the form element with a key of "title" + $form_state->setErrorByName('title', $this->t('The must be at least 5 characters long')); + } + } + + /** + * Implements a form submit handler. + * + * The submitForm method is the default method called for any submit elements. + * + * @param array $form + * The render array of the currently built form. + * @param FormStateInterface $form_state + * Object describing the current state of the form. + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + /* + * This would normally be replaced by code that actually does something + * with the title. + */ + $title = $form_state->getValue('title'); + drupal_set_message(t('You specified a title of %title.', ['%title' => $title])); + } + +} diff --git a/form_example/src/Tests/SimpleFormTest.php b/form_example/src/Tests/SimpleFormTest.php new file mode 100644 index 0000000..51db61d --- /dev/null +++ b/form_example/src/Tests/SimpleFormTest.php @@ -0,0 +1,61 @@ +drupalGet(''); + $this->assertResponse(200, 'The Home page is available.'); + $this->assertLinkByHref('examples/form_example/simple_form'); + + // Verify that anonymous can access the simpletest_examples page. + $this->drupalGet('examples/form_example/simple_form'); + $this->assertResponse(200, 'The Simple Form Example page is available.'); + + // Post a title. + $edit = ['title' => 'My Custom Title']; + $this->drupalPostForm('/examples/form_example/simple_form', $edit, t('Submit')); + $this->assertText('You specified a title of My Custom Title.'); + } + +}