I have a plugin that creates custom field types to extend crafts default field types.
The custom field types hold several different form input fields. For example the field may be called video and contain a title field, caption field, video slug field and a thumbnail field.
When the plugin is installed these fields appear in the default field dropdown list.
I now want to change the html template in the plugin so that the thumbnail field makes use of the default assets field built into craft. So I'd like to know if there is a way to add an asset field to the twig template in my plugin. The plugin does not need to use it's own database so can make use of the existing assets database.
Is this possible?
This is what I have so far.
In the fieldtype class:
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('customfieldtypes/video', array(
'name' => $name,
'value' => $value
));
}
public function prepValueFromPost($value)
{
return json_encode($value);
}
public function prepValue($value)
{
return json_decode($value, TRUE);
}
In the video template:
<label for="{{ name }}[title]">Title</label>
<input class="text nicetext fullwidth" type="text" name="{{ name }}[title]" value="{% if value %}{{ value.title }}{% else %}{{ null }}{% endif %}"><br />
<label for="{{ name }}[description]">Description</label>
<textarea class="text nicetext fullwidth" type="text" name="{{ name }}[description]">{% if value %}{{ value.description }}{% else %}{{ null }}{% endif %}</textarea><br />
<label for="{{ name }}[caption]">Caption</label>
<input class="text nicetext fullwidth" type="text" name="{{ name }}[caption]" value="{% if value %}{{ value.caption }}{% else %}{{ null }}{% endif %}"><br />
<label for="{{ name }}[link]">Link</label>
<input class="text nicetext fullwidth" type="text" name="{{ name }}[link]" value="{% if value %}{{ value.link }}{% else %}{{ null }}{% endif %}"><br />
<label for="{{ name }}[thumb]">Thumb</label>
<input class="text nicetext fullwidth" type="text" name="{{ name }}[thumb]" value="{% if value %}{{ value.thumb }}{% else %}{{ null }}{% endif %}">