0

Is it possible to hide fields on node edit form? I've tried Formatter Field but it doesn't work for me. I know it's possible in content type field display management but I need this functionality on node edit form.

  • Hide field option* on node edit form. Example: I'm editing a node and i have fields, which i don't want to display on the site for users, so i'm checking "Hide on site" option. – user504944 Aug 30 '15 at 20:16
3

The Field extra widgets allows to do this in a easy way:

Provides two helper widgets for the Field API:

  • Hidden: this widget doesn't show anything on the edit form, and as a consequence prevents the user from modifying the content of the widget. This is useful in some situations where using hook_field_access() is not suitable (for example when you want to have different edit forms for different users).
  • Read-only: this widget shows the content of the field on the edit form, but doesn't allow the user to edit it. The content is rendered using one of the formatter of the field.
  • 2
    This is the better answer because it actually provides a solution that addresses the Question directly, instead of making use of the side effect of setting the field permissions. – reor Nov 16 '16 at 0:31
  • 1
    Very useful module, didn't know about this! Thankfully this issue is fixed with form displays in D8 – Matt Fletcher Oct 26 '17 at 13:05
2

You can use the Field permissions module and disable the edit permission for that field, or you can use hook_form_alter and the #access property, here's an example:

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, $form_state, $form_id) {

  if ($form_id == "contenttype_node_form") {
    $form['field_foo']['#access'] = 0;
  }
}

For a node edit form you must also check if the node exists.

  • +1 for hook_form_alter(). That definitely is the cleanest solution. – leymannx Aug 30 '15 at 22:39
  • how to use hook_form_alter if i want to display two different screens of editing the same content type? – Justme Jul 7 '16 at 15:48
  • Sorry, can you better explain your question? – user1527576 Jul 7 '16 at 22:59
0

You can use hide() function in template.php or node--CONTENTTYPE--edit.tpl.php files.

0

you can use hook_form_alter to unset your specified fileds based on form_id check. hook_form_alter()

/**
* Implements hook_form_alter().
*/
function MY_MODULE_NAME_form_alter(&$form, $form_state, $form_id) {

  if ($form_id == "contenttype_node_form") {
    unset($form['field_name']);
 }
}

i hope now you can understand better..

  • You should provide a description with your code snippet; providing a snippet alone might not help anyone, especially if they don't know what to do with it. – Shawn Conn Aug 31 '15 at 5:00
0

Using: Drupal 7.39 If you need to show the field for form submission but do not want the user to be able to modify them maybe this will help

I was hijacking basic_cart form and trying to make the fields readonly, after much stuffing around.

I needed the fields to be submitted but disallow the user to edit the contents of those fields.

I tried your method but it didn't work, I was able to disable the field using:

$form[$fieldName][LANGUAGE_NONE][0]['#disabled'] = TRUE;

but that meant it (the field) didn't get submitted, so after looking into includes/form.inc I found this gem

 if (!empty($element['#disabled'])) {

if (!empty($element['#allow_focus'])) {
$element['#attributes']['readonly'] = 'readonly';
}
else {
  $element['#attributes']['disabled'] = 'disabled';
}
}

So then I found this worked.

$form[$fieldName][LANGUAGE_NONE][0]['#disabled'] = TRUE;
$form[$fieldName][LANGUAGE_NONE][0]['#allow_focus'] = TRUE;

You may or may not need to apply this patch ( I think it works without) https://www.drupal.org/files/issues/drupal-7.x-add_readonly_to_form_fields-3.patch

And thats how I disabled my fields :D

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.