diff -u b/fapi_example/fapi_example.links.menu.yml b/fapi_example/fapi_example.links.menu.yml --- b/fapi_example/fapi_example.links.menu.yml +++ b/fapi_example/fapi_example.links.menu.yml @@ -1,6 +1,6 @@ # Define default links for this module. fapi_example.description: - title: From API Examples + title: Form API Examples description: Form examples using Drupal Form API. route_name: fapi_example.description @@ -43,5 +43,5 @@ fapi_example.build_demo: title: Build Form Demo - description: A form build demonstration. + description: Demostrates the order of firing of from controller methods. route_name: fapi_example.build_demo parent: fapi_example.description diff -u b/fapi_example/fapi_example.routing.yml b/fapi_example/fapi_example.routing.yml --- b/fapi_example/fapi_example.routing.yml +++ b/fapi_example/fapi_example.routing.yml @@ -5,8 +5,8 @@ # controllers the content is determined by the buildForm method defined by the # form controller implementation. -# Access to these paths is restricted to users with the permission 'access content'. -# This is notated as _permission: 'access content'. +# Access to these paths is restricted to users with the permission +# 'access content'. This is notated as _permission: 'access content'. # Menu items corresponding to these URLs are defined separately in the # fapi_example.links.menu.yml file. diff -u b/fapi_example/src/Form/AjaxDemo.php b/fapi_example/src/Form/AjaxDemo.php --- b/fapi_example/src/Form/AjaxDemo.php +++ b/fapi_example/src/Form/AjaxDemo.php @@ -17,6 +17,11 @@ */ class AjaxDemo extends DemoBase { + /* + * Possible colors to choose from. + * Used by colorCallback to determine which colors to include in the + * select element. + */ private $colors = [ 'warm' => [ 'red' => 'Red', @@ -31,16 +36,17 @@ ]; /** - * Build the AJAX demo form. - * - * The #ajax attribute used in the temperature input element defines an ajax - * callback that will invoke the colorCallback method on this form object. - * Whenever the temperature element changes, it will invoke this callback and - * replace the contents of the color_wrapper container with the reults of this - * method call. + * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { + /* + * The #ajax attribute used in the temperature input element defines an ajax + * callback that will invoke the colorCallback method on this form object. + * Whenever the temperature element changes, it will invoke this callback + * and replace the contents of the color_wrapper container with the reults of this + * method call. + */ $form['temperature'] = [ '#title' => $this->t('Temperature'), '#type' => 'select', @@ -52,6 +58,8 @@ 'wrapper' => 'color-wrapper', ] ]; + + // Disable caching on this form. $form_state->setCached(FALSE); $form['actions'] =[ @@ -74,20 +82,21 @@ } /** - * Getter method for Form ID. - * - * @inheritdoc + * {@inheritdoc} */ public function getFormId() { return 'fapi_example_ajax_demo'; } /** - * Callback for Ajax event on color selection. + * Implements callback for Ajax event on color selection. */ public function colorCallback(array &$form, FormStateInterface $form_state) { $temperature = $form_state->getValue('temperature'); + // Add a color element to the color_wrapper container using the value + // from temparature to determine which colors to include in the select + // element. $form['color_wrapper']['color'] = [ '#type' => 'select', '#title' => $this->t('Color'), diff -u b/fapi_example/src/Form/BuildDemo.php b/fapi_example/src/Form/BuildDemo.php --- b/fapi_example/src/Form/BuildDemo.php +++ b/fapi_example/src/Form/BuildDemo.php @@ -15,34 +15,47 @@ * Implements the build demo form controller. * * The form uses drupal_set_message() calls to demonstrate the order of - * contoller method invocations by the form api. Note that currently there is - * no constructor in the FormBase class. + * contoller method invocations by the form api. * * @see \Drupal\Core\Form\FormBase * @see \Drupal\Core\Form\ConfigFormBase */ class BuildDemo extends FormBase { + /** + * Counter keeping track of the sequence of method invocation. + * @var int + */ + static $_sequence = 0; + + /** + * {@inheritdoc} + */ public function __construct() { - // Static variables here are used to tell you how often these methods - // are called within a single page load. - static $i=0; - $i++; - drupal_set_message("__construct $i"); + $this->displayMethodInvocation('__contstruct'); + } + + /** + * Display the method being called and it's sequence in the form + * processing. + * + * @param $method_name + * The method being invoked. + */ + private function displayMethodInvocation($method_name) { + self::$_sequence++; + drupal_set_message(self::$_sequence . ". $method_name"); } /** * Build form demonstration. * - * This form demonstrates the different type of build form events and their - * order of firing. + * This form demonstrates the different type of build form methods and their + * order of invocation. */ public function buildForm(array $form, FormStateInterface $form_state) { - static $i = 0; - $i++; - drupal_set_message("buildForm $i"); - + // Simple checkbox for ajax orders. $form['change'] = [ '#type' => 'checkbox', '#title' => $this->t('Change Me'), @@ -51,6 +64,7 @@ 'wrapper' => 'message-wrapper', ], ]; + $form['actions'] = [ '#type' => 'actions', ]; @@ -92,64 +106,35 @@ } /** - * 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. + * {@inheritdoc} */ public function getFormId() { - static $i = 0; - $i++; - drupal_set_message("getFormId $i"); + $this->displayMethodInvocation('getFormId'); return 'fapi_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. + * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { - static $i = 0; - $i++; - drupal_set_message("validateForm $i"); + $this->displayMethodInvocation('validateForm'); } /** - * 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. + * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - static $i = 0; - $i++; - drupal_set_message("submitForm $i"); + $this->displayMethodInvocation('submitForm'); } /** + * Implements ajax submit callback. + * * @param array $form * @param \Drupal\Core\Form\FormStateInterface $form_state - * Demonstrates ajax button submit. */ public function ajaxSubmit(array &$form, FormStateInterface $form_state) { - static $i = 0; - $i++; - drupal_set_message("ajaxSubmit $i"); + $this->displayMethodInvocation('ajaxSubmit'); $form['messages']['status'] = [ '#type' => 'status_messages', ]; @@ -158,14 +143,13 @@ } /** + * Implements submit callback for Rebuild button. + * * @param array $form * @param \Drupal\Core\Form\FormStateInterface $form_state - * Test a submit that causes form state rebuild */ public function rebuildFormSubmit(array &$form, FormStateInterface $form_state) { - static $i = 0; - $i++; - drupal_set_message("rebuildFormSubmit $i"); + $this->displayMethodInvocation('rebuildFormSubmit'); $form_state->setRebuild(TRUE); }