<p>To do this as you describe, you would need to add the <a href="http://buildwithcraft.com/docs/plugins/field-types#modifying-your-fieldtypes-stored-data-for-use" rel="nofollow">prepValue</a> function to your field type. You would then need to create a PHP class to fill with your field value and settings.</p>

<p>I'm not sure that it would work, but I also suggest adding the __invoke() method. You could use it to return the field value when you use the object as a variable (though Twig would call it as a method I think).</p>

CC BY-SA 3.0
2014-06-19T14:44:46.127Z
Could you expand a bit on how the php class part would work? I did end up trying that and basing my code of the OptionsData class but couldn't for the life of me work out how to use it - Yii didn't seem to know about my class when I called it like so `new MyPlugin_FieldTypeData`
Josh Angell
CC BY-SA 3.0
2014-06-19T14:04:14.487Z
2014-06-19T14:04:14.487Z
Bryan Redeagle
<p>Like plugins, fieldtypes can have their own settings as you're aware but while you can grab your plugin settings from pretty much anywhere, fieldtypes are a little more contextual.</p>

<p>In any case, like @Bryan pointed out, your fieldtype needs to implement <code>prepValue($value)</code> and ideally return a model containing the value as defined in <code>defineAttributes()</code> and the <code>maxNumber</code> as a property.</p>

<p>This basically means that when Craft calls your fieldtype's <code>prepValue($value)</code> which it does when the value of your field is needed (editing/outputting) you can return a model with the <code>value</code> and <code>maxNumber</code>.</p>

<hr>

<p><strong>EDIT:</strong></p>

<pre class="lang-php prettyprint-override"><code>// MyFieldType.php
public function prepValue($value)
{
    $model = new MyFieldModel;

    $model-&gt;setAttribute('value', $value);
    $model-&gt;setMaxNumber($this-&gt;getSettings()-&gt;getAttribute('maxNumber'));

    return $model;
}
</code></pre>

<pre class="lang-php prettyprint-override"><code>// MyFieldModel.php
// Explicit getter/setter for $maxNumber rather than defining it as an attribute
protected $maxNumber;

public function setMaxNumber($value)
{
    $this-&gt;maxNumber = $value;
}

public function getMaxNumber()
{
    return $this-&gt;maxNumber;
}

public function defineAttributes()
{
    return array(
        'value' =&gt; AttributeType::Number,
    );
}
</code></pre>

<pre class="lang-twig prettyprint-override"><code>// _entry.html or other template name
{{ entry.myFieldHandle.value }}
{{ entry.myFieldHandle.maxNumber }}
</code></pre>

<p><a href="http://twitter.com/selvinortiz" rel="nofollow" title="@selvinortiz">@selvinortiz</a></p>

CC BY-SA 3.0
2014-06-19T14:56:42.263Z
Great thanks Selvin! Where would the field model live? In the actual models directory or in the fieldtypes one?
Josh Angell
CC BY-SA 3.0
2014-06-19T15:01:05.810Z
In the models directory so that craft can autoload it for you.
Selvin Ortiz
CC BY-SA 3.0
2014-06-19T15:01:13.830Z
It would be in models. @Selvin, I would be concerned about not returning a number with the __toString. Wouldn't __invoke be preferred so you can return it as the right type?
Bryan Redeagle
CC BY-SA 3.0
2014-06-19T15:02:12.773Z
Thanks @SelvinOrtiz - I'll have a look tomorrow and let you know how I get on!
Josh Angell
CC BY-SA 3.0
2014-06-19T15:04:28.643Z
@Bryan if `__toString()` is a concern then @Josh can implement a `getValue()` method on the model and use it as {{ entry.myField.value }} which I would prefer over `__invoke()` since there is enough magic happening in Yii already.
Selvin Ortiz
CC BY-SA 3.0
2014-06-23T12:33:35.870Z
@SelvinOrtiz I've tried your method today but I'm getting `Object of class Craft\MyFieldModel could not be converted to int` - is this when I need the `getValue()` thing? If so, how do I use it? I've tried a few random stabs but no luck...
Josh Angell
CC BY-SA 3.0
2014-06-23T12:59:21.710Z
FYI I've been going for the `{{ entry.myField.settings.maxNumber }}` route.
Josh Angell
CC BY-SA 3.0
2014-06-23T15:16:30.110Z
If you've implemented it the way I outlined it in my answer and want to get maxNumber like this `{{ entry.myField.settings.maxNumber }}` then you can implement a `getMaxNumber()` on the model that returns that value and Yii should take care of the rest.
Selvin Ortiz
CC BY-SA 3.0
2014-06-23T15:25:14.157Z
I'll run through a few tests with a dummy fieldtype right now to ensure that we solve this in the cleanest way possible.
Selvin Ortiz
CC BY-SA 3.0
2014-06-23T17:00:43.883Z
Hey @joshangell,
I just updated my answer with a more verbose code/workflow example. Let me know if it helps!
Selvin Ortiz
CC BY-SA 3.0
2014-06-24T07:17:41.090Z
Thanks for this man - that does work but now if I just try `{{ entry.myFieldHandle }}` I get a bunch of crazy output: http://cl.ly/image/1u0T2M3a3U24.

