1

Iam using module Smart Date on Drupal 9.

And required programmatically add value to node in field with recurring rule (like Repeats every 1 year on xxxx-xx-xx). Technically, for this action also creating record 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:

// Create new node:
$node = \Drupal::entityTypeManager()->getStorage('node')->create([
  'type'        => 'event',
  'title'       => 'My new note title',
]);

// Assign value to field_date:
$node->set('field_date', '2022-03-01');
// ...needed add here also rule of yearly recurring...

field_data - its name of smartdate field, but how add field value with complete data (repeats every, until and etc.)? Like in field widget on node edit with admin panel, there are more options available for this field.

Any ideas?

3
  • Usually, I find the easiest way to do figure this kind of thing out is to 1. Inspect the edit form to see what the field names are - very often they map to the property names a field expects. 2. Create a presave hook and add a breakpoint, then fill out the form with values, submit the form and inspect the field at your breakpoint.
    – sonfd
    Commented Mar 7, 2022 at 12:35
  • Of course you can always review the module code / field definition too.
    – sonfd
    Commented Mar 7, 2022 at 12:36
  • Yes, you said it right... But now I'm working on a hosting server, there there is no xdebug, and I'm starting to learn Drupal 8/9 current, until it's clear in which direction to dig when the module is not quite standard
    – Eduard F.
    Commented Mar 7, 2022 at 13:40

2 Answers 2

0

When adding a Smart date range field to a content type, a table node__field_name will be added to the database. You have to fill in both the start (field_name_value) and end date (field_name_end_value) with a timestamp.

For example with your field name:

$node->field_date->value = 1676039400;
$node->field_date->end_value = 1676046600;

For all day events you can fill in the duration as well:

$node->field_date->value = 1675983600;
$node->field_date->end_value = 1676069940;
$node->field_date->duration = 1439;

Smart date fills in the number of days in minutes -1 minute for all day events as duration.

0

In case anyone else hits this problem, the solution that worked for me (Drupal 10.3, smart_date:4.2) is:

$node->field_date_and_time_range = [
  'value' => $arrival,
  'end_value' => $departure,
  'duration' => intval(($departure - $arrival) / 60),
];

where $arrival and $departure are integers created by strtotime() and duration is the difference between the two in minutes.

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.