You are here

function patterns_prepare_action in Patterns 7

Same name and namespace in other branches
  1. 7.2 includes/core/common.inc \patterns_prepare_action()

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.

@TODO Doc.

Parameters

array $actions An array of actions. Each action is an associative: array with keys 'action' and 'data'.

array $actions_map:

Return value

array $results array containing the description of eventual errors

2 calls to patterns_prepare_action()
patterns_batch_action in includes/core/batch.inc
Executes a batch action.
patterns_php_action in includes/core/php.inc
Execute a single pattern action.

File

includes/core/common.inc, line 20
The common functions used by the Batch and PHP running modes.

Code

function patterns_prepare_action(&$action, &$data, $actions_map) {

  //function patterns_prepare_actions(&$actions, $actions_map) {
  $status = PATTERNS_SUCCESS;
  $message = '';
  if (empty($action)) {
    return patterns_results();
  }

  // Keep a list of which modules handle what tags.
  $tag_modules = patterns_tagmodules_get_index($data);
  $errors = array();

  /////////////////////////////////////////////

  // Prepare actions for validation/processing

  /////////////////////////////////////////////

  //  foreach ($actions as &$action) {
  //    // TODO: error checking.
  $result = patterns_invoke('prepare', $action, $data);

  //  }
  // @TODO Not sure if prepare and validate could be merged.
  //       Maybe they were required to be separate when we used to have subpatterns.
  //  foreach ($actions as &$action) {
  $key =& $action;
  $data =& $data;

  /////////////////////////////////////////////////////////

  // This is in case of included patterns. Not for now
  //    $action_location = patterns_locate_action ($data['tag'], $actions_map);
  //    $index = $action_location['key'];
  //    $pattern_title = $action_location['title'];
  //    $pattern_file = $action_location['file'];

  /////////////////////////////////////////////////////////

  // TODO: manage multiple pattern includes
  $pattern_info = reset($actions_map['patterns']);
  $pattern_title = $pattern_info['title'];
  if (!array_key_exists($data['tag'], $tag_modules)) {

    // TODO: Use $index when there are multiple patterns again.
    // $errors[] = t('Action #%num (%tag) in pattern %title: <%tag> is not a valid tag', array('%num' => $index + 1, '%tag' => $data['tag'], '%title' => $pattern_title));
    $errors[] = t('Pattern %title: <%tag> is not a valid tag', array(
      '%tag' => $data['tag'],
      '%title' => $pattern_title,
    ));
  }
  else {

    //////////////////////////////////////////////////

    // Validate tags with their appropriate components

    //////////////////////////////////////////////////
    $results = patterns_invoke('validate', $key, $data);
    if (!patterns_error_check_results($results)) {

      // TODO: Use $index when there are multiple patterns again.
      // $errors[] = t('Action #%num (%tag) in pattern %title: !msg', array('!msg' => $results['msg'], '%num' => $index + 1, '%tag' => $data['tag'], '%title' => $pattern_title));
      $errors[] = t('pattern %title: !msg', array(
        '!msg' => $results['msg'],
        '%tag' => $data['tag'],
        '%title' => $pattern_title,
      ));
    }
  }

  // }
  if (count($errors)) {
    $message = t('Errors encountered during pre-processing:') . '<br/>' . implode('<br/>', $errors);
    $status = PATTERNS_ERR;
  }
  return patterns_results($status, $message);
}