4

Using Drupal 7:

I have several content types with their own sets of fields, but I also have a set of fields that they must all share in common. That is easy enough through the administrative UI, but I'm writing a module that programmatically will add the fields to all node content types that I specify. Additionally, I do not want the administrator to be able to remove or edit those fields through the administrative UI.

How can I make it so that as an administrator is managing content types that the admin cannot modify or remove these fields that I have specified?

Alternatively, I know that I can create a separate entity and force field existence that way, but then I loose access to a lot of the functionality already built into the node entity, especially the function hook_node_access which is also a requirement for this module.

Anyone have any suggestions or know how to solve this?

3

Refer to the field api documentation here. You do not have to add permissions to block the delete. All you need to do is add the "locked" attribute to the field definition.

locked (integer): Whether or not the field is available for editing. If TRUE, users can't change field settings or create new instances of the field in the UI. Defaults to FALSE.

| improve this answer | |
  • Awesome. That is way easier than I thought it would be. Thanks for the help. – James Oct 19 '12 at 19:48
0

There is a module for that names "Field Lock".

Of course it uses the API field definition "locked" as mentioned before.

Here is a short description from the module page:

Field lock adds additional level of permissions so certain users can lock a field, enter code herepreventing others from editing or deleting it.

| improve this answer | |
0

For existing fields that are provided by (eg, media_gallery or commerce) You can run-once the snippet from http://atlantic-canary.net/es/how-to-lockunlock-a-field-in-drupal

<?php
$field_name = 'field_name';
$field = field_read_field($field_name);
$field['locked'] = 0; // 0: unlock; 1: lock.
field_update_field($field);
?>

I did it for myself in an update hook, to ensure that my extension to media_gallery could expose some settings.

/**
 * UNLOCK the description field provided by media_gallery so we can tweak settings.
 */
function niwa_media_update_7002() {
  $field = field_read_field('media_description');
  $field['locked'] = FALSE;
  field_update_field($field);
}

In my case I just wanted to limit the text formats that were available as an image caption, as full_HTML was causing problems. better_formats.module could not help me until I'd unlocked the field.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.