1

Problem

I have a form where users can create a node by filling in a number of fields. It creates a node using the Entity Metadata Wrapper as shown here.

I have two date fields on this form, however whatever value is selected for the date on the form, today's date will be shown on viewing the newly created node.

If you then edit the node, the correct date appears and on saving the node the correct date re-appears. (Opening the node isn't enough, you have to save it.)

Has anyone solved this? Any idea what might have caused it?

Example Code

I checked through every step of the Start date part of the entity wrapper (using dpm).

The correct date ends up in $entity, but for some reason it's still displayed incorrectly if you don't open the node. (It reminds me of this other problem I had with the wrapper.)

function createanode_form($form, &$form_state){

  $form['field_start_date'] = array(
    '#type' => 'date_popup',
    '#title' => t('Start Date'),
    '#date_format' => 'd/m/Y',
    '#default_value' => NULL,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#weight' => 70,
  );


function createanode_form_submit($form, &$form_state) {

  $values = array(
    'type' => 'job_advert',
    'uid' => $user->uid,
    'status' => 1,
    'comment' => 1,
    'promote' => 0,
  );
  $entity = entity_create('node', $values);

  $ewrapper = entity_metadata_wrapper('node', $entity);

  $ewrapper->title->set($form_state['values']['title']);
  $ewrapper->body->set(array('value' => $my_body_content));
  $ewrapper->body->summary->set('Things that interest me');

  if ($form_state['values']['field_start_date'] != NULL) {
    $job_date = new DateTime($form_state['values']['field_start_date']);
      $entity->field_start_date[LANGUAGE_NONE][0] = array(
         'value' => date_format($job_date, 'Y-m-d'),
         'timezone' => 'UTC',
         'timezone_db' => 'UTC',
      );
  }

  entity_save('node', $entity);
}
3
  • do you insist only use entity or saving the node is important for you?
    – Yuseferi
    Feb 18, 2014 at 17:11
  • Do you mean try using the old way of saving a node as opposed to using the entity metawrapper? (i.e. the first example on here) Feb 19, 2014 at 2:35
  • If you only want to load then save , the old version is suitable and can perform you job correctly although entity is new method that with some changes exist in core of D8.everything dependencies what you want. sometimes should change the way to prevent spend more time.
    – Yuseferi
    Feb 19, 2014 at 5:15

0

Your Answer

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

Browse other questions tagged or ask your own question.