You are here

function _botcha_get_botcha_placement in BOTCHA Spam Prevention 7.2

Same name and namespace in other branches
  1. 6 botcha.inc \_botcha_get_botcha_placement()
  2. 6.2 botcha.inc \_botcha_get_botcha_placement()
  3. 6.3 botcha.module \_botcha_get_botcha_placement()
  4. 7 botcha.inc \_botcha_get_botcha_placement()
  5. 7.3 botcha.module \_botcha_get_botcha_placement()

Helper function to get placement information for a given form_id.

for more info about the fields 'path', 'key' and 'weight'.

Parameters

$form_id the form_id to get the placement information for.:

$form if a form corresponding to the given form_id, if there: is no placement info for the given form_id, this form is examined to guess the placement.

Return value

placement info array

See also

_botcha_insert_botcha_element()

1 call to _botcha_get_botcha_placement()
BotchaFormAbstract::addAdminLinks in controller/botcha_form.controller.inc

File

./botcha.inc, line 136
General BOTCHA functionality and helper functions.

Code

function _botcha_get_botcha_placement($form_id, $form) {

  // Get BOTCHA placement map from cache. Two levels of cache:
  // static variable in this function and storage in the variables table.
  static $placement_map = NULL;

  // Try first level cache.
  if ($placement_map === NULL) {

    // If first level cache missed: try second level cache.
    $placement_map = variable_get('botcha_placement_map_cache', NULL);
    if ($placement_map === NULL) {

      // If second level cache missed: start from a fresh placement map.
      $placement_map = array();

      // Prefill with some hard coded default entries.
      // The comment form can have a 'Preview' button, or both a 'Submit' and 'Preview' button,
      // which is tricky for automatic placement detection. Luckily, Drupal core sets their
      // weight (19 and 20), so we just have to specify a slightly smaller weight.
      $placement_map['comment_form'] = array(
        'path' => array(),
        'key' => NULL,
        'weight' => 18.9,
      );

      //FIXME: port over from CAPTCHA D7:

      // Additional note: the node forms also have the posibility to only show a 'Preview' button.
      // However, the 'Submit' button is always present, but is just not rendered ('#access' = FALSE)
      // in those cases. The the automatic button detection should be sufficient for node forms.
      // $placement_map['user_login'] = array('path' => array(), 'key' => NULL, 'weight' => 1.9);
      // TODO: also make the placement 'overridable' from the admin UI?
      // If second level cache missed: initialize the placement map
      // and let other modules hook into this with the hook_botcha_placement_map hook.
      // By default however, probably all Drupal core forms are already correctly
      // handled with the best effort guess based on the 'actions' element (see below).
      //      $placement_map = module_invoke_all('botcha_placement_map');
    }
  }

  // Query the placement map.
  if (array_key_exists($form_id, $placement_map)) {
    $placement = $placement_map[$form_id];
  }
  else {
    $buttons = _botcha_search_buttons($form);
    if (count($buttons)) {

      // Pick first button.
      // TODO: make this more sophisticated? Use cases needed.
      $placement = $buttons[0];
    }
    else {

      // Use NULL when no buttons were found.
      $placement = NULL;
    }

    // Store calculated placement in caches.
    $placement_map[$form_id] = $placement;
    variable_set('botcha_placement_map_cache', $placement_map);
  }
  return $placement;
}