Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

We have a custom fieldtype that can be attached to an entry type multiple times and everything works just fine. This fieldtype has multiple input fields (shortUrl, longUrl) and they have been properly namespaced to make them unique (via the fieldtype handle) and when the data is saved, it gets saved to a table that defines the necessary columns (id, elementId, shortUrl, longUrl) since the defineContetAttribute() method has been set to false.

As previously mentioned, this fieldtype has been properly namespaced and is taking advantage of the Craft JS buffer to avoid naming conflicts.

The issue comes up when we try to retrieve the data from our custom table via preValue($value) because $value will be null and all the other values we have access to in this context ($field->model->id, $field->model->handle, $field->element->id) are not unique enough to fetch a record.

I've tried a couple of workarounds but have not found a clean way to do this yet.

The prepValue() method provides the entry id ($field->element->id) which we can use to fetch the content (record) for each field since we have a relationship set but beyond that, I haven't found any other way to identify each record individually so that I can attach it to the right field instance within the input html.

There might be something simple that I'm missing and thought I would ask: )

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

Since you're saving your data in a separate table, you basically need two fields within your custom table to help you identify which field & element originally saved the content:

  • The element id (which you're already storing)
  • The field id (missing from your current setup)

You could even create an index on your custom table that would be a combo of those two fields.

In theory, you should be able to retrieve your data with a combination of the $field->model->id and $field->element->id.

When saving:

$attr['elementId'] = $field->element->id;
$attr['fieldId']   = $field->model->id;

$fieldRecord->setAttributes($attr);

When retrieving:

$fieldRecord = MyPlugin_FieldRecord::model()->findByAttributes(array(
    'elementId' => $field->element->id,
    'fieldId'   => $field->model->id,
));
share|improve this answer
1  
Thanks for the answer Lindsey, and yes, this is definitely going in the right direction. The fieldtype I'm working on has some complexities that cause issues somewhat unrelated to my actual problem (guzzle request caching issues and having to validate existence in local records and a third party API) but what you've suggested was one of my first workarounds and right now I'm refactoring a chunk of code that works based on the response from the API (which generates a short link) and if I get that part fixed, I'll confirm the final workflow I end up using. –  Selvin Ortiz Jun 29 at 4:50
    
Great, thanks Selvin! Definitely let me know if this solution works for you, as I'll soon need to implement the same thing on my plugin. :) –  Lindsey D Jun 29 at 4:55
    
Hey Lindsey, I've accepted your answer because this is the ideal workflow and what you've outlined works. I had other issues becuase of the complexity of the plugin and a bug in guzzle that had me thinking this set up was not working and went around in circles thinking the field id and element id was not enough uniqueness but this setup along with properly namespaced fields and JS buffering works well in pretty much any situation where multiple intances are needed. –  Selvin Ortiz Jun 29 at 18:52
    
Awesome, glad to hear you got it working! And thanks for the update! –  Lindsey D Jun 29 at 18:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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