Forum for discussing general topics related to Couch.
3 posts Page 1 of 1
Hey everyone.

@KK, Please could you provide me with some sample on how to achieve the following:

I have it already on the front-end, when users create a new cloned-page of a template, I have some code that sends a websocket notification and also creates a cloned page on another template of a notification with some data about the user/page created.

I have a template that admins create invoices for users, it's a cloned template (Obviously) and they use the back-end to create them. What I want to achieve is when they create a new cloned page of the invoices template, to do some other things (Create a page on the other template with some information about the user and about the page just created - such as the page link). and to create a websocket notification, which is just some short lines of php (Can be inside cms:php tags).

I am sure this is doable in V2, but I do not yet know how, and haven't seen how on the thread releasing V2. Can you provide me some sample or direction to get started?
Image
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.
Thanks KK, this worked perfectly.

I agree, this could've been done with the old method, but editing an entire template file just to add a block of code seemed like overkill and I was sure V2 had a more elegant solution. and there it is :)
Image
3 posts Page 1 of 1