2

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 %}">
1

I don't know enough about your end goal, but it almost sounds like what you're trying to do is what Craft already does very well. If you're just trying to save some time on Assets custom field creation, you might consider checking out FieldManager which lets you import/export fields across projects.

0

You can't just compose a new field type from multiple other fields by adding some Twig macro to you field's template!

There's a lot of work to do to make things work. You need to store your field in its own database table, so each child field gets its own column, you also need to design and set up field settings to make configuration of child fields possible.

I guess that you're currently storing multiple custom text fields as a serialized string in a single column (content attribute set to AttributeType::Mixed); this works for simple text, but not for more complex data.

  • ok so it's a bit more complex than I originally thought. I've added the code I have so far. (not sure if that is the correct way to create a custom field but it does work) So where would you suggest I go from here? Any plugins I can look at that do something similar or tutorial on setting up custom field types? – Lettie Jul 22 '15 at 10:35
  • It's a lot more complex, have a look at the source code of the Matrix field or the plugin SuperTable. They both make it possible to configure other fields within a field. – carlcs Jul 22 '15 at 10:39
  • ok cheers will do – Lettie Jul 22 '15 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.