function field_permissions_permissions_matrix in Field Permissions 7
Returns a field permissions matrix that can be inserted into a form.
The matrix's display is based on that of Drupal's default permissions page.
Note that this matrix must be accompanied by an appropriate submit handler (attached to the top level of the form) in order for the permissions in it to actually be saved. For an example submit handler, see _field_permissions_field_settings_form_submit().
Parameters
array $field: The field whose permissions will be displayed in the matrix.
array $instance: The field instance for which the permissions will be displayed. Although the permissions are per-field rather than per-instance, the instance label will be used to display an appropriate human-readable name for each permission.
Return value
array A form array defining the permissions matrix.
See also
_field_permissions_field_settings_form_submit()
1 call to field_permissions_permissions_matrix()
- _field_permissions_field_settings_form_alter in ./
field_permissions.admin.inc - Alter the field settings form.
File
- ./
field_permissions.admin.inc, line 216 - Administrative interface for the Field Permissions module.
Code
function field_permissions_permissions_matrix($field, $instance) {
// This function primarily contains a simplified version of the code from
// user_admin_permissions().
$form['#theme'] = 'user_admin_permissions';
$options = array();
$status = array();
// Retrieve all role names for use in the submit handler.
$role_names = user_roles();
$form['role_names'] = array(
'#type' => 'value',
'#value' => $role_names,
);
// Retrieve the permissions for each role, and the field permissions we will
// be assigning here.
$role_permissions = user_role_permissions($role_names);
$field_permissions = field_permissions_list_field_permissions($field, $instance['label']);
// Determine if it is safe to reset the default values for this field's
// permissions. If this is a new field (never saved with field permission
// data before), or if it's an existing field that is not currently using
// custom permissions and doesn't have any previously-saved ones already in
// the database, then it will be safe to reset them.
$reset_permissions_defaults = FALSE;
if (!isset($field['field_permissions']['type'])) {
$reset_permissions_defaults = TRUE;
}
elseif ($field['field_permissions']['type'] != FIELD_PERMISSIONS_CUSTOM) {
$all_assigned_permissions = call_user_func_array('array_merge_recursive', $role_permissions);
$assigned_field_permissions = array_intersect_key($all_assigned_permissions, $field_permissions);
$reset_permissions_defaults = empty($assigned_field_permissions);
}
// Store this information on the form so that other modules can use it (for
// example, if they want to set default permissions for other roles besides
// the admin role which we use it for below).
$form['#field_permissions_are_new'] = $reset_permissions_defaults;
// Go through each field permission we will display.
foreach ($field_permissions as $permission => $info) {
// Display the name of the permission as a form item.
$form['permission'][$permission] = array(
'#type' => 'item',
'#markup' => $info['title'],
);
// Save it to be displayed as one of the role checkboxes.
$options[$permission] = '';
// If we are in a situation where we can reset the field permissions
// defaults, we do so by pre-checking the admin role's checkbox for this
// permission.
if ($reset_permissions_defaults) {
if (($admin_rid = variable_get('user_admin_role', 0)) && isset($role_names[$admin_rid])) {
$status[$admin_rid][] = $permission;
}
// For fields attached to users, we also pre-check the anonymous user's
// checkbox for the permission to create the field, since that is the
// most common way in which new user entities are created.
if ($instance['entity_type'] == 'user' && $permission == 'create ' . $field['field_name']) {
$status[DRUPAL_ANONYMOUS_RID][] = $permission;
}
}
else {
foreach ($role_names as $rid => $name) {
if (isset($role_permissions[$rid][$permission])) {
$status[$rid][] = $permission;
}
}
}
}
// Build the checkboxes for each role.
foreach ($role_names as $rid => $name) {
$form['checkboxes'][$rid] = array(
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => isset($status[$rid]) ? $status[$rid] : array(),
'#attributes' => array(
'class' => array(
'rid-' . $rid,
),
),
);
$form['role_names'][$rid] = array(
'#markup' => check_plain($name),
'#tree' => TRUE,
);
}
// Attach the default permissions page JavaScript.
$form['#attached']['js'][] = drupal_get_path('module', 'user') . '/user.permissions.js';
// Attach our custom JavaScript for the permission matrix.
$form['#attached']['js'][] = drupal_get_path('module', 'field_permissions') . '/field_permissions.admin.js';
return $form;
}