`{{ entry.myFieldHandle.value }}` works though. I'm assuming this is the part where I need a `__toString()` or something? If I do implement one of those on my model I get the `could not be converted to int` error...
Josh Angell
CC BY-SA 3.0
2014-06-24T07:27:34.447Z
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/15295/discussion-between-joshangell-and-selvin-ortiz).
Josh Angell
CC BY-SA 3.0
2014-07-18T10:31:47.077Z
Ok I'm just going to mark this as correct even though I still haven't solved it - I think there must be something else going on in my plugin preventing `__toString()` from working as expected.
Josh Angell
CC BY-SA 3.0
2014-07-20T22:23:47.233Z
Hey Josh, sorry I didn't get to this sooner. I'd love to take a look at your code (if you can share it) and see how I can help you solve this for good. Feel free to reach me over hangout if you see me online or email me.
Selvin Ortiz
CC BY-SA 3.0
2014-06-19T14:51:49.350Z
2014-06-23T17:00:17.993Z
2014-06-23T17:00:17.993Z
Selvin Ortiz
<p>I have created a fieldtype that saves a single integer to the database, it has some settings that defines the maximum value that integer can be:</p>

<pre><code>protected function defineSettings()
{
  return array(
    'maxNumber' =&gt; array(AttributeType::Number, 'min' =&gt; 2, 'default' =&gt; 12)
  );
}
</code></pre>

<p>What I want to do when outputting my field in the templates is something like this:</p>

<pre><code>The value is {{ entry.myField }} of {{ entry.myField.maxNumber }}
</code></pre>

<p>Anyone have any ideas? Do I need to create a data class like the options-based fields use or something? I'm just stabbing around in the dark here...</p>

<plugin-development><fieldtypes>
When creating a fieldtype how do I provide methods to be used in the templates?
# When creating a fieldtype how do I provide methods to be used in the templates?

(Asked by: username_0 on 2014-06-19)

I have created a fieldtype that saves a single integer to the database, it has some settings that defines the maximum value that integer can be:
protected function defineSettings()
{
  return array(
    'maxNumber' => array(AttributeType::Number, 'min' => 2, 'default' => 12)
  );
}

What I want to do when outputting my field in the templates is something like this:
The value is {{ entry.myField }} of {{ entry.myField.maxNumber }}

Anyone have any ideas? Do I need to create a data class like the options-based fields use or something? I'm just stabbing around in the dark here...

**Tags:** plugin-development, fieldtypes


---

## 2 answers

### Answer by username_2 on 2014-06-19. Score: 7 | Accepted answer

Like plugins, fieldtypes can have their own settings as you're aware but while you can grab your plugin settings from pretty much anywhere, fieldtypes are a little more contextual.
In any case, like @Bryan pointed out, your fieldtype needs to implement prepValue($value) and ideally return a model containing the value as defined in defineAttributes() and the maxNumber as a property.
This basically means that when Craft calls your fieldtype's prepValue($value) which it does when the value of your field is needed (editing/outputting) you can return a model with the value and maxNumber.

