-1
public function buildForm(array $form, FormStateInterface $form_state) {
  $form['start_date'] = [
    '#type' => 'datetime',
    '#title' => $this->t('Start Date'),
    '#required' => TRUE, 
  ];
  $form['end_date'] = [
    '#type' => 'datetime',
    '#title' => $this->t('End Date'),
    '#required' => TRUE
  ];
  $form['location'] = [
    '#type' => 'entity_autocomplete',
    '#target_type' => 'node',
    '#selection_handler' => 'default',
    '#selection_settings' => ['target_bundles' => ['location']],
    '#title' => $this->t('Location'),
  ];
  $form['show'] = [
    '#type' => 'submit',
    '#value' => $this->t('Create Event'),
  ];
  return $form;
}

/**
  * {@inheritdoc}
  */
public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state->getValues();
  $location = $form_state->getValue('location');
  $sd = $form_state->getValue('start_date');
  $sd = $sd->format('Y-m-d H:i:s');
  if($location == 1){
    $loc = "ASCET SE Center";
  }
  $node = Node::create(array(
    'type' => 'event',
    'title' =>  $sd . " " . $form_state->getValue('location'),
    'langcode' => 'en',
    'uid' => '1',
    'status' => 1,
    'field_location' => $loc,
    'field_start_date' => $sd,
  ));

I'm attempting to populate fields within the content type (event). The Event node type has a start date, end date, and an entity reference to a Location content type. When passing the $loc I get a message that says:

"InvalidArgumentException: Value is not a valid entity. in Drupal\Core\Entity\Plugin\DataType\EntityReference->setValue()"

When I pass $sd as a string it doesn't populate. When I pass $sd as $form_state->getValue('start_date'); it says that there is too much data to put into the DB.

2

You need to pass the ID of the referenced entity in the target_id column for the entity reference field, and value column for the timestamp field. Something like:

$target_id = $form_state->getValue('location');
$date = $form_state->getValue('start_date')->getTimestamp();

$node = Node::create(array(
  'type' => 'event',
  'title' =>  $sd . " " . $form_state->getValue('location'),
  'langcode' => 'en',
  'uid' => '1',
  'status' => 1,
  'field_location' => [
    'target_id' => $target_id,
  ],
  'field_start_date' => [
    'value' => $date,
  ],
));
  • Thanks for the answer Clive. However, this doesn't seem to be working for me still. InvalidArgumentException: No entity type was provided, value is not a valid entity. is the message I receive for the location, and the date just simply doesn't populate. Thoughts? – RJ Reedy Nov 2 '17 at 14:05
  • I haven't tried the entity reference one, but that's definitely the column name so it should work in theory. The date one I've tried as recently as this morning in my own code and it woks fine for me (using 8.4). You are calling ->save() on the $node object after creation yeah? And what line is throwing the invalid arg exception? – Clive Nov 2 '17 at 14:21
  • It appears that line 72 is causing the error which is the 'target_id' => $target_id line. I am calling $node->save(). The node will create if I comment out the field_location array, however it doesn't populate the date information. – RJ Reedy Nov 2 '17 at 14:28
  • Ok...just a hunch, try 'field_location' => $form_state->getValue('location') instead – Clive Nov 2 '17 at 14:56
  • Actually no, try 'field_location' => [$target_id]. It's rather confusing – Clive Nov 2 '17 at 14:58

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.