3

So while building a field type (FT), what is the expected data type of the returned value from the prepValue method? I am not sure I am using this method right, as I am running into a PHP error when trying to save the FT.

My FT stores data in two ways, the first as a JSON string stored in the element content table. When my FT is saved, I used the onAfterElementSave() method for additional processing. During this method, I am creating rows in a db table created by my plugin. What I desire to happen is when I call my field from the template on the front-end, it returns a data model which I can then pass to other methods for further manipulation (in the template). All this works great.

Here is my prepValue() code in my FT.

public function prepValue($value)
{
    $value = json_decode($value);

    return new MyPlugin_MyModelNameModel((array) $value);
}

All this works fine until I go to save my entry, which is producing this error "Internal Server Error - Object of class stdClass could not be converted to string". When I return the data from the prepValue() method as a string, it works fine when saving but then break in the template because it's expecting to be a model.

I even added a __toString() method to my model in hopes that if the data needs to be a string, my model could dynamically pass the JSON back as a string but that isn't working either.

Here is the stack trace from the craft.log

2014/08/14 19:28:35 [error] [php] Object of class stdClass could not be converted to string (/Users/justinkimbrell/Sites/craft_dev/craft/app/helpers/StringHelper.php:61)
Stack trace:
#0 /Users/justinkimbrell/Sites/craft_dev/craft/app/fieldtypes/BaseFieldType.php(148): arrayToString()
#1 /Users/justinkimbrell/Sites/craft_dev/craft/app/services/ContentService.php(366): Craft\GoogleMaps_GoogleMapFieldType->getSearchKeywords()
#2 /Users/justinkimbrell/Sites/craft_dev/craft/app/services/ContentService.php(137): Craft\ContentService->_updateSearchIndexes()
#3 /Users/justinkimbrell/Sites/craft_dev/craft/app/services/ElementsService.php(898): Craft\ContentService->saveContent()
#4 /Users/justinkimbrell/Sites/craft_dev/craft/app/services/EntriesService.php(141): Craft\ElementsService->saveElement()
#5 /Users/justinkimbrell/Sites/craft_dev/craft/app/controllers/EntriesController.php(440): Craft\EntriesService->saveEntry()
#6 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/actions/CInlineAction.php(49): Craft\EntriesController->actionSaveEntry()
#7 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/CController.php(308): CInlineAction->runWithParams()
#8 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/CController.php(286): Craft\EntriesController->runAction()
#9 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/CController.php(265): Craft\EntriesController->runActionWithFilters()
#10 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/CWebApplication.php(282): Craft\EntriesController->run()
#11 /Users/justinkimbrell/Sites/craft_dev/craft/app/etc/web/WebApp.php(865): Craft\WebApp->runController()
#12 /Users/justinkimbrell/Sites/craft_dev/craft/app/etc/web/WebApp.php(254): Craft\WebApp->_processActionRequest()
#13 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/base/CApplication.php(180): Craft\WebApp->processRequest()
#14 /Users/justinkimbrell/Sites/craft_dev/craft/app/index.php(196): Craft\WebApp->run()
#15 /Users/justinkimbrell/Sites/craft_dev/public/index.php(14): require_once()
REQUEST_URI=/index.php/admin/entries/news/47

******************************************************************************************************

Hopefully the issue is clear. I am just unsure of how to properly use this method, and wondering how I can return data in a model and not as a string.

  • 1
    After getting the error, check your craft/storage/runtime/logs/craft.log file and search for [error]. When you find the log corresponding to the error you’re getting, please find the stack trace right below it and post it here, so we can see when the error is actually occurring. – Brandon Kelly Aug 14 '14 at 19:00
  • Just updated the post with the stack trace. – Justin Kimbrell Aug 14 '14 at 19:30
3

Looks like the error is occurring when your field type’s getSearchKeywords() function is called. The value that’s passed into that function will have already been run through prepValue(), so at that point it looks like it will be a MyPlugin_MyModelNameModel instance (whatever the actual class name is).

If your field type doesn’t override getSearchKeywords(), the BaseFieldType version will just pass the value off to StringHelper::arrayToString(), which will do its best to convert whatever variable it was passed into a string. Something in there must be tripping it up.

Your best bet here is probably to just override getSearchKeywords() in your field type. It should be returning a space-delimited list of keywords you want to be stored for the current field value.

| improve this answer | |
  • That worked, I knew it was something like that just couldn't figure out where it was. – Justin Kimbrell Aug 14 '14 at 21:19
  • Ah, this was driving me crazy. getSearchKeywords() really should be documented in the Field Types documentation. :) – André Elvan Jul 20 '15 at 17:40

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.