by
KK » Tue Aug 23, 2016 2:36 am
Hi,
So essentially you want the form in the admin-panel to do what a databound-form you have on the front-end does, right?
That shouldn't be a problem because in v2.0 the form used by the edit screen in admin-panel is also a regular databound-form.
As explained in the docs at
viewtopic.php?f=5&t=10241, we can override any admin-side snippet by placing a snippet with the same name in your theme folder ('sample' folder in the docs example).
The snippet used to render the edit form is named 'content_form.html'. Copy this snippet from the default 'couch/theme/_system' folder to your 'couch/theme/sample' folder.
Now whenever you edit any template, this copied template is the one that Couch will use. (You might find it useful to make some change to this snippet e.g. add <h1>hello</h1> just above the <cms:form> so as to visibly know if this is indeed the template being rendered).
You can now edit this template and add all the code you used on the front-end DBF.
However, this would mean that now the code you added would be used by the edit screens of *all* the templates in your admin-panel!Clearly that is not what you want.
You want to selectively override the 'content_form.html' snippet only for the particular template you mentioned (let us call it 'invoices.php').
This selective 'per-template' overriding can be done but will require a little bit of PHP.
This is how we do it -
In the 'couch/theme/sample' folder you'll find a file named 'kfunctions.php'. If you edit it, you'll find the following function at its very beginning -
- Code: Select all
function k_override_renderables(){
global $FUNCS;
// add more candidate template names for renderables implemented as templates
$FUNCS->add_event_listener( K_THEME_NAME.'_alter_render_vars_content_list_inner', 'MyTheme::_alter_render_vars' );
}
Please modify the function to add one line of additional code to make it as follows -
- Code: Select all
function k_override_renderables(){
global $FUNCS;
// add more candidate template names for renderables implemented as templates
$FUNCS->add_event_listener( K_THEME_NAME.'_alter_render_vars_content_list_inner', 'MyTheme::_alter_render_vars' );
$FUNCS->add_event_listener( K_THEME_NAME.'_alter_render_vars_content_form', 'MyTheme::_alter_render_vars' );
}
I cannot explain in detail what that code is doing but would like you to look closely at the highlighted parts from the code -
$FUNCS->add_event_listener( K_THEME_NAME.'_alter_render_vars_content_list_inner',
$FUNCS->add_event_listener( K_THEME_NAME.'_alter_render_vars_content_form',
We have added the 'content_form' while the existing statement had 'content_list_inner' in its place. I think you can make out that our 'content_form' is for the 'content_form.html' snippet we are trying to selectively override (and, by the same logic, the existing 'content_list_inner' is for 'content_list_inner.html' snippets).
With this code in place, Couch while rendering 'content_form' will first check if a snippet named 'content_form_
whatever-template-name.html' is present (where 'whatever-template-name' would be substituted by the current template being edited). If present, this snippet is used. Else, the default 'content_form.html' is used.
I think you can see that to make Couch selectively use a template for a particular template, we only need to add that template's name to the original snippet's name.Just a fine point about template name -
suppose our template name is 'invoices.php', to make this name fit to be used as a file-name we need to replace all non-ascii characters with '-' (hyphens). So, our 'invoices.php' will become 'invoices-php'.
Had the template name been 'old/paid/invoices.php' (happens when a template is in nested folders), the converted name would become - 'old-paid-invoices-php'. A bit of a hassle but nothing too difficult.
OK, coming back to our 'invoices.php' template, rename the 'content_form.html' snippet to 'content_form_
invoices-php.html'
And now when you edit any page of invoices.php you should see your snippet being used. All other templates will remain unaffected and will continue using the default template.
Try it

To be fair, we could also have used the older method of 'custom admin screen' to do the same thing but I thought this is a good opportunity to discuss the new method as we can use it for selectively overriding absolutely *all* snippets used by the admin-panel e.g. sidebar, logo, buttons etc.
Hope it helps. Let me know if something remains unclear.