EDIT:
// MyFieldType.php
public function prepValue($value)
{
    $model = new MyFieldModel;

    $model->setAttribute('value', $value);
    $model->setMaxNumber($this->getSettings()->getAttribute('maxNumber'));

    return $model;
}

// MyFieldModel.php
// Explicit getter/setter for $maxNumber rather than defining it as an attribute
protected $maxNumber;

public function setMaxNumber($value)
{
    $this->maxNumber = $value;
}

public function getMaxNumber()
{
    return $this->maxNumber;
}

public function defineAttributes()
{
    return array(
        'value' => AttributeType::Number,
    );
}

// _entry.html or other template name
{{ entry.myFieldHandle.value }}
{{ entry.myFieldHandle.maxNumber }}

@selvinortiz

> **Comments:**
> **username_0**: Great thanks Selvin! Where would the field model live? In the actual models directory or in the fieldtypes one?  
> **username_2**: In the models directory so that craft can autoload it for you.  
> **username_1**: It would be in models. @Selvin, I would be concerned about not returning a number with the __toString. Wouldn't __invoke be preferred so you can return it as the right type?  
> **username_0**: Thanks @SelvinOrtiz - I'll have a look tomorrow and let you know how I get on!  
> **username_2**: @Bryan if `__toString()` is a concern then @Josh can implement a `getValue()` method on the model and use it as {{ entry.myField.value }} which I would prefer over `__invoke()` since there is enough magic happening in Yii already.  
> **username_0**: @SelvinOrtiz I've tried your method today but I'm getting `Object of class Craft\MyFieldModel could not be converted to int` - is this when I need the `getValue()` thing? If so, how do I use it? I've tried a few random stabs but no luck...  
> **username_0**: FYI I've been going for the `{{ entry.myField.settings.maxNumber }}` route.  
> **username_2**: If you've implemented it the way I outlined it in my answer and want to get maxNumber like this `{{ entry.myField.settings.maxNumber }}` then you can implement a `getMaxNumber()` on the model that returns that value and Yii should take care of the rest.  
> **username_2**: I'll run through a few tests with a dummy fieldtype right now to ensure that we solve this in the cleanest way possible.  
> **username_2**: Hey @joshangell, I just updated my answer with a more verbose code/workflow example. Let me know if it helps!  
> **username_0**: Thanks for this man - that does work but now if I just try `{{ entry.myFieldHandle }}` I get a bunch of crazy output: http://cl.ly/image/1u0T2M3a3U24.  `{{ entry.myFieldHandle.value }}` works though. I'm assuming this is the part where I need a `__toString()` or something? If I do implement one of those on my model I get the `could not be converted to int` error...  
> **username_0**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/15295/discussion-between-joshangell-and-selvin-ortiz).  
> **username_0**: Ok I'm just going to mark this as correct even though I still haven't solved it - I think there must be something else going on in my plugin preventing `__toString()` from working as expected.  
> **username_2**: Hey Josh, sorry I didn't get to this sooner. I'd love to take a look at your code (if you can share it) and see how I can help you solve this for good. Feel free to reach me over hangout if you see me online or email me.  

---

### Answer by username_1 on 2014-06-19. Score: 1

To do this as you describe, you would need to add the [prepValue](http://buildwithcraft.com/docs/plugins/field-types#modifying-your-fieldtypes-stored-data-for-use) function to your field type. You would then need to create a PHP class to fill with your field value and settings.

I'm not sure that it would work, but I also suggest adding the \_\_invoke() method. You could use it to return the field value when you use the object as a variable (though Twig would call it as a method I think).

> **Comments:**
> **username_0**: Could you expand a bit on how the php class part would work? I did end up trying that and basing my code of the OptionsData class but couldn't for the life of me work out how to use it - Yii didn't seem to know about my class when I called it like so `new MyPlugin_FieldTypeData`  

---

