by
KK » Thu Oct 20, 2016 1:55 am
OK

So let us test the following out and see if that is what you want Paolo.
As you know, in v2.0 the edit-form shown in admin-panel is just a regular DBF (databound form).
For our proposed solution we'll override this default DBF with our own that will do some pre-processing of the submitted data before saving (to be specific - will check if the editable region we are interested in is empty and if yes insert a custom value instead).
There are two ways of overriding the default DBF -
1. The older way is to use custom admin-screen (covered in
viewtopic.php?f=5&t=10241#p24680)
2. The recent (and more advanced) way is to override the template internally used by Couch to render the edit screen in admin-panel. You can find a complete example here -
https://www.couchcms.com/forum/viewtopi ... 693#p25693Whichever of the two you go for (I'll suggest the second method), our custom form will differ only very slightly from the default form used by Couch (which is in 'couch/theme/_system/content_form.html' template). Therefore we can simply copy that template and make the following amendments.
The portion of the code we are interested in is this -
- Code: Select all
<cms:if k_success >
<cms:db_persist_form
_invalidate_cache='1'
_token=k_cur_token
/>
..
..
Please modify it to make it as follows -
- Code: Select all
<cms:if k_success >
<cms:if "<cms:not_empty frm_my_desc />" >
<cms:set tmp_value=frm_my_desc />
<cms:else />
<cms:capture into='tmp_value'>
Hello!! I am auto-generated text!
</cms:capture>
</cms:if>
<cms:db_persist_form
_invalidate_cache='1'
_token=k_cur_token
my_desc=tmp_value
/>
To explain the code -
I am assuming your editable region is named 'my_desc'. So upon successful form submission the DBF will make available the value submitted through that region as a variable named 'frm_my_desc' (i.e. append the name with a 'frm_' - regular form stuff in Couch).
Our code kicks in when the form is successfully submitted and then it checks the 'frm_my_desc' variable and stores it into a new variable named 'tmp_value'. If this value is empty, our code dynamically generates a new value and stores that instead into 'tmp_value'. Point is, the 'tmp_value' variable either contains the value explicitly submitted by our editable region or contains a dynamically generated value if nothing was submitted.
The final piece is the "my_desc=tmp_value" statement added to <cms:db_persist_form /> where we use the value contained in 'tmp_value' to save into the database as the value of 'my_desc' editable region.
And that is it.
If you think this works for you, you can place whatever code you wish into the auto-generated tmp_value. For example, instead of what I used above -
- Code: Select all
<cms:capture into='tmp_value'>
Hello!! I am auto-generated text!
</cms:capture>
- you can use the following -
- Code: Select all
<cms:capture into='tmp_value'>
<p><span style="text-decoration:underline">Nos reproductions de tableaux sur toile de
<cms:php> $print = "<cms:show frm_k_page_title />"; $printArray = explode(",", $print);$print = $printArray[0]; echo $print ; </cms:php>
sont de très bonne facture</span>,
et nous utilisons une méthode traditionnelle de reproduction qui a fait ses preuves.</p>
</cms:capture>
Please note that instead of >cms:show k_page_title />, I have used <cms:show frm_k_page_title /> (i.e. prefixed a 'frm_') because the DBF will make available submitted values in that form.
In case you are not sure what values are available for you to use in the form, place the following in the <cms:if k_success >condition and submit the form. It will show all the variables (including those submitted by the form) available for use -
- Code: Select all
<cms:abort>
<cms:dump_all />
</cms:abort>
Hope it helps. Do let me know.