Projet

Général

Profil

Révision 08475715

Ajouté par Assos Assos il y a plus de 7 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/field_permissions/field_permissions.admin.inc
8 8
/**
9 9
 * Obtain the list of field permissions.
10 10
 *
11
 * @param $field_label
11
 * @param string $field_label
12 12
 *   The human readable name of the field to use when constructing permission
13 13
 *   names. Usually this will be derived from one or more of the field instance
14 14
 *   labels.
15 15
 */
16 16
function field_permissions_list($field_label = '') {
17
  return array(
17
  $permissions = array(
18 18
    'create' => array(
19 19
      'label' => t('Create field'),
20 20
      'title' => t('Create own value for field %field', array('%field' => $field_label)),
......
36 36
      'title' => t("View anyone's value for field %field", array('%field' => $field_label)),
37 37
    ),
38 38
  );
39

  
40
  drupal_alter('field_permissions_list', $permissions, $field_label);
41

  
42
  return $permissions;
39 43
}
40 44

  
41 45
/**
42 46
 * Returns field permissions in a format suitable for use in hook_permission().
43 47
 *
44
 * @param $field
48
 * @param array $field
45 49
 *   The field to return permissions for.
46
 * @param $label
50
 * @param mixed $label
47 51
 *   (optional) The human readable name of the field to use when constructing
48 52
 *   permission names; for example, this might be the label of one of the
49 53
 *   corresponding field instances. If not provided, an appropriate label will
50 54
 *   be automatically derived from all the field's instances.
51 55
 *
52
 * @return
56
 * @return array
53 57
 *   An array of permission information, suitable for use in hook_permission().
54 58
 */
55 59
function field_permissions_list_field_permissions($field, $label = NULL) {
56
  $description = '';
57

  
58
  // If there is no preferred label, construct one from all the instance
59
  // labels.
60 60
  if (!isset($label)) {
61
    $labels = array();
62
    foreach ($field['bundles'] as $entity_type => $bundles) {
63
      foreach ($bundles as $bundle_name) {
64
        $instance = field_info_instance($entity_type, $field['field_name'], $bundle_name);
65
        $labels[] = $instance['label'];
61
    $label = $field['field_name'];
62
  }
63
  $instances = array();
64
  $description = '';
65
  foreach ($field['bundles'] as $entity_type => $bundles) {
66
    foreach ($bundles as $bundle_name) {
67
      $instance = field_info_instance($entity_type, $field['field_name'], $bundle_name);
68
      $entity = entity_get_info($entity_type);
69
      if (!isset($entity['bundles'][$bundle_name])) {
70
        continue;
66 71
      }
72
      $instance_desc_tokens = array(
73
        $entity['label'],
74
        $entity['bundles'][$bundle_name]['label'],
75
        $instance['label'],
76
      );
77
      $instances[] = '"' . implode(':', $instance_desc_tokens) . '"';
67 78
    }
68
    // If all the instances have the same label, just use that. Otherwise, use
69
    // the field name (with the full list of instance labels as the permission
70
    // description).
71
    $labels = array_unique($labels);
72
    if (count($labels) == 1) {
73
      $label = array_shift($labels);
74
    }
75
    else {
76
      $label = $field['field_name'];
77
      $description = t('This field appears as: %instances', array('%instances' => implode(', ', $labels)));
78
    }
79
    $description = t('This field appears as: %instances.', array('%instances' => implode(', ', $instances)));
79 80
  }
80 81

  
81 82
  $permissions = array();
......
91 92
}
92 93

  
93 94
/**
94
 * Implementation of hook_permission().
95
 * Implements hook_permission().
95 96
 */
96 97
function _field_permissions_permission() {
97 98
  $perms = array(
......
145 146
    '#type' => 'container',
146 147
    '#states' => array(
147 148
      'visible' => array(
148
        // We must cast this to a string until http://drupal.org/node/879580 is
149
        // fixed.
150
        ':input[name="field[field_permissions][type]"]' => array('value' => (string) FIELD_PERMISSIONS_CUSTOM),
149
        ':input[name="field[field_permissions][type]"]' => array('value' => FIELD_PERMISSIONS_CUSTOM),
151 150
      ),
152 151
    ),
153 152
    // Custom styling for the permissions matrix on the field settings page.
......
156 155
    ),
157 156
  );
158 157

  
158
  $form['field']['field_permissions']['permission_warning'] = array(
159
    '#type' => 'item',
160
    '#markup' => '<div class="messages error"><b>'
161
    . t('Field permissions error')
162
    . '</b><br />'
163
    . t('If you are seeing this message and are not seeing a table of permissions, something is wrong! See !link for more information.', array(
164
      '!link' => l(t('the Field Permission documentation'), 'https://www.drupal.org/node/2802067#known-issues', array(
165
        'attributes' => array('target' => '_blank'),
166
      )),
167
    )) . '</div>',
168
    '#states' => array(
169
      'visible' => array(
170
        ':input[name="field[field_permissions][type]"]' => array('value' => FIELD_PERMISSIONS_CUSTOM),
171
      ),
172
    ),
173
  );
174

  
159 175
  // Add the field permissions matrix itself. Wait until the #pre_render stage
160 176
  // to move it to the above container, to avoid having the permissions data
161 177
  // saved as part of the field record.
......
183 199
 * to actually be saved. For an example submit handler, see
184 200
 * _field_permissions_field_settings_form_submit().
185 201
 *
186
 * @param $field
202
 * @param array $field
187 203
 *   The field whose permissions will be displayed in the matrix.
188
 * @param $instance
204
 * @param array $instance
189 205
 *   The field instance for which the permissions will be displayed. Although
190 206
 *   the permissions are per-field rather than per-instance, the instance label
191 207
 *   will be used to display an appropriate human-readable name for each
192 208
 *   permission.
193 209
 *
194
 * @return
210
 * @return array
195 211
 *   A form array defining the permissions matrix.
196 212
 *
197 213
 * @see user_admin_permissions()
......
327 343
 * Menu callback; Field permissions overview.
328 344
 */
329 345
function field_permissions_overview() {
330
  drupal_add_css(drupal_get_path('module', 'field_permissions') .'/field_permissions.admin.css');
346
  drupal_add_css(drupal_get_path('module', 'field_permissions') . '/field_permissions.admin.css');
331 347

  
332
  $headers = array(t('Field name'), t('Field type'), t('Entity type'), t('Used in'));
348
  $headers = array(
349
    t('Field name'),
350
    t('Field type'),
351
    t('Entity type'),
352
    t('Used in'),
353
  );
333 354
  foreach (field_permissions_list() as $permission_type => $permission_info) {
334 355
    $headers[] = array('data' => $permission_info['label'], 'class' => 'field-permissions-header');
335 356
  }
336 357
  $destination = drupal_get_destination();
337 358

  
338
  // Load list of field instances, types and bundles in the system.
339
  $instances = field_info_instances();
359
  // Load list of fields, field types and bundles in the system.
340 360
  $field_types = field_info_field_types();
341
  $bundles = field_info_bundles();
361
  $bundles_info = field_info_bundles();
342 362

  
343 363
  // Retrieve the permissions for each role.
344 364
  $role_permissions = user_role_permissions(user_roles());
345 365

  
346 366
  // Based on field_ui_fields_list() in field_ui.admin.inc.
347 367
  $rows = array();
348
  foreach ($instances as $obj_type => $type_bundles) {
349
    foreach ($type_bundles as $bundle => $bundle_instances) {
350
      foreach ($bundle_instances as $field_name => $instance) {
368
  foreach (field_info_fields() as $field_name => $field) {
369
    foreach ($field['bundles'] as $entity_type => $bundles) {
370
      foreach ($bundles as $bundle) {
371
        // Some fields might belong to bundles that are disabled (which are not
372
        // returned by field_info_bundles()).
373
        // @see https://www.drupal.org/node/1351506
374
        if (!isset($bundles_info[$entity_type][$bundle])) {
375
          continue;
376
        }
351 377
        // Each field will have a row in the table.
352
        $field = field_info_field($field_name);
353
        $admin_path = _field_ui_bundle_admin_path($obj_type, $bundle);
378
        if (module_exists('field_ui')) {
379
          $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
380
          $field_admin_path = l($bundles_info[$entity_type][$bundle]['label'], $admin_path . '/fields/' . $field_name, array(
381
            'query' => $destination,
382
            'fragment' => 'edit-field-field-permissions-type',
383
          ));
384
        }
385
        else {
386
          $field_admin_path = $bundles_info[$entity_type][$bundle]['label'];
387
        }
354 388
        $rows[$field_name]['data'][0] = $field['locked'] ? t('@field_name (Locked)', array('@field_name' => $field_name)) : $field_name;
355
        $rows[$field_name]['data'][1] = t($field_types[$field['type']]['label']);
356
        $rows[$field_name]['data'][2] = $obj_type;
357
        $rows[$field_name]['data'][3][] = l($bundles[$obj_type][$bundle]['label'], $admin_path . '/fields/'. $field_name, array(
358
          'query' => $destination,
359
          'fragment' => 'edit-field-field-permissions-type',
360
        ));
389
        $rows[$field_name]['data'][1] = $field_types[$field['type']]['label'];
390
        $rows[$field_name]['data'][2] = $entity_type;
391
        $rows[$field_name]['data'][3][] = $field_admin_path;
361 392
        $rows[$field_name]['class'] = $field['locked'] ? array('menu-disabled') : array('');
362 393

  
363 394
        // Append field permissions information to the report.
......
381 412
            $all_users_have_access = isset($role_permissions[DRUPAL_ANONYMOUS_RID][$permission]) && isset($role_permissions[DRUPAL_AUTHENTICATED_RID][$permission]);
382 413
            $status_class = $all_users_have_access ? 'field-permissions-status-on' : 'field-permissions-status-off';
383 414
            $title = $all_users_have_access ? t('All users have this permission') : t('Not all users have this permission');
384
            $data = l('', 'admin/people/permissions', array(
415
            $data = l(NULL, 'admin/people/permissions', array(
385 416
              'attributes' => array(
386 417
                'class' => array('field-permissions-status', $status_class),
387 418
                'title' => $title,
......
416 447

  
417 448
    // Allow external modules alter the table headers and rows.
418 449
    foreach (module_implements('field_permissions_overview_alter') as $module) {
419
      $function = $module .'_field_permissions_overview_alter';
450
      $function = $module . '_field_permissions_overview_alter';
420 451
      $function($headers, $rows);
421 452
    }
422 453

  

Formats disponibles : Unified diff