<p>I would say you can do this yeah, it just take a little figuring out is all.</p>

<p>Your input template could look something like:</p>

<pre><code>&lt;input type="text" name="{{ name }}[title]" value="{{ value.title is defined ? value.title }}"&gt;
&lt;input type="text" name="{{ name }}[description]" value="{{ value.description is defined ? value.description }}"&gt;
&lt;input type="text" name="{{ name }}[caption]" value="{{ value.caption is defined ? value.caption }}"&gt;
&lt;input type="text" name="{{ name }}[thumb]" value="{{ value.thumb is defined ? value.thumb }}"&gt;
</code></pre>

<p>Then in your fieldtypes main class, something like:</p>

<pre><code>public function getInputHtml($name, $value)
{

    // If value is null, we set it to an array to prevent template errors
    if(!$value)
    {
      $value = array();
    }

    return craft()-&gt;templates-&gt;render('bosh/input', array(
        'name'  =&gt; $name,
        'value' =&gt; $value
    ));
}

public function prepValueFromPost($value)
{   
    return json_encode($value);
}

public function prepValue($value)
{
    return json_decode($value, TRUE);
}
</code></pre>

<p>So basically you are settings the fieldtype value to an array of <code>pluginHandle[field]</code> which then you <code>json_encode</code> so it can all go into database without any issues.</p>

<p>Then when you retrieve the value from the DB, its a string, so you <code>json_decode</code> it into an array. The <code>TRUE</code> part states you want an associative array, rather than a <code>stdClass</code> object.</p>

<p>Thats pretty much it at a basic level :)</p>

CC BY-SA 3.0
2015-07-16T11:37:36.737Z
thanks for the heads up. Thus works to some extent i.e. I get the field type option in the list and can add it to the entry types field layout. However I get an error locating the template file. Unable to find the template “bosh/input“ This sits in the plugin folder under templates/bosh/input.html any ideas?
Lettie
CC BY-SA 3.0
2015-07-16T11:39:37.543Z
yeah sorry bosh is just a silly name I gave to my test plugin. You just need to add the template  (jn your plugin directory) like `templates/input` and then call it like `pluginHandle/input`
Alec Ritson
CC BY-SA 3.0
2015-07-16T11:41:23.163Z
ignore that last comment think I've solved it! Cheers
Lettie
CC BY-SA 3.0
2015-07-16T12:21:32.877Z
getting a slight error on the value fields any clue how to fix it? Impossible to access an attribute ("title") on a NULL variable ("")
Lettie
CC BY-SA 3.0
2015-07-16T13:17:04.113Z
Yeah it looks like its because `$value` is null when it gets sent to the template, i've updated the `getInputHtml` method in my answer to show how you could prevent this.
Alec Ritson
CC BY-SA 3.0
2015-07-16T14:13:20.297Z
Cheers @AlexRitson just made a small change to the input fields as it was still throwing an error as the array was empty. All working ok now. One more question if I wanted to add an asset field for the thumb is this possible to create as a field in the plugin template?
Lettie
CC BY-SA 3.0
2015-07-16T14:16:01.077Z
Oh shoot yeah, you should only have needed to add `value.title is defined` on the title call. I assume it is possible, but that might be worth its own question or someone might have posted something? personally i've not tried this.
Alec Ritson
CC BY-SA 3.0
2015-07-16T14:36:13.120Z
@AlexRitson Ok will have a look around. Thanks for all your help.
Lettie
CC BY-SA 3.0
2015-07-16T14:39:53.070Z
Not a problem :)
Alec Ritson
CC BY-SA 3.0
2015-07-16T17:46:19.177Z
@AlecRitson I fixed the edit according to your suggestion, I agree that should resolve the issue. I also removed the `: null`, because [Twig has awesome ternary variations](http://twig.sensiolabs.org/doc/templates.html#other-operators), and doesn't require that part.
Lindsey D
CC BY-SA 3.0
2015-07-16T18:23:51.120Z
Ah sweet thanks for the edit! I didn't realise twig was that forgiving :)
Alec Ritson
CC BY-SA 3.0
2015-07-16T11:17:29.113Z
2015-07-16T17:42:16.420Z
2015-07-16T17:42:16.420Z
Alec Ritson
<p>Ok scenario that I'd like to create.
Create a custom field type lets call it videos. In this field type you would have 5 separate fields title, description, caption, link, thumb. This would make up the field type which could then be selected from the field type when creating a new field in the CP.</p>

<p>The objective is that this grouped field type could then be re-used within separate matrix fields or as a field in it's own right.</p>

<p>I know you can create field types for plugins but is there a way to create the above?</p>

<field><fieldtypes><custom-fieldtype><custom-field>
Custom field type that has multiple fields to extend the default field types
# Custom field type that has multiple fields to extend the default field types

(Asked by: username_0 on 2015-07-16)

Ok scenario that I'd like to create.
Create a custom field type lets call it videos. In this field type you would have 5 separate fields title, description, caption, link, thumb. This would make up the field type which could then be selected from the field type when creating a new field in the CP.

The objective is that this grouped field type could then be re-used within separate matrix fields or as a field in it's own right.

I know you can create field types for plugins but is there a way to create the above?

**Tags:** field, fieldtypes, custom-fieldtype, custom-field

---

## 1 answers

### Answer by username_1 on 2015-07-16. Score: 3 | Accepted answer

I would say you can do this yeah, it just take a little figuring out is all.
Your input template could look something like:





Then in your fieldtypes main class, something like:
public function getInputHtml($name, $value)
{

    // If value is null, we set it to an array to prevent template errors
    if(!$value)
    {
      $value = array();
    }

    return craft()->templates->render('bosh/input', array(
        'name'  => $name,
        'value' => $value
    ));
}

public function prepValueFromPost($value)
{   
    return json_encode($value);
}

public function prepValue($value)
{
    return json_decode($value, TRUE);
}

So basically you are settings the fieldtype value to an array of pluginHandle[field] which then you json_encode so it can all go into database without any issues.
Then when you retrieve the value from the DB, its a string, so you json_decode it into an array. The TRUE part states you want an associative array, rather than a stdClass object.
Thats pretty much it at a basic level :)

