2

I have a form that users can fill out and it creates a node, an event node to be shown in the community calendar of my website. I'm using Rules to create the new node when the "Webform: After a webform has been submitted" event (requires Webform Rules module).

Everything is working well except for the Date field. The event node needs a date and time, that's kinda the point of a calendar!

I can select the webform data with [data:date-value] and [data:time-value] and I know that they output the correct data because if I put them into a regular text field ([entity-created:body:value]) the date and time show up just fine. And I can use PHP if I need to:

<?php 
  echo $data['components']['date']['value'][0] ;
?>

I just don't know what Data Selector to choose for the date field. There's several that Rules knows about but so far I have no luck:

  • entity-created:field-date
    • if I choose this, in the resulting page to set the new value, there is no PHP Evaluation or Replacement Patterns available to use, only the Data Selector ajax dropdown, which doesn't allow for the [data:date-value].
    • and it says "Data types: Select data of the type List<struct>."
  • entity-created:field-date:0 - it goes from 0 to 3 although there's only 1 date field for the event node, and they all the same
    • if I choose this, in the resulting page to set the new value, there is no PHP Evaluation or Replacement Patterns available to use, only the Data Selector ajax dropdown, which doesn't allow for the [data:date-value].
    • and it says "Data types: Select data of the type Struct."
  • entity-created:field-date:0:value (Start Date)
    • in the resulting page to select the new value, it says: "Data types: Select data of the type Date." so I think it's a step in the right direction...
    • now there's a plain value field I can use (direct input mode),
      • instructions say: "The date in GMT. You may enter a fixed time (like 2016-06-04 16:01:07) or any other values in GMT known by the PHP strtotime() (link is external) function (like "+1 day"). Relative dates like "+1 day" or "now" relate to the evaluation time." If I put "+1 day" into the field and test it by submitting the webform, then a node is correctly created with the date field set one day into the future.
    • I can switch to the Data Selection mode and I have Data Selectors, PHP Evaluation and I can add an offset.
      • if I set the new value to [data:date-value] or [data:date-value] [data:time-value] then it doesn't say there's any errors, but when I test it but submitting the form and checking the new node that was created, the Date field was not set
      • if I try to set the value to <?php echo $data['components']['date']; ?> ... still no luck. And no errors are displayed or logged.
    • it also lists entity-created:field-date:0:value2 (End Date) and entity-created:field-date:0:duration (Duration) as possible "Set a data value" things to select

Anybody have any idea what I'm supposed to do here?? Thank you!

  • if i put return $value + strtotime($data['components']['date']['value'][0]); into the PHP Evaluation field with site:current-date still in the Data Selector field, it does set the Date field of the new node BUT IT DOESN'T SET THE DATE/TIME INTO THE FUTURE as expected! THEREFOR I CAN CONCLUDE that the PHP Evaluation field CANNOT access the $data variable and probably nothing but the $value variable D: – Dan Mantyla Jun 24 '16 at 19:41
1

Re Dan Mantyla's comment, if your date field is not a Date (UNIX timestamp) or Date (ISO) field but just a normal Date field, the following code works for me:

$thedate = $data['components']['date']['value'][0].' 00:00:00';

// set the date
$entity_created->field_date[LANGUAGE_NONE][0] = array(
    'value' => $thedate,
  );
0

it's not possible

when setting value for entity-created:field-date:0:value if I use the data selector field and choose site:current-date then I am able to set the date field of the new node to the current date and time.

if I use PHP to emulate the same date format, ie "Fri, 06/24/2016 - 14:10", with the field data - return format_date(strtotime($data['components']['date']['value'][0] . ' ' . $data['components']['time']['value'][0])); - it prints in the same format (I know because I print it to the screen using the dms() function) but it does NOT set the date field of the new node. It does not work :(

If I simply type the string and use PHP to return it in the PHP Evaluation field of the Rules setting, like this - return "Thu, 08/04/2016 - 02:02"; - it STILL DOES NOT WORK ! :(

if I put return $value + 1000; into the PHP Evaluation field with site:current-date still in the Data Selector field, it does set the Date field of the new node correctly, with the date a few minutes into the future from the current date and time, as expected.

if i put return $value + strtotime($data['components']['date']['value'][0]); into the PHP Evaluation field with site:current-date still in the Data Selector field, it does set the Date field of the new node BUT IT DOESN'T SET THE DATE/TIME INTO THE FUTURE as expected! THEREFOR I CAN CONCLUDE that the PHP Evaluation field CANNOT access the $data variable and probably nothing but the $value variable.

edit: https://www.drupal.org/node/1231418 - "In contrast to the php input evaluator, the php data processor doesn't support accessing variables yet. Patch welcome."

0

While it is not possible to set the date using the Set a Data Value action (see my other answer), it is possible to set it using the Execute custom PHP code action.

This is the code I am using.

// set the date
$date = format_date(strtotime($data['components']['date']['value'][0] . ' ' . $data['components']['time']['value'][0]), 'custom', 'Y-m-d H:i:s', 'UTC'); // ISO format
$entity_created->field_date[LANGUAGE_NONE][0] = array(
    'value' => $date,
    'timezone' => 'UTC',  
    'timezone_db' => 'UTC',
  );

The format is extremely important. It will not work unless it exactly matches the format, which is controlled by the 'Field Type' you have chosen for the date field of the content type (ISO or Unix Timestamp). I have ISO. I was missing the '\T' part of the format and I was scratching my head for weeks trying to figure it out. Date Field Types explains it a little.

  • Executing custom PHP in a Rules Action requires the (dangerous) PHP filter to be enabled, and requires PHP code to be stored in your Drupal database. Which is perceived as not a good practise. You may want to reconsider your solution, using the technique explained in the 2nd part of my answer. All it takes is to add an extra Rules Action to "Add a variable". – Pierre.Vriens Dec 1 '17 at 13:00
0

Since you are using Webform, the "clue" to answer your question might be that you have access to all labels and all values of any field of a webform available, i.e:

  • [data:MachineFieldName-title] contains the Label of a field with machine name MachineFieldName.
  • [data:MachineFieldName-value] contains the Value of a field with machine name MachineFieldName.

Refer to my answer to "How to create a node from a Webform using rules?" for way more details.

The above is a good start, but not sufficient to achieve your goal (= "... what Data Selector to choose for the date field", as in your question). However, you can use a technique similar to what is described in my answer to "How to do a data comparison to submitted webform data when using the Rules module?". For short:

  • Add an extra Rules Action to Add a variable, with a value assigned to it equal to your [data:date-value-raw] (and with a data type which equals the data type of your Webform component). Looks like you need 2 variables though: 1 related to [data:date-value], one for [data:time-value].
  • In your subsequent Rules Actions you can refer to the value of these variables you just added (that's the clue!).
  • Down voted because it doesn't really answer the question, just seems to provide generic advice. – mpdonadio Jun 3 '16 at 22:50
  • @mpdonadio ... You want to reconsider your downvote (as per my improvement I just applied)? Trust me, this technique I added just now works like a charm ... – Pierre.Vriens Dec 1 '17 at 12:56

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.