3

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?

3

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

Your input template could look something like:

<input type="text" name="{{ name }}[title]" value="{{ value.title is defined ? value.title }}">
<input type="text" name="{{ name }}[description]" value="{{ value.description is defined ? value.description }}">
<input type="text" name="{{ name }}[caption]" value="{{ value.caption is defined ? value.caption }}">
<input type="text" name="{{ name }}[thumb]" value="{{ value.thumb is defined ? value.thumb }}">

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 :)

  • 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 Jul 16 '15 at 11:37
  • 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 Jul 16 '15 at 11:39
  • ignore that last comment think I've solved it! Cheers – Lettie Jul 16 '15 at 11:41
  • 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 Jul 16 '15 at 12:21
  • 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 Jul 16 '15 at 13:17

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.