You are here

function field_permissions_overview in Field Permissions 7

Same name and namespace in other branches
  1. 6 includes/admin.inc \field_permissions_overview()

Menu callback; Field permissions overview.

1 string reference to 'field_permissions_overview'
field_permissions_menu in ./field_permissions.module
Implements hook_menu().

File

./field_permissions.admin.inc, line 345
Administrative interface for the Field Permissions module.

Code

function field_permissions_overview() {
  drupal_add_css(drupal_get_path('module', 'field_permissions') . '/field_permissions.admin.css');
  $headers = array(
    t('Field name'),
    t('Field type'),
    t('Entity type'),
    t('Used in'),
  );
  foreach (field_permissions_list() as $permission_type => $permission_info) {
    $headers[] = array(
      'data' => $permission_info['label'],
      'class' => 'field-permissions-header',
    );
  }
  $destination = drupal_get_destination();

  // Load list of fields, field types and bundles in the system.
  $field_types = field_info_field_types();
  $bundles_info = field_info_bundles();

  // Retrieve the permissions for each role.
  $role_permissions = user_role_permissions(user_roles());

  // Based on field_ui_fields_list() in field_ui.admin.inc.
  $rows = array();
  foreach (field_info_fields() as $field_name => $field) {
    foreach ($field['bundles'] as $entity_type => $bundles) {
      foreach ($bundles as $bundle) {

        // Some fields might belong to bundles that are disabled (which are not
        // returned by field_info_bundles()).
        // @see https://www.drupal.org/node/1351506
        if (!isset($bundles_info[$entity_type][$bundle])) {
          continue;
        }

        // Each field will have a row in the table.
        if (module_exists('field_ui')) {
          $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
          $field_admin_path = l($bundles_info[$entity_type][$bundle]['label'], $admin_path . '/fields/' . $field_name, array(
            'query' => $destination,
            'fragment' => 'edit-field-field-permissions-type',
          ));
        }
        else {
          $field_admin_path = $bundles_info[$entity_type][$bundle]['label'];
        }
        $rows[$field_name]['data'][0] = $field['locked'] ? t('@field_name (Locked)', array(
          '@field_name' => $field_name,
        )) : $field_name;
        $rows[$field_name]['data'][1] = $field_types[$field['type']]['label'];
        $rows[$field_name]['data'][2] = $entity_type;
        $rows[$field_name]['data'][3][] = $field_admin_path;
        $rows[$field_name]['class'] = $field['locked'] ? array(
          'menu-disabled',
        ) : array(
          '',
        );

        // Append field permissions information to the report.
        $type = isset($field['field_permissions']['type']) ? $field['field_permissions']['type'] : FIELD_PERMISSIONS_PUBLIC;
        foreach (array_keys(field_permissions_list_field_permissions($field)) as $index => $permission) {

          // Put together the data value for the cell.
          $data = '';
          $full_colspan = FALSE;
          if ($type == FIELD_PERMISSIONS_PUBLIC) {
            $data = t('Public field (author and administrators can edit, everyone can view)');
            $full_colspan = TRUE;
          }
          elseif ($type == FIELD_PERMISSIONS_PRIVATE) {
            $data = t('Private field (only author and administrators can edit and view)');
            $full_colspan = TRUE;
          }
          else {

            // This is a field with custom permissions. Link the field to the
            // appropriate row of the permissions page, and theme it based on
            // whether all users have access.
            $all_users_have_access = isset($role_permissions[DRUPAL_ANONYMOUS_RID][$permission]) && isset($role_permissions[DRUPAL_AUTHENTICATED_RID][$permission]);
            $status_class = $all_users_have_access ? 'field-permissions-status-on' : 'field-permissions-status-off';
            $title = $all_users_have_access ? t('All users have this permission') : t('Not all users have this permission');
            $data = l(NULL, 'admin/people/permissions', array(
              'attributes' => array(
                'class' => array(
                  'field-permissions-status',
                  $status_class,
                ),
                'title' => $title,
              ),
              'query' => $destination,
              'fragment' => drupal_html_class("edit {$permission}"),
            ));
          }

          // Construct the cell.
          $rows[$field_name]['data'][4 + $index] = array(
            'data' => $data,
            'class' => array(
              'field-permissions-cell',
            ),
          );
          if ($full_colspan) {
            $rows[$field_name]['data'][4 + $index]['colspan'] = 5;
            break;
          }
        }
      }
    }
  }
  foreach ($rows as $field_name => $cell) {
    $rows[$field_name]['data'][3] = implode(', ', $cell['data'][3]);
  }
  if (empty($rows)) {
    $output = t('No fields have been defined for any content type yet.');
  }
  else {

    // Sort rows by field name.
    ksort($rows);

    // Allow external modules alter the table headers and rows.
    foreach (module_implements('field_permissions_overview_alter') as $module) {
      $function = $module . '_field_permissions_overview_alter';
      $function($headers, $rows);
    }
    $output = theme('table', array(
      'header' => $headers,
      'rows' => $rows,
    ));
  }
  return $output;
}