reverted: --- b/field_example/config/schema/field_example.schema.yml +++ a/field_example/config/schema/field_example.schema.yml @@ -8,5 +8,5 @@ label: 'Default' mapping: value: + type: string - type: integer label: 'Value' diff -u b/form_example/form_example.routing.yml b/form_example/form_example.routing.yml --- b/form_example/form_example.routing.yml +++ b/form_example/form_example.routing.yml @@ -1,3 +1,14 @@ +# This routing.yml file makes both the form example description page and the +# included sample forms available at specific URL's on your site. A route +# maps a URL path to a controller. For page controllers it defines the +# function or method that will be called when the page is accessed. For form +# controllers the content is determined by the buildForm method defined by the +# form controller implementation. + +# Access to these paths are not restricted. This is notated as _access: 'TRUE'. + +# Menu items corresponding to these URLs are defined separately in the +# form_example.links.menu.yml file. form_example.description: path: 'examples/form_example' defaults: @@ -11,4 +22,4 @@ - _form: '\Drupal\form_example\Controller\SimpleForm' + _form: '\Drupal\form_example\Form\SimpleForm' _title: 'Simple Form Example' requirements: - _access: 'TRUE' \ No newline at end of file + _access: 'TRUE' diff -u b/form_example/src/Controller/Page.php b/form_example/src/Controller/Page.php --- b/form_example/src/Controller/Page.php +++ b/form_example/src/Controller/Page.php @@ -2,21 +2,23 @@ /** * @file - * Implement simple page controller for simple pages + * Implement simple page controller for simple pages. */ + namespace Drupal\form_example\Controller; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Url; /** - * Class Page * Simple page controller for drupal. */ -class Page extends ControllerBase -{ +class Page extends ControllerBase { + /** + * Lists the examples provided by form_example. + */ public function description() { $content['intro'] = array( - '#markup' => t('
Form examples to demonstrate commen UI solutions using the Drupal Form API.
'), + '#markup' => t('Form examples to demonstrate comment UI solutions using the Drupal Form API.
'), ); $content['links'] = array( '#theme' => 'item_list', @@ -29 +31,2 @@ -} \ No newline at end of file + +} reverted: --- b/form_example/src/Controller/SimpleForm.php +++ /dev/null @@ -1,65 +0,0 @@ - 'textfield', - '#title' => t('Title'), - '#required' => TRUE, - ); - - $form['actions'] = array( - '#type' => 'actions', - ); - - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Submit'), - ); - - return $form; - } - - /** - * @param array $form - * @param FormStateInterface $form_state - * Implements a form submit handler. - * - * The submitForm method is the default method called for any submit elements - */ - 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])); - } - -} \ No newline at end of file diff -u b/form_example/src/Tests/SimpleFormTest.php b/form_example/src/Tests/SimpleFormTest.php --- b/form_example/src/Tests/SimpleFormTest.php +++ b/form_example/src/Tests/SimpleFormTest.php @@ -17,6 +17,7 @@ * SimpleTest uses group annotations to help you organize your tests. * * @group form_example + * * @ingroup form_example */ class SimpleFormTest extends WebTestBase { @@ -24,19 +25,19 @@ /** * Our module dependencies. * - * - * @var array + * @var array List of test dependencies. */ static public $modules = array('form_example'); /** * The installation profile to use with this test. - * @var string + * + * @var string Installation profile required for test. */ protected $profile = 'minimal'; /** - * Test Example Forms + * Test example forms provided by form_example. * * Enable SimpleTest Example and see if it can successfully return its main * page and if there is a link to the simpletest_example in the Tools menu. @@ -51,7 +52,7 @@ $this->drupalGet('examples/form_example/simple_form'); $this->assertResponse(200, 'The Simple Form Example page is available.'); - // Post a title + // 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.'); only in patch2: unchanged: --- /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])); + } + +}