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

前端 未结 3 437
感情败类
感情败类 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:31
    function your_module_form_alter(&$form, &$form_state, $form_id){
    
        switch($form_id) {
        case 'nameOfTheNode_node_form':
            //your code here. check the value from from_state.
        break;
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-16 15:43

    In this case, I use module Conditional Fields https://www.drupal.org/project/conditional_fields

    For example: If my Dependees field has a value, Dependent field can be visible/invisible, enabled/disabled, required/optional, checked/unchecked

    0 讨论(0)
提交回复
热议问题