Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi,
I am having a odd problem with the date field (Date module).
If I create a node manually, using a string as a date say 2/13/2103 it correctly saves to the node.
However, if I pass the same exact string through an RSS field using Feed Import, it doesn't work.
I get this message:
PDOException: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2/13/2013' for column 'field_pub_date_value' at row 1: INSERT INTO {field_data_field_pub_date} (entity_type, entity_id, revision_id, bundle, delta, language, field_pub_date_value) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 953 [:db_insert_placeholder_2] => 449 [:db_insert_placeholder_3] => video [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => 2/13/2013 ) in field_sql_storage_field_storage_write() (line 448 of /home/xxxxxx/public_html/modules/field/modules/field_sql_storage/field_sql_storage.module)
Comment | File | Size | Author |
---|---|---|---|
#6 | datefeedimport.png | 14.33 KB | netentropy |
Comments
Comment #1
Sorin Sarca CreditAttribution: Sorin Sarca commentedHi, you have to create a custom filter for that and usit it in Filters section. Example:
I don't remember right now the date field structure, but it is something like that.
You have to return an object with properties that match field properties.
Comment #2
netentropy CreditAttribution: netentropy commentedCool. Are we supposed to add filters directly to the filters included file or is their a hook where we can write a module for custom filters.
Comment #3
Sorin Sarca CreditAttribution: Sorin Sarca commentedA filter can be any PHP function, so you can put your functions where you want, just make sure they are loaded.
Comment #4
netentropy CreditAttribution: netentropy commentedAny other ideas on this? I find it odd that the node form will accept a typed string yet won't accept it from the xpath. I can add dates pragmatically to nodes but that doesn't work in this case either.
Thanks again!
Comment #5
Sorin Sarca CreditAttribution: Sorin Sarca commentedCreate manually a node, fill a date and use dpm() to print show node properties. Upload here the image containing data relevant to date field.
Comment #6
netentropy CreditAttribution: netentropy commentedThanks again for taking a look. Here is the requested image.
Comment #7
Sorin Sarca CreditAttribution: Sorin Sarca commentedHere is an example:
Comment #8
netentropy CreditAttribution: netentropy commentedIt only worked for me with a capital H
function convert_to_date_field($date) {
$date = strtotime($date);
$date = date("Y-m-d H:i:s", $date);
return (object) array(
'value' => $date,
'timezone_db' => 'America/New_York',
'date_type' => 'datetime',
);
}
Comment #9
Sorin Sarca CreditAttribution: Sorin Sarca commentedComment #10
aniebel CreditAttribution: aniebel commentedHi, I am doing this same thing and despite creating this custom module, I'm still getting invalid date format error when trying to import. A few questions:
1.) what's the syntax for adding the new function to the filter? Do I still add the "::" before?
2.) does it matter what the widget type is for the date field? Suppose it's a select list where it becomes an array?
I'd like to write up my full example of a user import via XML once I get it working if that helps others.
Comment #11
aniebel CreditAttribution: aniebel commentedComment #12
Sorin Sarca CreditAttribution: Sorin Sarca commented1) You can use any php or Drupal function, just write its name and desired parameters.
For example, if you want to make all text uppercase you can use php's strtoupper function.
If a class provides a static method (function) you can use it by specifying both class name and method separated by :: (double colon).
Feed Import provides some filter functions encapsulated in FeedImportFilter class (as static methods). This is a special case when you are not required to specify class name (FeedImportFilter will be assumed), only the function name.
You can see all provided filters here https://drupal.org/node/1363028
So if you want to replace some text you can simply use ::replace. It is still correct to use FeedImportFilter::replace.
Even if php has str_replace function it will not work on multivalued fields, but methods from FeedImportFilter takes care of those cases.
2) Should not matter. There are hundreds of field types in Drupal, and I cannot support all of them. But you are able to have a field with all properties by returning an object (simplest way is to cast the array to object), as in above examples of date field.
You can found info about field properties with devel module.
Hope this helps.
Comment #13
aniebel CreditAttribution: aniebel commentedThanks for the kind feedback. My issue was the input date was year only. As soon as I changed it to 2002-12-31 instead of 2002, it worked like a charm. Will be posting my findings once I do a complete import.
Comment #14
Sorin Sarca CreditAttribution: Sorin Sarca commentedComment #15
docans CreditAttribution: docans commentedHi
I have a text field date value in the format
How do i convert it. Which php code is most suitable
Comment #16
Sorin Sarca CreditAttribution: Sorin Sarca as a volunteer commentedHi @docans,
please open a new issue and describe your problem (using more details) so I can help you.
Thanks!
Comment #17
docans CreditAttribution: docans commented