Problems, need help? Have a tip or advice? Post it here.
6 posts Page 1 of 1
I have a 'blog' template that incorporates a checkbox list of a 'tags' template via 'relation'.

Meaning that when I go into the Admin Panel to enter a new blog post, there's a checklist of categories where I can assign any number of category tags to that particular post.

My question is:

For any blog post where I don't check the box of any tag, could there be a way for Couch to automatically assign a default, "Uncategorized" tag to that post?

I know you can set a default value for type='radio' and type='checkbox' fields, but with the type='relation', I'm not sure if this will be possible.

https://docs.couchcms.com/concepts/relationships.html

Whoever's using the Admin Panel can always put a check in the box of the "Uncategorized" manually, but in case he forgets to do so, it would be nice to have as a failsafe.
Hey couchclass,

I don't know the answer to your immediate question (can opt_selected= 'Uncategorized' be set on a relationship tag) - have you tried it? Maybe experiment and see. I'm sure, however, that KK will have the simple yes/no answer to that question.

Without more information on what you are trying to achieve it is difficult to know why you would want this behaviour. Since the relationship feature is supposed to bring together pages from two different templates, I can only assume that what you want is for there to be a 'tags' page called 'Uncategorized' which would then display all pages where no tag has been set by the user. I am wondering if a workaround would be to create that 'tag' page called 'Uncategorized' and in the code for that page, where you would have written some code to say "show pages that are uncategorized" you could use some code to say "show pages not related" (perhaps having to make use of Couch's 'if' or 'exists' tags, or the 'not_empty tag but inverting it). Again, as I said, without any more explanation, or code, to work from, it is difficult to understand the problem. Feel free to post the code and I'll have a go at understanding it.

Good luck!

Anthony
@couchclass, I agree with @AnthonyW90 - you can easily figure out if a post is related to zero tags and show it as 'Uncategorized'; no need to create a specific tag named 'uncategorized' for this. Please see '2. Enhanced cms:pages tag' in viewtopic.php?f=5&t=8581 for sample queries.

That said, if you absolutely must use a discrete category, here is something for you.
Please paste the following code in your 'addons/kfunctions.php' file -
Code: Select all
$FUNCS->add_event_listener( 'admin_pre_action', function( $route ){
    global $FUNCS, $CTX, $DB;

    /*** edit values ***/
    $template = 'blog.php';
    $field_name = 'categories';

    $tags_template = 'tags.php';
    $tag_name = 'uncategorized';
    /*** end edit values ***/


    if( $route->module=='pages' && $route->name=='edit_view' && $route->masterpage==$template ){
        $token = $CTX->get( 'k_cur_token' );
        $FUNCS->add_event_listener( 'form_posted_'.$token, function($method, &$pg) use($field_name, $tags_template, $tag_name){
            global $DB;

            $field_name = 'f_'.$field_name.'_chk';
            $rs = $DB->select(K_TBL_TEMPLATES . ' t, ' . K_TBL_PAGES . ' p' , array('p.id'), "p.template_id = t.id AND t.name='".$tags_template."' AND p.page_name='".$tag_name."'" );
            if( count($rs) ){
                $tag_id = $rs[0]['id'];

                if( isset($_POST[$field_name]) && is_array($_POST[$field_name])){
                    $arr_ids = $_POST[$field_name];

                    if( count($arr_ids) > 1 ){// remove default value
                        if( ($pos = array_search($tag_id, $arr_ids)) !== false ){
                            unset($arr_ids[$pos]);
                            $arr_ids = array_values( $arr_ids );
                            $_POST[$field_name] = $arr_ids;
                        }
                    }
                }
                else{
                    $_POST[$field_name] = $tag_id;
                }
            }
        });
    }
});

Make sure to edit the following block in the code above to put in the values specific to your setup -
Code: Select all
    /*** edit values ***/
    $template = 'blog.php';
    $field_name = 'categories';

    $tags_template = 'tags.php';
    $tag_name = 'uncategorized';
    /*** end edit values ***/

Now anytime all the checkboxes are left empty, the code above automatically checks the specified default tag.
Conversely, if any other tag is selected by the user, the default tag is unchecked.

Hope it helps. Do let me know.
@KK, ain't same possible in couch words via <cms:config_form_view> ?
@trendoman, my first attempt was indeed through <cms:config_form_view>. However, in this case, since the 'relation' field is also physically present on the form, it was picking up values from $_POST and then the explicit values set through <cms:config_form_view> were leading to a clash.

Therefore it became necessary to overwrite the raw values in $_POST.
Anthony and KK,

Thank you. This was a very useful way to think about and look at this. KK, I haven't had time to implement your code yet, but I'm definitely going to very soon and will let you know how it goes. Thank you kindly indeed for working that out and sharing. I'm eager to try it out.

--CC
6 posts Page 1 of 1
cron