You are here

function _permissions_lock_cleanup_orphan_headers in Permissions Lock 7

Helper function to remove permission table headers without related permissions

1 call to _permissions_lock_cleanup_orphan_headers()
permissions_lock_form_alter in ./permissions_lock.module
Implements hook_form_alter().

File

./permissions_lock.module, line 161
Lock permissions on the permissions administration pages for certain roles

Code

function _permissions_lock_cleanup_orphan_headers(&$form) {
  if (!empty($form['permission'])) {
    $prev_numeric = FALSE;
    $num = 0;
    foreach ($form['permission'] as $permission => $value) {
      if (is_numeric($permission)) {
        if ($prev_numeric) {

          // 2 numeric items following each other. Remove the first one, as it's an orphan header
          unset($form['permission'][$num]);
        }
        $prev_numeric = TRUE;
        $num = $permission;
      }
      else {
        $prev_numeric = FALSE;

        // Unset $num, needed for last iteration check
        unset($num);
      }
    }

    // After last iteration in foreach, check if last row was numeric
    // If so, remove, it is an orphan
    if (isset($num)) {
      unset($form['permission'][$num]);
    }
  }
}