Drupal 7 - Hide certain form fields of a content edit form depending on the content data

前端 未结 3 469
感情败类
感情败类 2021-01-16 15:17

In Drupal 7, is there a way to change the standard edit form for a content type based on a certain content?

For example:

I have a content type with a checkb

3条回答
  •  死守一世寂寞
    2021-01-16 15:38

    hook_form_alter is the way to do this, as explained by Mihaela, but what options do you have inside that function?

    • If you want just to disable field (it will be visible, but user can't change it) you can do it like this:

      $form['field_myfield']['#disabled'] = TRUE;

    • And if you want it to be hidden, but to keep value it has before editing the way to do that is:

      $form['field_myfield']['#access'] = FALSE;

    I.e. hiding it (somewhere I saw someone suggesting that):

    hide($form['field_myfield']);
    

    really hides the field, but after that, when form is saved this field has empty value, validation fails, etc, so that's not a good way to do this. Hiding makes sense only if you want to print separately that field later, at some other place.

提交回复
热议问题