API page: http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...

The documentation says returning false will not allow that operation. For example the following disables editing of the field:

function grom_field_access($op, $field, $entity_type, $entity, $account) {
  if ($op == 'view') {
    return true;
  }
  if ($field['field_name'] == 'field_it_notes') {
    return false;
  }
  return true;
} 

Is it possible to limit the field so its read only when editing of the field is requested?

Comments

grom358’s picture

Priority: Major » Minor

Not a bug. My code sample was incorrect

grom358’s picture

Title: Problem with hook_field_access » Read only fields with hook_field_access
Category: bug » feature
grom358’s picture

Issue summary: View changes

Fixed code sample

yched’s picture

Status: Active » Closed (works as designed)

I don't think I get this. You can specify access for the 'edit' $op.

grom358’s picture

Yeah but if you don't give it access for the 'edit' $op then it doesn't show at all. I would like it to show up read only. So the user can see the value of the field but can't change it.

grom358’s picture

Status: Closed (works as designed) » Active
yched’s picture

Title: Read only fields with hook_field_access » Allow read-only fields on edit forms
Version: 7.7 » 8.x-dev

Ah got it.
This is more complex than it sounds. This means either :
- displaying the field value in a disabled widget. Would require all widgets to support a 'disabled' state, which is currently not in the contract for 'being a widget',
- displaying the field value using a formatter, but then - which formatter should be used ? Would require some way of choosing the formatter.

larowlan’s picture

subs

deciphered’s picture

Just adding my 2c from discussion in IRC:

I agree with the disabled state approach, it gives the Widget developer control on how the field should act when disabled.

However, that doesn't look to be the way that grom358 is leaning, so in the case of a Formatter, there would need to be a Display mode for the form as there are many formatters that wouldn't make any sense in the back-end (such as a very large Image Style).

Ultimately, some in between would also be nice, have the default formatter for the back-end/form display act differently from the standard default formatter. An example would be an Image field, the default for use in the front-end is to render it as an Image, the default for the back-end could be just the files URI.

Cheers,
Deciphered.

joachim’s picture

Issue tags: +Needs usability review

My feeling is that a widget that's visibly disabled is much better UX than outputting a formatter.

Tagging for input from the usability team.

grom358’s picture

If I make the widgets support #disabled state, how to control when it should be disabled? The following is code as proof of concept:

function field_patch_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  foreach (field_info_instances($entity_type, $entity->type) as $field_name => $field) {
    if (!field_access('edit', $field, $entity_type, $entity) && field_access('view', $field, $entity_type, $entity)) {
      $form[$field_name][$langcode] = field_view_field($entity_type, $entity, $field_name, 'form', $langcode);
    }
  }
}

function field_patch_entity_info_alter(&$entity_info) {
  $entity_info['node']['view modes']['form'] = array(
    'label' => 'Form',
    'custom settings' => FALSE,
  );
}

With this its controlled through the field permissions. Then using modules like Field Permissions and Rules can setup making a field view only when editing the node.

yched’s picture

'disabled' widgets would work by having field_default_form() do the field_access() checks and specifying #disabled = true in the base $element provided to hook_field_widget_form()

grom358’s picture

Updated code. Works with field group module. Setting the form state wasn't needed for my test case but added it in case it may be needed elsewhere.

function field_patch_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  foreach (field_info_instances($entity_type, $entity->type) as $field_name => $instance) {
    if (!field_access('edit', $instance, $entity_type, $entity) && field_access('view', $instance, $entity_type, $entity)) {
      // Add state for field to form_state
      $parents = $form['#parents'];
      $items = field_get_items($entity_type, $entity, $field_name, $langcode);
      field_form_set_state($parents, $field_name, $langcode, $form_state, array(
        'field' => field_info_field($field_name),
        'instance' => $instance,
        'items_count' => count($items),
        'array_parents' => array(),
        'errors' => array(),
      ));

      $form[$field_name]['#type'] = 'container';
      $form[$field_name]['#weight'] = $instance['widget']['weight'];      
      $form[$field_name][$langcode] = field_view_field($entity_type, $entity, $field_name, 'form', $langcode);
    }
  }
}

function field_patch_entity_info_alter(&$entity_info) {
  $entity_info['node']['view modes']['form'] = array(
    'label' => 'Form',
    'custom settings' => FALSE,
  );
}
Bojhan’s picture

Issue tags: -Needs usability review

ehh?

larowlan’s picture

sheldon rampton’s picture

I had a situation where I needed to be able to show some fields on the node edit form, but only enable editing of those fields for certain user roles. Other user roles are supposed to be able to view those fields while editing but not modify them. the field_extrawidgets module was not a good fit for my needs because it doesn't support making fields read-only on a per-user-role basis. I therefore wrote my own down-and-dirty solution by adding the following code to my "job" module:


/**
 * Implements hook_form_alter().
 */
function job_permission() {
  return array(
    'modify restricted project fields' => array(
      'title' => t('Modify restricted project fields'), 
      'description' => t('Modify the agency name, labor category and assignment number fields on project pages.'),
    ),
  );
}

/**
 * Implements hook_form_alter().
 */
function job_form_job_node_form_alter(&$form, &$form_state, $form_id) {
  if (!user_access('modify restricted project fields')) {
    $form['field_agency_term']['#disabled'] = TRUE;
    $form['field_ogs_labor_category']['#disabled'] = TRUE;
    $form['field_assignment_number']['#disabled'] = TRUE;
  }
}
sheldon rampton’s picture

@yched I think my use case -- where some user roles should be able to view but not edit certain fields while editing -- is likely to come up from time to time as something that people want. I realize that some widgets may not support a disabled state, but most if not all of the standard field types in core support #disabled, so I think it should be possible to support this for all field types that recognize a #disabled attribute. For any field types that don't recognize #disabled, the failover is pretty benign.

It would be easier for the field permissions module to support this if Drupal's node access options included a "display as read-only during editing" option. I've started a ticket that suggests this: http://drupal.org/node/1391544

sheldon rampton’s picture

Issue summary: View changes

Changed issue to feature request

anrikun’s picture

On D7, you may try the Field Readonly module.

amateescu’s picture

This functionality has been available in D7 contrib for more than two years in the Field extra widgets module, as noted by @larowlan in #14 ...

sheldon rampton’s picture

@amateescu: But as I noted in #15, the Field Extra widgets module does not support making fields read-only on a per-user-role basis. On the website I was building when I posted my comment two years ago, they wanted some user roles to be able to see the read-only widget when editing, and they wanted other user roles to actually be able to edit that field.

I wrapped up that project a long time ago, so this is no longer an issue for me in any current projects. Out of curiosity, though, does the Field Readonly module provide that granularity on a user-role basis with respect to field editing/viewing when editing content?

anrikun’s picture

@Sheldon: As stated on the project page, the Field Readonly module works in conjunction with any module like Field Permissions that makes some fields non-accessible. It simply displays them on edit forms as read-only items instead of letting them hidden.
It fully respects Field Permissions' settings.

amateescu’s picture

@Sheldon, I don't think that functionality needs to be provided by a module, all that's needed is a implementation of hook_field_widget_properties_alter() that changes the widget type based on the user role.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

andypost’s picture

Version: 8.9.x-dev » 9.1.x-dev
Issue summary: View changes

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

sherkajon’s picture

So far I have achieved read only mode of the fields on edit mode by using Field Permissions in combination with Read-only Fields modules.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.