<p><code>entry.myFieldHandle</code> will return whatever the field type’s <a href="https://craftcms.com/classreference/fieldtypes/IFieldType#prepValue-detail" rel="nofollow">prepValue()</a> function returns.</p>

<p>If you <em>always</em> want that to be HTML, then prepValue() can just return the HTML as a string or \Twig_Markup object via <a href="https://craftcms.com/classreference/helpers/TemplateHelper#getRaw-detail" rel="nofollow">TemplateHelper::getRaw()</a> so devs don't have to type <code>|raw</code> when outputting the value.</p>

<p>If you want it to be an object that supports sub-properties and methods, then have prepValue() return the object. Give the class a <code>__toString()</code> method, which defines the string that should be returned when the object is treated like a string. Behind the scenes, <code>__toString()</code> is what will be called when a template has <code>{{ entry.myFieldHandle }}</code>. But note that this is a PHP construct and PHP mandates that the method returns an actual string, so you can’t return a \Twig_Markup object here. Devs will have to type <code>{{ entry.myFieldHandle|raw }}</code> to prevent Twig from escaping the HTML.</p>

CC BY-SA 3.0
2016-08-14T19:18:59.090Z
Yeah, I was hoping that Craft did some magic with the result of the toString method, but it looks like I'll just have to put what I wanted to do into a method on the object that's returned. Thanks for the clarification!
Taylor Daughtry
CC BY-SA 3.0
2016-08-14T17:24:27.330Z
2016-08-14T17:24:27.330Z
Brandon Kelly
<p>So, I'm in the process of building a fieldtype, and I'd like to output an HTML element and some javascript when a developer calls my plugin like this:</p>

<pre><code>{{ entry.fieldtypeHandle }}
</code></pre>

<p>The field returns an object with other methods/properties on it, but I'd like to specifically provide something different when a developer calls my fieldtype without any other chaining.</p>

<p>I've tried pass a class with a <code>__toString()</code> method, but it just returns a string. (I don't know what I expected, honestly.) I also know that coupling the HTML output with the class <em>seems</em> a bit dirty, even if that did work.</p>

<p>Is this possible?</p>

<plugin-development><custom-fieldtype>
How can I specify what happens when someone calls my fieldtype without any further dot chains?
# How can I specify what happens when someone calls my fieldtype without any further dot chains?

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

So, I'm in the process of building a fieldtype, and I'd like to output an HTML element and some javascript when a developer calls my plugin like this:
{{ entry.fieldtypeHandle }}

The field returns an object with other methods/properties on it, but I'd like to specifically provide something different when a developer calls my fieldtype without any other chaining.
I've tried pass a class with a __toString() method, but it just returns a string. (I don't know what I expected, honestly.) I also know that coupling the HTML output with the class seems a bit dirty, even if that did work.
Is this possible?

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


---

## 1 answers

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

`entry.myFieldHandle` will return whatever the field type’s [prepValue()](https://craftcms.com/classreference/fieldtypes/IFieldType#prepValue-detail) function returns.

If you *always* want that to be HTML, then prepValue() can just return the HTML as a string or \Twig\_Markup object via [TemplateHelper::getRaw()](https://craftcms.com/classreference/helpers/TemplateHelper#getRaw-detail) so devs don't have to type `|raw` when outputting the value.

If you want it to be an object that supports sub-properties and methods, then have prepValue() return the object. Give the class a `__toString()` method, which defines the string that should be returned when the object is treated like a string. Behind the scenes, `__toString()` is what will be called when a template has `{{ entry.myFieldHandle }}`. But note that this is a PHP construct and PHP mandates that the method returns an actual string, so you can’t return a \Twig\_Markup object here. Devs will have to type `{{ entry.myFieldHandle|raw }}` to prevent Twig from escaping the HTML.

> **Comments:**
> **username_0**: Yeah, I was hoping that Craft did some magic with the result of the toString method, but it looks like I'll just have to put what I wanted to do into a method on the object that's returned. Thanks for the clarification!  

---

