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

<p>If your field type doesn’t override <code>getSearchKeywords()</code>, 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.</p>

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

CC BY-SA 3.0
2014-08-14T21:19:52.140Z
That worked, I knew it was something like that just couldn't figure out where it was.
Justin Kimbrell
CC BY-SA 3.0
2015-07-20T17:40:17.820Z
Ah, this was driving me crazy. getSearchKeywords() really should be documented in the Field Types documentation. :)
André Elvan
CC BY-SA 3.0
2014-08-14T19:47:38.263Z
2014-08-14T19:47:38.263Z
Brandon Kelly
<p>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.</p>

<p>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 <code>onAfterElementSave()</code> 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.</p>

<p>Here is my <code>prepValue()</code> code in my FT. </p>

<pre><code>public function prepValue($value)
{
    $value = json_decode($value);

    return new MyPlugin_MyModelNameModel((array) $value);
}
</code></pre>

<p>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 <code>prepValue()</code> method as a string, it works fine when saving but then break in the template because it's expecting to be a model.</p>

<p>I even added a <code>__toString()</code> 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.</p>

<p>Here is the stack trace from the craft.log</p>

<pre><code>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-&gt;getSearchKeywords()
#2 /Users/justinkimbrell/Sites/craft_dev/craft/app/services/ContentService.php(137): Craft\ContentService-&gt;_updateSearchIndexes()
#3 /Users/justinkimbrell/Sites/craft_dev/craft/app/services/ElementsService.php(898): Craft\ContentService-&gt;saveContent()
#4 /Users/justinkimbrell/Sites/craft_dev/craft/app/services/EntriesService.php(141): Craft\ElementsService-&gt;saveElement()
#5 /Users/justinkimbrell/Sites/craft_dev/craft/app/controllers/EntriesController.php(440): Craft\EntriesService-&gt;saveEntry()
#6 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/actions/CInlineAction.php(49): Craft\EntriesController-&gt;actionSaveEntry()
#7 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/CController.php(308): CInlineAction-&gt;runWithParams()
#8 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/CController.php(286): Craft\EntriesController-&gt;runAction()
#9 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/CController.php(265): Craft\EntriesController-&gt;runActionWithFilters()
#10 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/web/CWebApplication.php(282): Craft\EntriesController-&gt;run()
#11 /Users/justinkimbrell/Sites/craft_dev/craft/app/etc/web/WebApp.php(865): Craft\WebApp-&gt;runController()
#12 /Users/justinkimbrell/Sites/craft_dev/craft/app/etc/web/WebApp.php(254): Craft\WebApp-&gt;_processActionRequest()
#13 /Users/justinkimbrell/Sites/craft_dev/craft/app/framework/base/CApplication.php(180): Craft\WebApp-&gt;processRequest()
#14 /Users/justinkimbrell/Sites/craft_dev/craft/app/index.php(196): Craft\WebApp-&gt;run()
#15 /Users/justinkimbrell/Sites/craft_dev/public/index.php(14): require_once()
REQUEST_URI=/index.php/admin/entries/news/47

******************************************************************************************************
</code></pre>

<p>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.</p>

CC BY-SA 3.0
2014-08-14T19:00:06.377Z
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
CC BY-SA 3.0
2014-08-14T19:30:08.757Z
Just updated the post with the stack trace.
Justin Kimbrell
<plugin-development><custom-fieldtype>
What is the expected data type to be returned when using the prepValue() method?
# What is the expected data type to be returned when using the prepValue() method?

(Asked by: username_0 on 2014-08-14)

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.

**Tags:** plugin-development, custom-fieldtype

> **Comments:**
> **username_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.  
> **username_0**: Just updated the post with the stack trace.  

---

## 1 answers

### Answer by username_1 on 2014-08-14. Score: 3 | Accepted answer

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.

> **Comments:**
> **username_0**: That worked, I knew it was something like that just couldn't figure out where it was.  
> **username_2**: Ah, this was driving me crazy. getSearchKeywords() really should be documented in the Field Types documentation. :)  

---

