You are here

function _captcha_get_captcha_placement in CAPTCHA 6.2

Same name and namespace in other branches
  1. 8 captcha.inc \_captcha_get_captcha_placement()
  2. 7 captcha.inc \_captcha_get_captcha_placement()

Helper function to get placement information for a given form_id.

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 _captcha_insert_captcha_element() for more info about the fields 'path', 'key' and 'weight'.

1 call to _captcha_get_captcha_placement()
captcha_form_alter in ./captcha.module
Implementation of hook_form_alter().

File

./captcha.inc, line 232
General CAPTCHA functionality and helper functions.

Code

function _captcha_get_captcha_placement($form_id, $form) {

  // Get CAPTCHA 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('captcha_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,
      );

      // 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?
    }
  }

  // Query the placement map.
  if (array_key_exists($form_id, $placement_map)) {
    $placement = $placement_map[$form_id];
  }
  else {
    $buttons = _captcha_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('captcha_placement_map_cache', $placement_map);
  }
  return $placement;
}