1

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.

1
  • 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
4

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'] = FALSE;
  }
}

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

4
  • +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? Jul 7 '16 at 22:59
  • I'm very interested in implement this, I make a custom module with only 2 files, the info.yml (I did not filled any dependency, not sure if that part is ok) and the other file with your code (starting with opening <?php), my module name on the function, in form ID I put the Content Type machine name, and for the field, the field machine name. This file is saved in src/Form and saved with the name FormStateInterface.php (not sure if the file can get a custom file name), but it is not working. Maybe is because I am trying in Drupal 9? I would appreciate if you tell me what I am doing wrong.
    – MrIssue
    Feb 19 at 19:59
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
  • 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 Oct 26 '17 at 13:05
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..

1
  • 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

0

Use hook_form_alter to set the disabled property TRUE for the form field:

If its required to Disable the field only while editing only then,

function module_name_form_alter(&$form, &$form_state, $form_id){
  if($form_id == 'form_id_name'){
    if (arg(2) == 'edit') { #Use the URL parts
        $form['field_name']['#disabled'] = TRUE;
     }
  }
}

If its required to Hide the field only while editing only then,

function module_name_form_alter(&$form, &$form_state, $form_id){
  if($form_id == 'form_id_name'){
       if (arg(2) == 'edit') { #Use the URL parts
         $form['field_name']['#access'] = FALSE;
     }
   }
}
-1

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

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.