Custom Field Type with Multiple Values I'd like to create a field type for my plugin called "location" with two text boxes: one that holds a coordinate for latitude, and one for longitude. I haven't figured out yet how to map those two values from HTML input elements in a template to a JSON array in the database.  Is there an elegant way to achieve this with the Craft API?
You can do this simply by setting your field type’s defineContentAttribute() method to return AttributeType::Mixed, which will tell Craft to JSON-encode its value before storing it in the database.



public function defineContentAttribute()
{
    return AttributeType::Mixed;
}


Then, whatever you submit in the field’s post data will be saved as-is.

For example, if your getInputHtml() method had this:

public function getInputHtml($name, $value)
{
    $lat = (isset($value['lat']) ? $value['lat'] : '');
    $lng = (isset($value['lng']) ? $value['lng'] : '');

    return '&lt;input type="text" name="'.$name.'[lat]" value="'.$lat.'"&gt;' .
           '&lt;input type="text" name="'.$name.'[lng]" value="'.$lng.'"&gt;';
}


Then it would be expected that the resulting post data for your field would look something like:

array(
    'lat' =&gt; '12345',
    'lng' =&gt; '67890'
)


And with AttributeType::Mixed, that’s exactly how it would get stored. Behind the scenes it will get JSON-encoded when getting saved to the DB, and JSON-decoded when getting fetched from the DB, but your field type will never need to know about that.