OOE: special tweaks to Drupal7 core's form.inc required to support object-oriented form controllers

DEPRECATED: this software engineering content has been flagged as not for future use.
Keywords

News: Downloads now available: Drupal7 core patch required to support object-oriented form controllers; and OOE bundle with patched Drupal-7.-36

NEW: There is now a patch for Drupal 7.x-dev core as of 2015-04-04 (Drupal-7.36+) for the files includes/form.inc and includes/ajax.inc, making it easier for you experiment with the OOE = Object Oriented Examples = One Of Each tutorial module. Visit also: Download: a Drupal-7.x-dev patch to support the OO form controllers of the OOE tutorial module , and learn how to add your community support for inclusion of this modification into Drupal-7.37 !
NEW: Until the OOE patch for Drupal7 core is accepted, you may now also experiment with the OOE = Object Oriented Examples = One Of Each tutorial module easily using: Download: OOE bundle with adapted Drupal-7.36 and installation instructions.

The OOE module depends (as a "tip of the hat") on the experimental Page controller project.

It also currently requires a small and completely harmless tweak to the Drupal7 core files includes/form.inc and includes/ajax.inc, as described at:

- Drupal 7.25: form.inc 1471: form_execute_handlers($type, &$form, &$form_state): $function($form, $form_state); Does not work for static class method 'Drupal\mymodule\FormContoller::handler'

- drupal_retrieve_form() issues Undefined index: warning on line 764 when static class method used as $form_id: hinders use of object-oriented FormController

The reason this small tweak is needed is that Drupal7 (at least up to Drupal-7.36) currently assumes specification of non-OO .module file functions for the above, not static methods of form controller classes as used by OOE. It's very simple to simply edit the Drupal7 core includes/form.inc and includes/ajax.incfiles after these code examples.

The following code tweaks are now considered OBSOLETE, but still work, and are kept here just as a record and explanation. You should instead use the OOE Drupal7 Bundle or the OOE Drupal7 patch mentioned above.

ARCHIVAL: Instructions for tweaks to form.inc

In includes/form.inc find the following line, in Drupal7.30 it is line 791:

function drupal_retrieve_form($form_id, &$form_state) {
..
  if (!function_exists($form_id)) {

Replace it so that it reads like this (you might like to comment out the original as a record):

function drupal_retrieve_form($form_id, &$form_state) {
..
  // if (!function_exists($form_id)) { // ORIGINAL: fails for static class method as form id.
    $sep = '::';
    $is_static_method = strpos($form_id,$sep);
    $function_exists = false;
    if ($is_static_method) {
        $explode = explode($sep,$form_id);
        $class = $explode[0];
        $method = $explode[1];
        $function_exists = method_exists($class,$method);
    }
    else {
      $function_exists = function_exists($form_id);
    }
    if (!$function_exists) {

Also in includes/form.inc find this line (in Drupal 7.30 it's line 1531):

function form_execute_handlers($type, &$form, &$form_state) {
..
    else {
      $function($form, $form_state);

Replace it so that it reads like this (again you might like to comment it out as a record of the change):

      // $function($form, $form_state); // ORIGINAL: fails for static class method.
      $sep = '::';
      $is_static_method = strpos($function,$sep);
      if ($is_static_method) {
        call_user_func_array($function,array($form,$form_state));
      }
      else {
        $function($form, $form_state);
      }

The OOE object-oriented form controller examples will now run without any errors.


You may also optionally make the following change to includes/ajax.inc recommended by developer sammarks15 according to this patch.

In includes/ajax.inc find these lines in function ajax_form_callback():

if (!empty($callback) && function_exists($callback)) {
    $result = $callback($form, $form_state);

Replace them with:

  if (!empty($callback) && is_callable($callback)) {
    if (strpos($callback, '::')) {
      $result = call_user_func_array($callback, array($form, $form_state));
    } else {
      $result = $callback($form, $form_state);
    } 

You should now also be able to use static class methods in AJAX callbacks.

Visit also