diff --git a/form_example/form_example.info.yml b/form_example/form_example.info.yml new file mode 100644 index 0000000..2232340 --- /dev/null +++ b/form_example/form_example.info.yml @@ -0,0 +1,5 @@ +name: Form example +type: module +description: 'Demonstrates hook_form() and related features' +package: Example modules +core: 8.x diff --git a/form_example/form_example.module b/form_example/form_example.module new file mode 100644 index 0000000..c294890 --- /dev/null +++ b/form_example/form_example.module @@ -0,0 +1,55 @@ + 'Form Example', + 'route_name' => 'form_example', + 'access callback' => TRUE, + 'expanded' => TRUE, + ); + $items['examples/form_example/tutorial'] = array( + 'title' => 'Form Tutorial', + // 'page callback' => 'drupal_get_form', + 'route_name' => array('form_example_tutorial_1'), + 'access callback' => TRUE, + 'description' => 'A set of ten tutorials', + // 'file' => 'form_example_tutorial.inc', + // 'type' => MENU_NORMAL_ITEM, + ); + $items['examples/form_example/tutorial/1'] = array( + 'title' => '#1', + // 'page callback' => 'drupal_get_form', + 'route_name' => array('form_example_tutorial_1'), + 'access callback' => TRUE, + 'description' => 'Tutorial 1: Simplest form', + // 'type' => MENU_DEFAULT_LOCAL_TASK, + // 'file' => 'form_example_tutorial.inc', + ); + return $items; +} diff --git a/form_example/form_example.routing.yml b/form_example/form_example.routing.yml new file mode 100644 index 0000000..04f0296 --- /dev/null +++ b/form_example/form_example.routing.yml @@ -0,0 +1,21 @@ +form_example: + path: 'examples/form_example' + defaults: + _form: '\Drupal\form_example\Forms\FormExampleForm' + # _content: '\Drupal\form_example\Controller\FormExampleController::description' + requirements: + _access: 'TRUE' +form_example_tutorial_1: + path: 'examples/form_example/tutorial/1' + defaults: + _form: '\Drupal\form_example\Forms\FormExampleForm' + requirements: + _access: 'TRUE' +# form_example_2: +# path: 'examples/form_example/tutorial/2' +# defaults: +# _form: '\Drupal\form_example\Forms\FormExampleForm1' +# requirements: +# _access: 'TRUE' + + diff --git a/form_example/lib/Drupal/form_example/Controller/FormExampleController.php b/form_example/lib/Drupal/form_example/Controller/FormExampleController.php new file mode 100644 index 0000000..d82b1b0 --- /dev/null +++ b/form_example/lib/Drupal/form_example/Controller/FormExampleController.php @@ -0,0 +1,95 @@ + t('

The form example module provides a tutorial, extensible multistep example, an element example, and a #states example

') + ); + + return $build; + } + + // /** + // * Constructs a simple page. + // * + // * The simple _content callback, mapped to the path + // * 'examples/page_example/simple'. + // * + // * _content callbacks return a renderable array with the content area of the + // * page. The theme system will later render and surround the content in the + // * appropriate blocks, navigation, and styling. + // * + // * @todo: Is this paragraph valid? + // * If you do not want to use the theme system (for example for outputting an + // * image or XML), just return the right response object. + // */ + // function simple() { + // return array( + // '#markup' => '

' . t('Simple page: The quick brown fox jumps over the lazy dog.') . '

' + // ); + // } + + // * + // * A more complex _content callback that takes arguments. + // * + // * This callback is mapped to the path + // * 'examples/page_example/arguments/{first}/{second}'. + // * + // * The arguments in brackets are passed to this callback from the page URL. + // * + // * This function also demonstrates a more complex render array in the returned + // * values. Instead of just rendering the HTML with a theme('item_list'), the + // * list is left unrendered, and a #theme attached to it so that it can be + // * rendered as late as possible, giving more parts of the system a chance to + // * change it if necessary. + // * + // * Consult @link http://drupal.org/node/930760 Render Arrays documentation + // * @endlink for details. + // * + // * @param string $first + // * A string to use, should be a number. + // * @param string $second + // * Another string to use, should be a number. + // * + // * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException + // * If the parameters are invalid. + + // function arguments($first, $second) { + // // Make sure you don't trust the URL to be safe! Always check for exploits. + // if (!is_numeric($first) || !is_numeric($second)) { + // // We will just show a standard "access denied" page in this case. + // throw new AccessDeniedHttpException(); + // } + + // $list[] = t("First number was @number.", array('@number' => $first)); + // $list[] = t("Second number was @number.", array('@number' => $second)); + // $list[] = t('The total was @number.', array('@number' => $first + $second)); + + // $render_array['page_example_arguments'] = array( + // // The theme function to apply to the #items + // '#theme' => 'item_list', + // // The list itself. + // '#items' => $list, + // '#title' => t('Argument Information'), + // ); + // return $render_array; + // } +} diff --git a/form_example/lib/Drupal/form_example/Forms/FormExampleForm.php b/form_example/lib/Drupal/form_example/Forms/FormExampleForm.php new file mode 100644 index 0000000..32c8bb7 --- /dev/null +++ b/form_example/lib/Drupal/form_example/Forms/FormExampleForm.php @@ -0,0 +1,68 @@ + 'select', + '#title' => t('Form Example'), + '#default_value' => 'Form Example', + '#options' => array( + 'form_example_1' => t('Form'), + 'form_example_2' => t('Example'), + ), + '#required' => TRUE, + ); + $form['actions']['#type'] = 'actions'; + $form['actions']['submit'] = array( + '#name' => 'op', + '#type' => 'submit', + '#value' => t('Save'), + '#button_type' => 'primary', + ); + $form['actions']['cancel'] = array( + '#name' => 'op', + '#type' => 'submit', + '#value' => t('Cancel'), + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + + parent::submitForm($form, $form_state); + } + +} diff --git a/form_example/lib/Drupal/form_example/Tests/FormExampleTestCase.php b/form_example/lib/Drupal/form_example/Tests/FormExampleTestCase.php new file mode 100644 index 0000000..8760714 --- /dev/null +++ b/form_example/lib/Drupal/form_example/Tests/FormExampleTestCase.php @@ -0,0 +1,89 @@ + 'Cron example functionality', + 'description' => 'Test the functionality of the Cron Example.', + 'group' => 'Examples', + ); + } + + /** + * Enable modules and create user with specific permissions. + */ + function setUp() { + parent::setUp(); + // Create user. Search content permission granted for the search block to + // be shown. + $this->web_user = $this->drupalCreateUser(array('administer site configuration')); + $this->drupalLogin($this->web_user); + } + + /** + * Login user, create an example node, and test block functionality through + * the admin and user interfaces. + */ + function testFormExampleBasic() { + // Pretend that cron has never been run (even though simpletest seems to + // run it once...) + variable_set('form_example_next_execution', 0); + $this->drupalGet('examples/form_example'); + + // Initial run should cause form_example_cron() to fire. + $post = array(); + $this->drupalPostForm('examples/form_example', $post, t('Run cron now')); + $this->assertText(t('form_example executed at')); + + // Forcing should also cause form_example_cron() to fire. + $post['form_reset'] = TRUE; + $this->drupalPostForm(NULL, $post, t('Run cron now')); + $this->assertText(t('form_example executed at')); + + // But if followed immediately and not forced, it should not fire. + $post['form_reset'] = FALSE; + $this->drupalPostForm(NULL, $post, t('Run cron now')); + $this->assertNoText(t('form_example executed at')); + + + $this->assertText(t('There are currently 0 items in queue 1 and 0 items in queue 2')); + $post = array( + 'num_items' => 5, + 'queue' => 'form_example_queue_1', + ); + $this->drupalPostForm(NULL, $post, t('Add jobs to queue')); + $this->assertText('There are currently 5 items in queue 1 and 0 items in queue 2'); + $post = array( + 'num_items' => 100, + 'queue' => 'form_example_queue_2', + ); + $this->drupalPostForm(NULL, $post, t('Add jobs to queue')); + $this->assertText('There are currently 5 items in queue 1 and 100 items in queue 2'); + + $post = array(); + $this->drupalPostForm('examples/form_example', $post, t('Run cron now')); + $this->assertPattern('/Queue 1 worker processed item with sequence 5 /'); + $this->assertPattern('/Queue 2 worker processed item with sequence 100 /'); + } +} + +/** + * @} End of "addtogroup form_example". + */