0

my code creates a new date element instead of modify the original elementI am learning Drupal 7. I have a custom module, trying to modify a date form element in my page. I have the form id identified, and Devel module list me all the fields in this page, I think I am typing the correct field name but it doesn't work, It creates a new form element instead of modify the form element. Maybe the element is inside a fieldset, but I don't know how to write the correct line to modify. It is a datepicker and I am trying to modify the minDate property for the date element.

Here is my code:

function davidcustom_form_alter(&$form, &$form_state, $form_id) {
  drupal_set_message($form_id);  // print form ID to messages

  // Check when the right form is passed
  if ($form_id == 'commerce_cart_add_to_cart_form_24296') {

    $form['field_line_item_fecha_reserva'] = array(
      '#title' => t('Pick the desired date'),
      '#type' => 'date_popup',
      '#datepicker_options' => array(
        'minDate' => 10,
      ),
    );


  }

}

1 Answer 1

1

When you can't find an array key, the best thing to do is to dump the entire form array and search for it. If you use the Devel module, you can use the following:

dpm($devel);

This will give you a nice collapsible output through which you can search. Or you can use the old-school method that I use, and dump it directly using:

die('<pre>' . print_r($form, TRUE) . '</pre>');
6
  • How can I know if my field is inside a fieldset, and then I have to include too the fieldset in my line of code to identify the form element?
    – bunset
    Commented Feb 11, 2017 at 3:49
  • Dig through the output and you can find it whether it's in a fieldset or what not. Also, what you have done in your original code is create a new form element. You just need to alter the elements that you want to change.
    – Jaypan
    Commented Feb 11, 2017 at 3:54
  • What is the code to only modify 'minDate' => 10 in my form element?
    – bunset
    Commented Feb 11, 2017 at 4:07
  • $form['field_line_item_fecha_reserva']['#datepicker_options']['minDate'] = 10;
    – Jaypan
    Commented Feb 11, 2017 at 5:00
  • It's more complicated then that with fields, see drupal.stackexchange.com/questions/27767/…
    – Clive
    Commented Feb 11, 2017 at 10:16

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.