You are here

function coder_upgrade_callback_perm in Coder 7.2

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

Updates hook_perm() arrays.

Permissions are required to have titles and descriptions.

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 2029
Provides conversion routines applied to functions (or hooks).

Code

function coder_upgrade_callback_perm($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();

  // The values of this array are the permission items.
  // TODO If someone used keys with the values, this would result in consecutive
  // keys in the array and likely fail.
  if ($type == 'value') {
    $current2->type = 'key';
    $permission = $current2->data
      ->toString();
    $permission2 = str_replace("'", "\\'", $permission);
    $string = "array('title' => t({$permission}), 'description' => t('TODO Add a description for {$permission2}'),)";

    // Create new array expression.
    $editor
      ->getReader()
      ->setPreserveArrayFormat();
    $expression = $editor
      ->expressionToStatement($string);
    $expression->multiline = 1;
    $expression->preserve = 0;
    $editor
      ->getReader()
      ->setPreserveArrayFormat(TRUE);

    // Insert new expression nodes.
    $new = $array2->values
      ->insertAfter($current2, $expression, 'value');
    $array2->values
      ->insertAfter($current2, '=>', 'assign');

    // Force the settings on the original array (which is usually inline).
    $array2->multiline = 1;
    $array2->preserve = 0;

    // Set $current2 to last item inserted above to avoid redundant looping.
    $current2 = $new;
  }
}