0

I need to get some fields of a node edit form, for that purpose I use field_attach_form() to get the fields that I want with the value of the node.

Here's an example of the code of my form function:

function my_form($form, &$form_state, $node_id, $fields) {
  $node = node_load($node_id);
  $node_form = $form;
  field_attach_form('node', $node, $node_form, $form_state);

  foreach ($fields as $field) {
    if ($field->show) {
      $form[$field->name] = array('#type' => 'fieldset', '#title' => t('Field'), '#tree' => TRUE,);
      $form[$field->name]['override'] = array('#type' => 'checkbox', '#title' => t('Override'),);
      $form[$field->name][$field->name] = $node_form[$field->name];
      $form[$field->name][$field->name]['#states'] = array(
        'enabled' => array(
          ':input[name="' . $field->name . '[override]"]' => array('checked' => TRUE)
        ),
      );
    }
  }

  return $form;
}

I just wrap every field (that I want to show) in a fieldset with a checkbox.

One of the field is an image type, when field_attach_form loads the field, it shows the image thumbnail and the image file name perfectly, but when I click submit (without edit the field content) the value of the field is empty. In fact, the image field is required so the validation fails.

When I remove the previous image and upload a new one, it works perfectly. Am I missing something?

3
  • How are you calling field_attach_form?
    – jonpeck
    Oct 28, 2013 at 18:34
  • At the begin of the hook_form, as I post in my question.
    – ilazgo
    Oct 29, 2013 at 7:32
  • @jonpeck I updated my code. The problem comes with the #states if the field is disabled, when the form does the validation the field is empty.
    – ilazgo
    Oct 30, 2013 at 8:27

1 Answer 1

0

I found a workaround: I just have the fields twice, one enabled and the other disabled. With the checkbox I show one or another.

function my_form($form, &$form_state, $node_id, $fields) {
  $node = node_load($node_id);
  $node_form = $form;
  field_attach_form('node', $node, $node_form, $form_state);

  foreach ($fields as $field) {
    if ($field->show) {
      $form[$field->name] = array('#type' => 'fieldset', '#title' => t('Field'), '#tree' => TRUE,);

      $form[$field->name]['override'] = array('#type' => 'checkbox', '#title' => t('Override'),);

      $form[$field->name][$field->name] = $node_form[$field->name];
      $form[$field->name][$field->name]['#states'] = array(
        'visible' => array(
          ':input[name="' . $field->name . '[override]"]' => array('checked' => TRUE)
        ),
      );

      $form[$field->name][$field->name . 'disabled'] = $node_form[$field->name];
      $form[$field->name][$field->name . 'disabled']['#disabled'] = TRUE;
      $form[$field->name][$field->name . 'disabled']['#states'] = array(
        'visible' => array(
          ':input[name="' . $field->name . '[override]"]' => array('checked' => FALSE)
        ),
      );
    }
  }

  return $form;
}

It seems that the action enabled / disabled is buggy.

Your Answer

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

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