I'm using hook_form_alter to modify an admin edit node screen. The form has a datetime field which I'm unable to get the value for in the render array. textfields value usually appears in value->#default_value.

xxx_form_alter(&$form, &$form_state, $form_id) {

enter image description here

When the page loads, the value is in the input field but I can't find where I can get hold of the value before the page loads.


The solution was $item['value']['#default_value']->render(). This outputs the value in the following format Y-m-d H:i:s e.

Alternatively, calling format('Y-m-d') or any other format string will update accordingly

because '#default_value' was of type DrupalDateTime which must not implement jsonSerialize this is why I was getting and empty object for a value when dumping out as JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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