0

I'd need to enable editing of certain fields only when the node they're assigned to already exists. Therefore I need to disable them on node create form and enable them on node edit form.

I though I'd do it the same way as with users where you have two separate forms (user profile a user register) and therefore can use settings array on field instance (at least I believe it works this way).

...
'settings' => array(
   'user_register_form' => 0, 
),
...

The problem is you can't do it with nodes as nodes don't have separate forms for edit and create.

I know I can do it directly via altering CONTENTTYPENAME_node_form (changing the access status of the fields to false or unseting them completely when the node does not yet exist) but it doesn't seem to be the very best solution (It doesn't feel systematical to unset it on form alter hook).

BTW: I know that someone asked similar question but he hasn't received any answers.

  • There is nothing bad about using #access=false in a hook_form_alter. But avoid deleting fields using unset() because it may produces problems/php notices with other modules which implement hook_form_alter or the form validation. – Nebel54 Mar 17 '14 at 21:05
  • Hmm, ok. Will do it that way. – Petrroll Mar 17 '14 at 21:24
3

This is certainly without a doubt a job for hook_form_alter() and quite easy to accomplish, eg, something along the lines of:

function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {

  switch ($form_id) {

    case 'foo_node_form':

      if ($_GET['q']=='node/add/foo') {

        // we are creating a node of type foo so do stuff to the form one way here

      } else {

        // we are editing a node of type foo so do stuff to the form another way here

      }

      break;

   // other forms here

  }

}

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.