> **Comments:**
> **username_0**: thanks for the heads up. Thus works to some extent i.e. I get the field type option in the list and can add it to the entry types field layout. However I get an error locating the template file. Unable to find the template “bosh/input“ This sits in the plugin folder under templates/bosh/input.html any ideas?  
> **username_1**: yeah sorry bosh is just a silly name I gave to my test plugin. You just need to add the template  (jn your plugin directory) like `templates/input` and then call it like `pluginHandle/input`  
> **username_0**: ignore that last comment think I've solved it! Cheers  
> **username_0**: getting a slight error on the value fields any clue how to fix it? Impossible to access an attribute ("title") on a NULL variable ("")  
> **username_1**: Yeah it looks like its because `$value` is null when it gets sent to the template, i've updated the `getInputHtml` method in my answer to show how you could prevent this.  
> **username_0**: Cheers @AlexRitson just made a small change to the input fields as it was still throwing an error as the array was empty. All working ok now. One more question if I wanted to add an asset field for the thumb is this possible to create as a field in the plugin template?  
> **username_1**: Oh shoot yeah, you should only have needed to add `value.title is defined` on the title call. I assume it is possible, but that might be worth its own question or someone might have posted something? personally i've not tried this.  
> **username_0**: @AlexRitson Ok will have a look around. Thanks for all your help.  
> **username_1**: Not a problem :)  
> **username_2**: @AlecRitson I fixed the edit according to your suggestion, I agree that should resolve the issue. I also removed the `: null`, because [Twig has awesome ternary variations](http://twig.sensiolabs.org/doc/templates.html#other-operators), and doesn't require that part.  
> **username_1**: Ah sweet thanks for the edit! I didn't realise twig was that forgiving :)  

---

