You are here

function coder_upgrade_callback_hook_info in Coder 7.2

Same name and namespace in other branches
  1. 7 coder_upgrade/conversions/function.inc \coder_upgrade_callback_hook_info()

Updates hook_hook_info() arrays.

hook_hook_info() is now called hook_trigger_info(), and its return value has been changed and simplified.

@todo Database table {trigger_assignments} - 'op' field was removed, and the 'hook' field now contains the full function name.

Parameters

PGPNode $node: The node of the statement containing the array object.

PGPArray $array2: The array object containing the array element ($current2).

PGPNode $current2: The node object of the array element.

string $hook: The hook name.

string $type: The type (key or value) of the array element.

string $key: The key of the array element (or the most recent key).

string $value: The value of the array element (or NULL if element is a key).

File

coder_upgrade/conversions/function.inc, line 1828
Provides conversion routines applied to functions (or hooks).

Code

function coder_upgrade_callback_hook_info($node, &$array2, &$current2, $hook, $type, $key, $value) {

  // DONE
  cdp("inside " . __FUNCTION__);
  if (!$current2 instanceof PGPNode) {
    clp("ERROR: current2 is not a PGPNode object in hook_{$hook}");
    return;
  }
  $editor = PGPEditor::getInstance();

  /*
   * Array levels
   * - L0 return array(
   * - L1   'node' => array(
   * - L2     'nodeapi' => array(
   * - L3       'presave' => array(
   * - L4         'runs when' => t('When either saving a new post or updating an existing post'),
   */

  // The keys of this array are the module (or content type) the hook pertains to.
  if ($type == 'key') {
    cdp("Found the module (or content type) key");
    if (!$current2->data
      ->isType(T_CONSTANT_ENCAPSED_STRING)) {
      clp("ERROR: key expression is not a string in hook_{$hook}");
      return;
    }

    // This is the module (or content type) the hook pertains to.
    // TODO Other prefixes may need to be modified.
    switch ($key) {
      case 'nodeapi':
        $prefix = 'node';
        break;
      case 'taxonomy':
        $prefix = 'taxonomy_term';
        break;
      default:
        $prefix = $key;
        break;
    }

    // Find the value element for this key.
    if (!$array2
      ->findNextValue($current2)) {
      clp("ERROR: did not find a value expression for the triggers key in hook_{$hook}");
      return;
    }
    if (!$current2->data
      ->isType(T_ARRAY)) {
      clp("ERROR: value expression is not a PGPArray object in hook_{$hook}");
      return;
    }

    /*
     * The triggers information element is an associative array doubly keyed by
     * module name with its value being an array of action information. The new
     * format combines the module name and action, eliminating one nested array.
     * Example:
     * 'node' => array(
     *   'nodeapi' => array(
     *     'presave' => array(
     *       'runs when' => t('When either saving a new post or updating an existing post'),
     *     ),
     *   ),
     * ),
     *
     * 'node' => array(
     *   'node_presave' => array(
     *     'label' => t('When either saving a new post or updating an existing post'),
     *   ),
     * ),
     */
    $triggers =& $current2->data
      ->getElement();
    $trigger = $triggers->values
      ->first();
    while ($trigger->next != NULL) {
      if ($trigger->type == 'key') {
        if (!$trigger->data
          ->isType(T_CONSTANT_ENCAPSED_STRING)) {
          clp("ERROR: trigger key expression is not a string in hook_{$hook}");
          return;
        }

        // This is the module (or content type) the hook pertains to.
        // Concatenate prefix with key.
        $suffix = trim($trigger->data
          ->toString(), "'\"");
        $new_key = "'{$prefix}_{$suffix}'";
        $trigger->data = $editor
          ->expressionToStatement($new_key);
      }
      elseif ($trigger->type == 'value') {
        if (!$trigger->data
          ->isType(T_ARRAY)) {
          clp("ERROR: trigger value expression is not a PGPArray object in hook_{$hook}");
          return;
        }

        // This is the array of trigger information items.
        // Currently, the only key is 'label' (formerly 'runs when').
        $information_items =& $trigger->data
          ->getElement();
        $information_items->multiline = 1;
        $information_items->preserve = 0;
        if (!$information_items
          ->changeKey('runs when', "'label'")) {
          clp("ERROR: could not change 'runs when' key expression in hook_{$hook}");
          clp($information_items
            ->toString());
          return;
        }
      }
      $trigger =& $trigger->next;
    }

    // Move up level 3 (triggers info) to level 2 (redundant module name) of the array.
    $array2->values = $triggers->values;

    // $array2->values = $array3->values;

    /*
     * Set array properties so the rewritten array:
     * - has a comma after all values
     * - is not indented one level too many
     */
    $array2->count = $triggers->count;
    $array2->commaCount = $triggers->count;
    $array2->multiline = 1;
    $array2->preserve = 0;
  }
}