Problems, need help? Have a tip or advice? Post it here.
7 posts Page 1 of 1
Hello,

I'm trying to make an editable field have unique default text which is generated by an addon.
So I wrote an addon, and it works. However I noticed that the code executes also when I visit the template's cloned page. Since it does some database querying, I want that particular block of code to execute only when I'm creating a new cloned page in the admin panel.

This is how I'm doing it in a template:

Code: Select all
<cms:editable
        name='tool_barcode'
        label='Barkodas'
        type='text'
        order='4'
        group='tool_main_info_group'>
        <cms:generate-code field_name='tool_barcode' />
    </cms:editable>


I really can't think of how to do it...
Ok, I realize now what I've been doing wrong here, but there's another issue.

Initial problem was solved by doing so in config_form_view:

Code: Select all
<cms:field 'tool_barcode'>
            <cms:input
                name=k_field_name
                type='bound'
                trust_mode='1'
                value="<cms:generate-code field_name='tool_barcode' />"
            />
        </cms:field>


But now problem is how do I generate the code only if the field is empty? seems to ignore the value="" tag...
Hi,

The second approach, as you have noticed, won't work because with type = 'bound', the input is just a pointer to the original <cms:editable> with the same name - all parameters are automatically taken form the original definition. The one set here will be ignored.

Coming to your original approach, that seems to be fine.
Your <cms:generate-code> tag will actually execute every time the template is visited by a super-admin.
The output of the tag is compared to its previous output and if the two are same, no database changes are made.

So, if your tag does not generate a unique value every time it is executed, there should be no problem in letting it run - it will happen only for the super-admin and change nothing if the output does not change.

Or is it that the tag produces a different output for every run? In which case, this approach will not work.
Please let me know.
Yes, it produces different output every time. The number must be unique.
My idea was that when I go to create a new page, the barcode field is populated automatically with an available number. If I'd open an existing page, the barcode shouldn't change...
In that case, you'll need a much different (and more complex) solution.

We have a 'Sequential ID' addon (viewtopic.php?f=8&t=11372) that does something similar.
Please check it and see if it can serve your purpose. If not, you may use it as a base (or at least a guide) for your custom addon.

Hope it helps.
I saw that addon earlier, but thought it might be easy to make a unique random number generator heh :P I'll take a deeper look into that UID thingy.
@KK: maybe it'd be possible to add a button beside the input field, which would call a PHP API to get the code and then insert it? hmm...
7 posts Page 1 of 1