Unique identifiers for Repeater field individual records #233
Replies: 1 comment
-
The Repeater did originally have unique IDs for items (relative to the field itself), but it was removed because it introduced a potential for data corruption if IDs are removed or modified in any way. This used to manifest as problems with nested forms or relations or items going out of order. It is best to think of the Repeater as a simple JSON item editor, but instead of manipulating one field it's manipulating a block of fields. When the Translate plugin is brought into the mix, it makes sense for it to be wholly translatable because essentially the Translate plugin is saving the same JSON structure for another language. Personally, I find if you need this level of control over the translations, you are better off using the Relation manager as opposed to the Repeater. |
Beta Was this translation helpful? Give feedback.
-
First of all - this is just an idea, feature proposal. Also note that I have worked with Winter CMS projects recently but there are still plenty of things to learn so some things in this discussion may be overlooked (I may see something too easy or over-complicate something).
The main reason of this discussion:
Let's say we have a repeater field with following example structure:
If we then want to use Winter's Translate plugin and add translate functionality for this repeater field then it's only possible for the whole field
content
by definingpublic $translatable = ['content'];
in our model class. But in the ideal scenario user should be able to specify individual fields which could be translated, for example, fieldquestion
andanswer
value could be translated butimage
value would be same for all languages and would be loaded automatically into other language repeater fields when user switches between languages. Probably there should be language switch functionality for every translatable field not the whole repeater field in general.Because of that currently there is also overload and duplication for data input for multi-language repeater fields - if you have repeater field with many records and fields and lot of them should not be translated (be same in all languages) then by switching to another language you still need to recreate all repeater records from zero. If only certain fields could be translatable and other not then user would not need to switch whole repeated field to another language but only adjust translatable field values.
In model class this definitions may look like
public $translatable = ['content.*.question', 'content.*.answer'];
- if we want question and answer field to be translatable (language switch for these fields) but image field would be same in all languages.The main limitations at the current situation:
If I understand correctly then probably the biggest limitations of implementing this kind of Translate plugin functionality is that Repeater field records can be sorted freely and there is no identifier for each individual record - that explains why it would be nearly impossible to track which field translation belongs to which field after resorting repeater records. So we finally come to the subject of this discussion - there probably should be option to enable unique identifiers for each individual repeater field record.
About unique identifier idea:
In my opinion, this new functionality should be optional and there should be another new configuration parameter for repeater field so there would be no issues with already existing repeater fields, Translate plugin etc. Also in some cases probably some users would prefer current Repeater field + Translate plugin behavior when language switch works for the whole repeater field. It should also work only if model class has proper definitions for fields which should be allowed to translate (similar like I specified before).
Unique identifier probably should be generated internally from UNIX timestamp which is then hashed or processed in a way that gives kinda unique output string or at the simplest case - using
uniqid()
PHP function. So, for example, unique identifier for one record would be5819f3ad1c0ce
, for another9f39aa0ecd89d
etc. This identifier then could be stored within repeater field JSON data next to each record data by additional key, for example,_uuid
etc. and used to reference to the same record later - this probably then could be also used by Translate plugin to identifier individual records and fields, and their respective translations. Previously mentioned_uuid
field would not be directly shown to user by editing repeater field values in back-end so it would only be used internally to identify each record.Additional use cases for unique record identifiers:
In addition to previously described Translate plugin scenario, this functionality may help solve or help in other situations:
_uuid
, for example,content.5819f3ad1c0ce.answer
orcontent.9f39aa0ecd89d.question
- may be useful for cases when users have large and complex repeater fields and they don't want to loop through all repeater records to find only specific record._uuid
may even be used in back-end form to display different record editing forms or logic to each repeater record individually.Anyway, mostly this
_uuid
could be used by Translate plugin or other plugins, internal processes.I am hoping that someone would understand what I was thinking with all of this. 😆 Feel free to ask questions or continue discussion.
Beta Was this translation helpful? Give feedback.
All reactions