I'm currently using Feeds to import events from an iCal source, and using Feeds Tamper / rewrite plugin to pass some static values to the created nodes. However, it's now giving the following error, causing the import to fail:

An AJAX HTTP error occurred. HTTP Result Code: 500 Debugging information follows. Path: /drupal/batch?id=225&op=do StatusText: Service unavailable (with message) ResponseText: Recoverable fatal error: Object of class FeedsDateTime could not be converted to string in feeds_tamper_rewrite_callback() (line 45 of /var/www/html/techwrap.co.uk/drupal/sites/all/modules/feeds_tamper/plugins/rewrite.inc).

These are not date or time fields, but links, plain text and location fields. Disabling all the fields set to rewrite in Tamper solves the issue, but strangely I have one field set to rewrite a static term into a node reference field, and that works without any problem. Other plugins also work fine.

I've tried different versions of Feeds Tamper, Feeds and Date iCal. Also tried usual tricks of deleting / recreating importers.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

twistor’s picture

Version: 7.x-1.0-beta5 » 7.x-1.x-dev
Priority: Major » Normal
Status: Active » Postponed (maintainer needs more info)

Can you give me some information about your setup?

I can't reproduce this.

RedEight’s picture

I'm experiencing the same error. After tracking down the issue, here is what is happening. Feeds Tamper, in feeds_tamper/plugins/rewrite.inc line 45 calls strtr(). strtr() is a function defined in php for string translation with two implementations. The implementation used by Feeds Tamper is strtr( string, array). The array is a set of Key Value pairs where the key is the string to replace and the value is the string to replace with. The reason we are getting this error is that the FeedsDateTime objects don't have a __toString() method because DateTime does not define a __toString().

There are a couple ways to fix this.
1: Toss some code into rewrite.inc to handle this case.
Replace

foreach ($item as $key => $value) {
  $trans['[' . $key . ']'] = is_array($value) ? reset($value) : $value;
}

With

foreach ($item as $key => $value) {
  if ($value instanceof DateTime) {
    $value = $value->format('c'); // Make it a string so we don't have a php error.
  }
  $trans['[' . $key . ']'] = is_array($value) ? reset($value) : $value;
}

The advantages of this are that it only effects Feeds Tamper and any tampered FeedsDateTime rewrites.

2: Extend FeedsDateTime in the Feeds module to have a __toString method.
Add the following to the class definition for FeedsDateTime located on line 559 in /sites/all/modules/feeds/plugins/FeedsParser.inc

/**
 * Helper function to render the object as a string if this becomes necessary
 */
public function __toString(){
  return $this->format('c');
}

This has the benefit of fixing this issue in any module that blindly assumes FeedsDateTime objects can be handled as strings.
The biggest concern with this fix is that we are modifying a class to have a new method and this could adversely affect code that assumed FeedsDateTime objects do not have a __toString() method.
This would break if you ever updated to a newer version of Feeds if the module maintainer has not added this 'fix' to the Feeds module.

3: Write a custom module that extends FeedsDateTime class to include out __toString() method.
I haven't explored this yet, but this would be the cleanest of the solutions. I'm not familiar enough with php and drupal at this time to feel comfortably dropping untested code here for a custom module. If I end up rolling this solution I'll come back and post it for completion purposes...

Twistor, I'm curious to see what you think the correct solution would be to this issue.

twistor’s picture

You're on the right track. The __toString() method is most of the solution. I am more curious about what module/parser is puttin DateTime objects in there like that, so I can track down the fix more correctly.

RedEight’s picture

I'm pretty sure that would be the Date iCal module. https://www.drupal.org/project/date_ical

I know I'm using it and, judging by the reference to iCals in the original post, I think it's safe to assume that rudeboypeter was also using it.

twistor’s picture

Title: RecoverableFatal error on rewrite plugin » Make FeedsDateTime stringable.
Project: Feeds Tamper » Feeds
Version: 7.x-1.x-dev » 7.x-2.x-dev
Component: Plugins » Code
Assigned: Unassigned » twistor
Category: Bug report » Feature request
Status: Postponed (maintainer needs more info) » Active

FeedsDateTime needs __toString() method. We can't simply extend FeedsElemement since it already extends DateTime. Blech, this is crappy.

The iCal parser should be returning FeedsDateTimeElement, not FeedsDateTime, but that doesn't do the right thing either. We should store the original un-modified string to use in the __toString() method, rather than some generic conversion.

twistor’s picture

Status: Active » Needs review
Issue tags: +Needs tests
FileSize
2.96 KB

Maybe we should just always return a formatted date?

I'm not sure.

twistor’s picture

The last submitted patch, 6: feeds-datetime-stringable-2225019-6.patch, failed testing.

  • twistor committed 96e5089 on 7.x-2.x
    Issue #2225019 by twistor: Make FeedsDateTime stringable
    
twistor’s picture

Assigned: twistor » Unassigned
Status: Needs review » Fixed
Issue tags: -Needs tests

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.