AbstractFormController.php

AbstractFormController

Namespace

Drupal\ooe\Form

File

lib/Drupal/ooe/Form/AbstractFormController.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * AbstractFormController
  5. */
  6. namespace Drupal\ooe\Form;
  7. use Drupal\ooe\Module\DefaultModuleHelper;
  8. use Drupal\ooe\Common;
  9. /**
  10. * Adapted from Chris Skene's page_controller (@link PageController)
  11. *
  12. * UML:
  13. * @link http://drupal7demo.webel.com.au/node/2328 AbstractFormController @endlink
  14. *
  15. * @author darrenkelly
  16. */
  17. abstract class AbstractFormController extends DefaultModuleHelper implements IFormController {
  18. // ,IModuleHelper //implicit via DefaultModuleHelper {
  19. /**
  20. * Name of the builder callback method.
  21. *
  22. * @var string
  23. */
  24. const BUILDER = 'build';
  25. /**
  26. * The name of the controller class.
  27. *
  28. * @var string
  29. */
  30. // static private $controller_class_name;
  31. /**
  32. * Form builder delegator.
  33. *
  34. * @param array $form
  35. * A Drupal form array.
  36. * @param array $form_state
  37. * A Drupal form state array as a reference.
  38. * @param string $arg_controller_class_name
  39. * The controller class name.
  40. *
  41. * @return array
  42. * A Drupal form array.
  43. */
  44. static public function createForm($form, &$form_state, $arg_controller_class_name) {
  45. // $controller_class_name = is_array($vargs) ? array_shift($vargs):$vargs;
  46. $controller_class_name = $arg_controller_class_name;
  47. // @todo handle when called with $vargs array,
  48. // not just single controller class name string.
  49. $method = self::BUILDER;
  50. // $args = is_array($vargs) ? $vargs : array($vargs);
  51. // $args = array($form, &$form_state,$vargs);
  52. $args = array($form, &$form_state); // @todo extend ?
  53. if (class_exists($controller_class_name)) {
  54. $controller = new $controller_class_name(Common::MODULE);
  55. return call_user_func_array(array($controller, $method), $args);
  56. // ISSUE: Notice: Undefined index:
  57. // \Drupal\ooe\Form\AbstractFormController::createForm
  58. // in drupal_retrieve_form()
  59. // (line 764 of /home/webel02/public_html/drupal7/includes/form.inc).
  60. //
  61. // Cause is clear:
  62. //
  63. // drupal_retrieve_form(): 764: if (!function_exists($form_id)) {
  64. //
  65. // http://au1.php.net/function_exists
  66. // http://au1.php.net/manual/en/function.method-exists.php
  67. //
  68. // php > class Test { static public function foo($arg) {echo $arg;} }
  69. // php > echo function_exists('Test::foo');
  70. // php > if (function_exists('Test::foo')) echo "exists";
  71. // php > if (method_exists('Test','foo')) echo "exists";
  72. // exists
  73. }
  74. throw new \Exception('Invalid FormController');
  75. }
  76. /**
  77. * Constructor.
  78. *
  79. * @param string $module
  80. * The module machine name.
  81. */
  82. public function __construct($module) {
  83. // IMPORTANT: Access level to
  84. // Drupal\ooe\Form\AbstractFormController::__construct()
  85. // must be public (as in class Drupal\ooe\Module\DefaultModuleHelper)
  86. parent::__construct($module);
  87. }
  88. /**
  89. * Utility method for accessing a named value from the form state.
  90. *
  91. * @param array $form_state
  92. * The Drupal form state array.
  93. * @param string $name
  94. * The named value to fetch.
  95. *
  96. * @return string
  97. * The matching value.
  98. */
  99. static protected function getFormValue(array $form_state, $name) {
  100. return $form_state['values'][$name];
  101. // @return string|array// @todo?
  102. }
  103. /**
  104. * Add a validation handler to a form.
  105. *
  106. * Should be called in the form builder implementation of a subclass.
  107. *
  108. * @param array $form
  109. * A Drupal form array.
  110. * @param string $validatorMethodName
  111. * The name of a _static_ validator method (only) with signature
  112. * ($form, &$form_state); DO NOT include the class name !
  113. *
  114. * @throws \Exception
  115. */
  116. protected function addValidateHandler(&$form, $validatorMethodName) {
  117. // dpm($validatorMethodName,
  118. // 'AbstractFormController::addValidateHandler:$validatorMethodName');
  119. // DEBUG
  120. if (empty($validatorMethodName) || !is_string($validatorMethodName)) {
  121. throw new \Exception('$validatorMethodName must be a non empty string !');
  122. }
  123. $class = get_class($this);
  124. if (!method_exists($class, $validatorMethodName)) {
  125. throw new \Exception('$validatorMethodName(' . $validatorMethodName . ') is not a valid method of class(' . $class . ') !');
  126. }
  127. $form['#validate'][] = "\\$class::$validatorMethodName";
  128. // Above safe: if $form['#validate'] does not exist yet it will be created.
  129. }
  130. /**
  131. * Add a submit handler to a form.
  132. *
  133. * Should be called in the form builder implementation of a subclass.
  134. *
  135. * @param array $form
  136. * A Drupal form array.
  137. * @param string $submitMethodName
  138. * The name of a _static_ validator method (only) with
  139. * signature ($form, &$form_state); DO NOT include the class name !
  140. *
  141. * @throws \Exception
  142. */
  143. protected function addSubmitHandler(&$form, $submitMethodName) {
  144. if (empty($submitMethodName) || !is_string($submitMethodName)) {
  145. throw new \Exception('$submitMethodName must be a non empty string !');
  146. }
  147. $class = get_class($this);
  148. if (!method_exists($class, $submitMethodName)) {
  149. throw new \Exception('$submitMethodName(' . $submitMethodName . ') is not a valid method of class(' . $class . ') !');
  150. }
  151. $form['#submit'][] = "\\$class::$submitMethodName";
  152. // Above is safe: if $form['#submit'] does not exist yet it will be created.
  153. }
  154. }
  155. /*
  156. static protected function newThis() {
  157. //http://stackoverflow.com/questions/5197300/new-self-vs-new-static
  158. //What is the difference between new self and new static?
  159. //self refers to the same class whose method
  160. // the new operation takes place in.
  161. //static in PHP 5.3's late static bindings refers to
  162. // whatever class in the hierarchy which you call the method on.
  163. return new static();//PHP5.3
  164. //FAILS: Fatal error: Cannot instantiate abstract class
  165. // Drupal\ooe\Form\AbstractFormController
  166. //Because by definition of the fixed 'page arguments'
  167. //it is calling on an AbstractFormController !
  168. }
  169. */

Classes

Namesort descending Description
AbstractFormController Adapted from Chris Skene's page_controller (@link PageController)