You are here

function coder_upgrade_upgrade_call_theme_alter in Coder 7.2

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

Implements hook_upgrade_call_theme_alter().

File

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

Code

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

  // DONE
  global $_coder_upgrade_theme_registry;

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

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

  // Process function call.
  $name =& $item->name;
  $p0 = $item
    ->getParameter();
  if (!$p0
    ->isType(T_CONSTANT_ENCAPSED_STRING)) {

    // TODO Look for the assignment to the theme hook (a variable or expression).
    $item
      ->insertStatementBefore($editor
      ->commentToStatement('// TODO Please change this theme call to use an associative array for the $variables parameter.'));
    return;
  }

  // Process changes to specific themes.
  $theme = trim($item
    ->printParameter(), "'\"");

  // This change is not documented.
  // Width and height parameters have been added after path.
  if ($theme == 'image' || $theme == 'image_style') {

    // DONE
    $index = $theme == 'image' ? 2 : 3;
    if ($item
      ->parameterCount() > $index) {

      // Delete $getsize parameter.
      $item
        ->deleteParameter($index + 3);

      // Insert placeholders for width and height parameters.
      $editor
        ->insertParameter($item, $index, "''");
      $editor
        ->insertParameter($item, $index + 1, "''");
    }
  }

  // http://drupal.org/node/224333#theme_page
  if ($theme == 'page') {

    // DONE
    $item
      ->insertStatementBefore($editor
      ->commentToStatement('// TODO Please change this theme call as discussed at http://drupal.org/node/224333#theme_page.'));

    // Get the statement (i.e. node) this function call is part of.
    $parent =& $item->parent;

    // Comment out the statement.
    $text = $parent->data
      ->toString();
    $parent->data = $editor
      ->textToStatements('// ' . $text)
      ->getElement();
    return;
  }

  // http://drupal.org/node/224333##theme_pager
  // Limit parameter has been removed.
  if ($theme == 'pager') {

    // DONE
    $item
      ->deleteParameter(2);

    // Does nothing if no third parameter.
  }

  // http://drupal.org/node/224333#placeholder
  // theme('placeholder') has been replaced by drupal_placeholder().
  if ($theme == 'placeholder') {

    // DONE
    $item
      ->deleteParameter();
    $name['value'] = 'drupal_placeholder';
    return;
  }

  // http://drupal.org/node/224333#theme_username
  // theme('username') parameters have changed.
  // Note: this is automatically handled below.
  //  if ($theme == 'username') { // DONE
  //    $p1 = $item->printParameter(1);
  //    $p1 = "array('account' => $p1)";
  //    $editor->setParameter($item, 1, $p1);
  //    return;
  //  }

  /*
   * http://drupal.org/node/224333#theme_changes
   *
   * In module pre-processing, create a global variable to hold the array of
   * theme registrations for all core files (including disabled modules). Add
   * to this array the theme entries for the current module. Find the current
   * theme call in the global list to get the names of its parameters.
   * Array-itize the parameters.
   *
   * See drupal_common_theme() in common.inc and hook_theme in the core modules.
   *
   * See http://drupal.org/files/issues/theme-wip-572618-83.patch
   */
  $theme = trim($item
    ->printParameter(0), "'\"");
  if (!isset($_coder_upgrade_theme_registry[$theme])) {
    clp("ERROR: Theme entry for '{$theme}' not found in theme registry or hook_theme");
    $item
      ->insertStatementBefore($editor
      ->commentToStatement('// TODO Please change this theme call to use an associative array for the $variables parameter.'));
    return;
  }
  $hook = $_coder_upgrade_theme_registry[$theme];
  if (isset($hook['variables']) && $hook['variables']) {
    $count = $item
      ->parameterCount();

    // TODO Incorporate default value elimination into this.
    if ($count == 1) {

      // This theme call is missing some or all of the variables parameters.
      $item
        ->insertStatementBefore($editor
        ->commentToStatement('// TODO Please change this theme call to use an associative array for the $variables parameter.'));
      return;
    }
    elseif ($count - 1 > count($hook['variables'])) {

      // The number of parameters to this theme call exceeds the number of
      // variables parameters defined in hook_theme.
      clp("ERROR: Number of parameters in call to theme '{$theme}' exceeds the number of parameters found in theme registry or hook_theme");
      $item
        ->insertStatementBefore($editor
        ->commentToStatement('// TODO Please change this theme call to use an associative array for the $variables parameter.'));
      return;
    }
    $defaults = array_fill(0, $count - 1, "'XXX_YYY'");
    $string = $editor
      ->arrayitize($item, 1, array_keys($hook['variables']), $defaults);
    $temp = $editor
      ->expressionToStatement($string);
    $temp
      ->getElement(0)->multiline = 0;
    $item
      ->setParameter(1, $temp);
  }
}