Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T14:44:46.127Z
Answers.Text: 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`
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T14:04:14.487Z
Answers.LastActivityDate: 2014-06-19T14:04:14.487Z
Answers.UserDisplayName: Bryan Redeagle
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T14:56:42.263Z
Answers.Text: Great thanks Selvin! Where would the field model live? In the actual models directory or in the fieldtypes one?
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T15:01:05.810Z
Answers.Text: In the models directory so that craft can autoload it for you.
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T15:01:13.830Z
Answers.Text: 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?
Answers.UserDisplayName: Bryan Redeagle
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T15:02:12.773Z
Answers.Text: Thanks @SelvinOrtiz - I'll have a look tomorrow and let you know how I get on!
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T15:04:28.643Z
Answers.Text: @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.
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-23T12:33:35.870Z
Answers.Text: @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...
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-23T12:59:21.710Z
Answers.Text: FYI I've been going for the `{{ entry.myField.settings.maxNumber }}` route.
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-23T15:16:30.110Z
Answers.Text: 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.
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-23T15:25:14.157Z
Answers.Text: I'll run through a few tests with a dummy fieldtype right now to ensure that we solve this in the cleanest way possible.
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-23T17:00:43.883Z
Answers.Text: Hey @joshangell,
  I just updated my answer with a more verbose code/workflow example. Let me know if it helps!
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T07:17:41.090Z
Answers.Text: 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...
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T07:27:34.447Z
Answers.Text: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/15295/discussion-between-joshangell-and-selvin-ortiz).
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-18T10:31:47.077Z
Answers.Text: 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.
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-20T22:23:47.233Z
Answers.Text: 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.
Answers.UserDisplayName: Selvin Ortiz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T14:51:49.350Z
Answers.LastActivityDate: 2014-06-23T17:00:17.993Z
Answers.LastEditDate: 2014-06-23T17:00:17.993Z
Answers.UserDisplayName: Selvin Ortiz
Body: <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>
  
Tags: <plugin-development><fieldtypes>
Title: When creating a fieldtype how do I provide methods to be used in the templates?
ThreadText: # 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 @username_1 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:**
  
  ```php
  // MyFieldType.php
  public function prepValue($value)
  {
      $model = new MyFieldModel;
  
      $model->setAttribute('value', $value);
      $model->setMaxNumber($this->getSettings()->getAttribute('maxNumber'));
  
      return $model;
  }
  
  ```
  
  ```php
  // 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,
      );
  }
  
  ```
  
  ```twig
  // _entry.html or other template name
  {{ entry.myFieldHandle.value }}
  {{ entry.myFieldHandle.maxNumber }}
  
  ```
  
  [@username_2](http://twitter.com/username_2 "@username_2")
  
  > **Comments:**
  > **username_0**: Great thanks username_2! 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. @username_2, 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 @username_2 - I'll have a look tomorrow and let you know how I get on!  
  > **username_2**: @username_1 if `__toString()` is a concern then @username_0 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**: @username_2 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 @username_0, 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-username_0-and-username_2-username_2).  
  > **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 username_0, 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`  
  
  ---
  
  