sebastian
sebastian

Reputation: 17298

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

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 checkbox...once it it checked and the form is saved, I do not want this checkbox to be visible anymore...therefore based on the checkboxes value in the Database I want to hide form fields when showing the form.

I am building a small specific project site, where a company wants to add projects, and their customers are supposed to follow certain steps (upload some content, provide information etc.), and also should be able to check off certain requirements, and once these are checked off, they should not be visible/editable to them.

Also the displayed form fields should depend on an user's role, and then FURTHER be limited depending on the content's database entries.

Is there a module, which could achieve this behaviour? "rules" and "field/permissions" come close to what I need, but are not sufficient. Or did I just miss the option to change a form field's accessibility based on conditions?

What I need is some place to define a logic like "IF (VALUEOF(CHECKBOX_1) == TRUE) THEN DO_NOT_SHOW(CHECKBOX_1)"

Upvotes: 1

Views: 8569

Answers (3)

MilanG
MilanG

Reputation: 7114

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.

Upvotes: 13

coffeduong
coffeduong

Reputation: 1463

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

Upvotes: 0

Mihaela
Mihaela

Reputation: 109

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;
    }
}

Upvotes: 3

Related Questions