You are here

function coder_upgrade_upgrade_call_drupal_add_js_alter in Coder 7.2

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

Implements hook_upgrade_call_drupal_add_js_alter().

File

coder_upgrade/conversions/call.inc, line 719
Provides conversion routines applied to function calls.

Code

function coder_upgrade_upgrade_call_drupal_add_js_alter(&$node, &$reader) {

  // DONE (UPDATED)
  // Create helper objects.
  $editor = PGPEditor::getInstance();

  // Get the function call object.
  $item =& $node->data;

  // Process function call.
  $name =& $item->name;

  /*
   * With
   * drupal_add_js('misc/collapse.js', 'core', 'header', FALSE, TRUE, TRUE);
   * we will output
   * drupal_add_js('misc/collapse.js', array('type' => 'file', 'weight' => JS_LIBRARY));
   * which is correct, although the function will also accept
   * drupal_add_js('misc/collapse.js', array('weight' => JS_LIBRARY));
   * The example begs the question why someone would have included all
   * the default parameters.
   *
   * A type of 'core', 'module' or 'theme' all convert to 'file' which is
   * the new default. The weight parameter then corresponds to the old type.
   */
  cdp($item->parameters
    ->print_r());
  $count = $item->parameters
    ->count();
  if ($count == 1) {
    return;
  }
  if ($count == 2) {
    $type = trim($item
      ->printParameter(1), "'\"");
    if ($type == 'module') {

      // if (in_array($type, array('module', ''))) {
      $item
        ->deleteParameter(1);
      return;
    }
    elseif (in_array($type, array(
      'core',
      'theme',
    ))) {

      // Add a default value for the scope parameter (removed below).
      $editor
        ->insertParameter($item, 2, "'header'");
    }
  }

  // Insert a new weight parameter.
  $type = trim($item
    ->printParameter(1), "'\"");
  $weight = coder_upgrade_js_weight($type);
  $editor
    ->insertParameter($item, 3, "{$weight}");

  // Arrayitize the parameters.
  $keys = array(
    'type',
    'scope',
    'weight',
    'defer',
    'cache',
    'preprocess',
  );
  $defaults = array(
    "'module'",
    "'header'",
    'JS_DEFAULT',
    'FALSE',
    'TRUE',
    'TRUE',
  );
  $string = $editor
    ->arrayitize($item, 1, $keys, $defaults);
  $string = preg_replace('@[\'"](core|theme)[\'"]@', "'file'", $string);

  // Could be deleted.
  if ($string != 'array()') {
    $temp = $editor
      ->expressionToStatement($string);
    $temp
      ->getElement(0)->multiline = 0;
    cdp($temp
      ->print_r());
    $item
      ->setParameter(1, $temp);
  }
}