hook_field_update_forbid() allows modules to prevent fields from being updated, but field_ui doen't catch the documented FieldUpdateForbiddenExceptions when they are thrown.

I don't know enough about creating tests for the Drupal test suite to prepare a full patch, so I will provide some notes here for someone (may be at a later date) to work from.

field_ui.admin.inc:1580 Only attempts to catch FieldExceptions, but not FieldUpdateForbiddenExceptions. I propose that line 1585 the following code be inserted:

  catch (FieldUpdateForbiddenException $e) {
    drupal_set_message(t('Update of field %label forbidden: %message.', array('%label' => $instance['label'], '%message' => $e->getMessage())), 'error');
    $form_state['redirect'] = field_ui_next_destination($entity_type, $bundle);
  }

A more significant issue exists in field_ui.admin.inc:1974
Call to field_update_field isn't wrapped in a try-catch block, so any exceptions thrown aren't caught. This code should be wrapped in a similar try-catch block which handles both FieldExceptions, and FieldUpdateForbiddenExceptions

Comments

q0rban’s picture

I see you ran into this in UUID module, but I cannot reproduce this:

/**
 * Implements hook_field_update_forbid().
 *
 * Ensures cardinality is always 1.
 * 
 */
function uuid_field_update_forbid($field, $prior_field, $has_data) {
  if ($field['type'] == 'uuid' && 1 !== $field['cardinality']) {
    throw new FieldUpdateForbiddenException(t("Cardinality for @field_name must always be 1.", array('@field_name' => $field['field_name'])));
  }
}

This works fine for me.

yched’s picture

Component: field system » field_ui.module

recategorize

Version: 7.0-rc4 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.