Answers.Body: <p>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 <a href="https://github.com/engram-design/FieldManager" rel="nofollow noreferrer">FieldManager</a> which lets you import/export fields across projects.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-04-06T04:12:30.597Z
Answers.LastActivityDate: 2017-04-06T04:12:30.597Z
Answers.UserDisplayName: casey
Answers.Body: <p>You can't just compose a new field type from multiple other fields by adding some Twig macro to you field's template!</p>
  
  <p>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.</p>
  
  <p>I guess that you're currently storing multiple custom text fields as a serialized string in a single column (content attribute set to <code>AttributeType::Mixed</code>); this works for simple text, but not for more complex data.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-22T10:35:05.707Z
Answers.Text: 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?
Answers.UserDisplayName: Lettie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-22T10:39:18.880Z
Answers.Text: 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.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-22T10:53:19.087Z
Answers.Text: ok cheers will do
Answers.UserDisplayName: Lettie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-21T14:57:06.887Z
Answers.LastActivityDate: 2015-07-21T14:57:06.887Z
Answers.UserDisplayName: carlcs
Body: <p>I have a plugin that creates custom field types to extend crafts default field types.</p>
  
  <p>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.</p>
  
  <p>When the plugin is installed these fields appear in the default field dropdown list.</p>
  
  <p>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.</p>
  
  <p>Is this possible?</p>
  
  <p>This is what I have so far.</p>
  
  <p>In the fieldtype class:</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('customfieldtypes/video', 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>In the video template:</p>
  
  <pre><code>&lt;label for="{{ name }}[title]"&gt;Title&lt;/label&gt;
  &lt;input class="text nicetext fullwidth" type="text" name="{{ name }}[title]" value="{% if value %}{{ value.title }}{% else %}{{ null }}{% endif %}"&gt;&lt;br /&gt;
  &lt;label for="{{ name }}[description]"&gt;Description&lt;/label&gt;
  &lt;textarea class="text nicetext fullwidth" type="text" name="{{ name }}[description]"&gt;{% if value %}{{ value.description }}{% else %}{{ null }}{% endif %}&lt;/textarea&gt;&lt;br /&gt;
  &lt;label for="{{ name }}[caption]"&gt;Caption&lt;/label&gt;
  &lt;input class="text nicetext fullwidth" type="text" name="{{ name }}[caption]" value="{% if value %}{{ value.caption }}{% else %}{{ null }}{% endif %}"&gt;&lt;br /&gt;
  &lt;label for="{{ name }}[link]"&gt;Link&lt;/label&gt;
  &lt;input class="text nicetext fullwidth" type="text" name="{{ name }}[link]" value="{% if value %}{{ value.link }}{% else %}{{ null }}{% endif %}"&gt;&lt;br /&gt;
  &lt;label for="{{ name }}[thumb]"&gt;Thumb&lt;/label&gt;
  &lt;input class="text nicetext fullwidth" type="text" name="{{ name }}[thumb]" value="{% if value %}{{ value.thumb }}{% else %}{{ null }}{% endif %}"&gt;
  </code></pre>
  
Tags: <plugin-development><assets><plugins><custom-fieldtype><custom-field>
Title: Add an assets field to a field type plugin template
ThreadText: # Add an assets field to a field type plugin template
  
  (Asked by: username_0 on 2015-07-16)
  
  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 %}">
  
  ```
  
  **Tags:** plugin-development, assets, plugins, custom-fieldtype, custom-field
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-04-06. Score: 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](https://github.com/engram-design/FieldManager) which lets you import/export fields across projects.
  
  
  ---
  
  ### Answer by username_2 on 2015-07-21. Score: 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.
  
  > **Comments:**
  > **username_0**: 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?  
  > **username_2**: 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.  
  > **username_0**: ok cheers will do  
  
  ---
  
  