Score:1

How to add complete value programmatically with Smart Date module?

sa flag

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?

sonfd avatar
in flag
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 avatar
in flag
Of course you can always review the module code / field definition too.
sa flag
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
Score:0
ca flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.