1

I have a field that is part of a conditional related to several field collections. As such, it needs to be rendered when adding or editing a form. However, once the node has been created, I don't want users to be able to modify the value. So on the edit page, I want to hide the DIV surrounding it.

I thought I could trick it with CSS in Panels, but add and edit use the same page and the same CSS shows in each.

1
  • Use form alter get arg from url do your trick here for adding extra class and now you can play the css trick Aug 24, 2014 at 3:57

3 Answers 3

2

As both answers above mention, there are two ways to hide a field on the node edit form:

  1. via css.
  2. via a form alter hook.

CSS Solution

On the node creation page the body tag has class "page-node-add", but on the edit node page - "page-node-edit", so you can hide the field with CSS in your theme like so:

.page-node-edit .field-name-field-myfieldname {
  display: none;
}

The CSS-based solution has two main caveats:

  1. An astute user will be able to unhide the field, modify it, and submit the form. There is nothing on the server-side checking to prevent submitting a change.
  2. If you're using the admin theme on node edit, then adding custom css to the page is a bit more complicated than just adding this line of CSS to your front-end theme's stylesheet. You'll need to either add it via a custom module, or hack the admin theme (not recommended), or implement a custom child-theme of the admin theme.

For these reasons, I recommend the second option, use a form alter hook to hide the desired field:

Hook_Form_Alter Solution

You should add the following snippet in a custom module enabled on your site. You may either remove the form element entirely with '#access' => FALSE or leave the form element visible but non-editable using '#disabled' => TRUE.

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'MYNODETTYPE_node_form') {
    if (isset($form['#node']) && isset($form['#node']->nid)){
      // Prevent editing a field once the node has been created by 
      // hiding the field entirely on the edit form.
      $form['field_MYFIELDNAME']['#access'] = FALSE;

      // Alternatively, if you want to display the field, but prevent
      // it from being edited, you can disable the form element.
      $form['field_MYFIELDNAME']['#disabled'] = TRUE;
    }
  }
}

This solution has no caveats really, and is probably the better solution.

2
  • Thanks- I hadn't thought about being able to unhide by inspecting the CSS and changing it. I will definitely use the hook_form_alter method in Production. But for local testing, I will stick with the CSS to finish the proof of concept mockup. Thanks!
    – Dave
    Aug 24, 2014 at 23:54
  • My needs where the other way arround, to hide a field on "node add" and have access on "node edit", following the rigth way with your code i did "hook_form_alter", solution: before the "if" i set node access false on my field, and inside "if" i set node_aacess true. i upvoted your answer, works, perfect. thanks James.
    – pinueve
    Mar 29, 2017 at 1:35
3

Whene you go to node create page, the body tag has class "page-node-add", but on edit node page - "page-node-edit". So you can add CSS code:

.page-node-edit #your_div_id {display: none;}
2
  • This solution has a couple caveats, that I've outlined below in my combined answer. Aug 24, 2014 at 22:35
  • Ah, I knew there had to be something. I just didn't go up far enough to see the page-node-edit class. Thanks!
    – Dave
    Aug 24, 2014 at 23:51
0

You should inspect the node object to see if there is a nid, which is the primary identifier for the node table. If there is a nid, it is the edit page, and if there is no nid, then it is creation page.

Something like this:

function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'MYNODETTYPE_node_form) { // check for the node edit form
    if (isset($form['#node']) && $form['#node']->type == 'MYNODETTYPE'){ // check for your bundle here
      if (!isset($form['#node']->nid)) { // a node exists
        // create form

      }
      else { // no node exists yet
        // edit form
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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