Using module Smart Date on Drupal 9. And required programmatically add value to node with recurring rule (like Repeats every 1 year on xxxx-xx-xx). For this action also needed create item in Rule table... But I understand, how this do... It seems like the smartdate module itself should make an entry in this additional table.

I used like this operation:

$node->set('field_date', $results['kal_tahtpaevad_kuup']);

field_data - its smartdate field, but how add field value with complete data (repeats every, until and etc.)
$results['kal_tahtpaevad_kuup'] - its my date... also needed add yearly repeat rule?

Any ideas?

Comments

tostinni’s picture

Hi quantum3k, have you found a solution ?

Thanks

ashroof’s picture

I faced same issue, I was able to work around by adding form_state to add the smartDate value
here is my code
 

// date and time values 
$startDate = new DateTime('2024-16-03 10:00:00 Am');
$endDate = new DateTime('2024-16-03 10:30:00 Am');

// smartDate array which has all recurring information
$schedule = ["time_wrapper" => ["value" => ["date" => $startDate->format('Y-m-d'), "time" => $startDate->format('H:i:s')], "end_value" => ["date" => $endDate->format('Y-m-d'), "time" => $endDate->format('H:i:s')]],
                "duration" => 30,
                "timezone" => "UTC",
                "repeat" => "WEEKLY", // DAILY - MONTHLY - YEARLY
                "repeat-end" => "COUNT",
                "repeat-end-count" => "8",
                "repeat-end-date" => "", //$endDate
                "_weight" => "0",
            ];
// add node array with node information 
            $node = array(
                'type' => $type,
                'created' => time(),
                'uid' => $uid,
                'title' => $title,
                'body' => ['value' => $body],
            );
 $node =  \Drupal::entityTypeManager()->getStorage($entity_type)->create($node);
            $node_form_object = \Drupal::entityTypeManager()
                ->getFormObject('node', 'default')
                ->setEntity($node);
            $form_state = new FormState();
            $form_state->setValue('field_event_date', [$schedule]);
            $form_state->setValue('op', $this->t('Save'));
            $form_state->setValidateHandlers([]);
            \Drupal::formBuilder()->submitForm($node_form_object, $form_state);

you can change the $schedule array to meet your recurring rule