You are here

function coder_upgrade_op_to_hook in Coder 7.2

Same name in this branch
  1. 7.2 coder_upgrade/conversions/other.inc \coder_upgrade_op_to_hook()
  2. 7.2 coder_upgrade/conversions/tool.inc \coder_upgrade_op_to_hook()
Same name and namespace in other branches
  1. 7 coder_upgrade/conversions/other.inc \coder_upgrade_op_to_hook()
  2. 7 coder_upgrade/conversions/tool.inc \coder_upgrade_op_to_hook()

Creates hook_$op function from the case (or if) block of an $op-style hook.

Parameters

PGPNode $node: A node object containing a PGPClass (or function) item.

PGPNode $case_node: A node object containing a PGPCase item.

string $hook: A string of the new function name.

array $parameters: An array of function parameters.

10 calls to coder_upgrade_op_to_hook()
coder_upgrade_callback_block in coder_upgrade/conversions/other.inc
Updates hook_block().
coder_upgrade_callback_block in coder_upgrade/conversions/function.inc
Updates hook_block().
coder_upgrade_callback_comment in coder_upgrade/conversions/other.inc
Updates hook_comment().
coder_upgrade_callback_comment in coder_upgrade/conversions/function.inc
Updates hook_comment().
coder_upgrade_callback_nodeapi in coder_upgrade/conversions/other.inc
Updates hook_nodeapi().

... See full list

File

coder_upgrade/conversions/tool.inc, line 202
Provides tools to assist with conversion routines.

Code

function coder_upgrade_op_to_hook($node, $case_node, $hook, $parameters) {

  /*
   * Copy the case body to the new hook function.
   * Insert before (or after) the $item function.
   *
   * When case body is empty (e.g. insert, update), then use next reference
   * until a non-empty body is found.
   *
   * TODO
   * Add the new function to the list of functions.
   * This is useful when we may need to check for the existence of a function
   * on another upgrade.
   * Example: hook_link() becomes part of hook_node_view()
   * or hook_comment_view() based on $type parameter. Also hook_link_alter()
   * code goes in hook_node_view_alter() or hook_comment_view_alter().
   * See http://drupal.org/node/224333#node_links.
   */
  global $_coder_upgrade_module_name;
  $case =& $case_node->data;

  // Set values for the new hook function.
  $comment = array(
    'type' => T_DOC_COMMENT,
    'value' => "/**\n * Implements hook{$hook}().\n */",
  );
  $name = $_coder_upgrade_module_name . $hook;

  // Create the new hook function.
  $function = new PGPClass($name);
  $function->comment = $comment;
  $function->type = T_FUNCTION;
  $function->parameters = new PGPList();

  // Use the editor to set the function parameters.
  $editor = PGPEditor::getInstance();
  $editor
    ->setParameters($function, $parameters);

  // Copy the case (or if) block as the body of the function.
  $function->body = $case->body;
  if ($function->body
    ->isEmpty()) {

    // Find the next case with a body.
    $case_node2 = $case_node->next;
    while ($case_node2->next != NULL) {
      $case2 = $case_node2->data;
      $body2 = $case2->body;
      if (!$body2
        ->isEmpty()) {
        $function->body = $case2->body;
        break;
      }
      $case_node2 = $case_node2->next;
    }
  }

  // Remove the break statement from a case block.
  if ($break = $function->body
    ->find(T_BREAK, 'reverse', TRUE)) {
    cdp("return statement found in hook");
    $function->body
      ->delete($break);
  }

  // Remove any trailing blank lines (after break) that are included in body.
  $last = $function->body
    ->last();
  if (is_array($last->data) && $last->data['type'] == T_WHITESPACE) {
    cdp("YAHOO: found whitespace statement in hook_nodeapi");
    $function->body
      ->delete($last);
  }

  // Get the statement list the function node is part of.
  $container =& $node->container;

  // Insert the new function before the old function.
  $container
    ->insertBefore($node, $function, 'function');

  // Insert a blank line.
  $whitespace = array(
    'type' => T_WHITESPACE,
    'value' => 1,
  );
  $container
    ->insertBefore($node, $whitespace, 'whitespace');
}