You are here

function patterns_invoke in Patterns 7

Same name and namespace in other branches
  1. 5 patterns.module \patterns_invoke()
  2. 6.2 patterns.module \patterns_invoke()
  3. 6 patterns.module \patterns_invoke()
  4. 7.2 patterns.module \patterns_invoke()

Execute hook_patterns with the given operation and return the results.

It also builds up, if necessary, the list of available action-tags and binds them to the correct module.

Parameters

string $hook: The hook to be called. One of {'prepare', 'validate', 'callbacks', 'build', 'params', 'cleanup'}.

string $action: The action to be executed. One of {'create', 'modify', 'delete'}.

array $data: (optional) The data supplied as an associative array. Defaults NULL.

mixed $form_id: (optional) The name of the form to be used. Defaults NULL.

array $extra: (optional) Extra parameters to pass to the function. Defaults NULL.

Return value

array $return An array packed by patterns_results().

3 calls to patterns_invoke()
patterns_implement_action in includes/core/common.inc
Setup and run an action.
patterns_prepare_action in includes/core/common.inc
Preparing and validating the action tags as they are written in the pattern file. Concretely, it invokes operations 'prepare', and 'validate' on the pattern component.
_patterns_scan_action in includes/parser/scan.inc
Helper function for scanning patterns.

File

./patterns.module, line 792

Code

function patterns_invoke($hook, $action, &$data = NULL, $form_id = NULL, &$extra = NULL) {
  $tag_modules = patterns_tagmodules_get_index($data);

  // TODO: check the parameters
  // Unsetting it while the module performs operations.
  $tag = $data['tag'];
  unset($data['tag']);
  if (isset($tag_modules[$tag])) {
    $module = $tag_modules[$tag]['module'];
    $func = $module . '_patterns_' . $hook;
    if (function_exists($func)) {
      if ($form_id) {

        // We are past hook_patterns_build().
        $return = $func($action, $form_id, $data, $extra);
      }
      else {
        $return = $func($action, $tag, $data, $extra);
      }
    }
    else {
      $msg = t('Function %func not found. Hook %hook skipped', array(
        '%func' => $func,
        '%hook' => $hook,
      ));
      $return = patterns_results(PATTERNS_SUCCESS, $msg);
    }
  }
  else {
    $msg = t('Required patterns tag %tag is not provided by any component.', array(
      '%tag' => $tag,
    ));
    $return = patterns_results(PATTERNS_ERR, $msg);
  }

  // Check if the output is correctly formatted, and eventually try to correct it.
  if (!_patterns_is_patterns_results($return)) {
    if ($return === TRUE) {
      $return = array(
        'status' => PATTERNS_SUCCESS,
      );
    }
    elseif ($return === FALSE) {
      $msg = t('Unspecified error occurred in %func.', array(
        '%func' => $func,
      ));
      $return = patterns_results(PATTERNS_ERR, $msg);
    }
    else {
      $msg = t('The return value of %func is not properly formatted.', array(
        '%func' => $func,
      ));
      $return = patterns_results(PATTERNS_ERR, $msg);
    }
  }
  $return['details']['action'] = $action;
  $return['details']['hook'] = $hook;
  $return['details']['function'] = $form_id;
  $data['tag'] = $tag;
  return $return;
}