Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.

PLUGIN DEV: How can I add dynamic url for suggest_requests() and match_request($request)

+1 vote
452 views
asked Aug 10 in Q2A Core by esqeudero

I want a plugin page url appear as

"user/".$handle.'/something'

 I need to set suggest_requests() and match_request($request) at plugin page. So, I tried this one but it wont work.

function suggest_requests() 

{

$handle = qa_request_part(1);

return array(

array(

'title' => 'Something',

'request' => 'user/'.$handle.'/something',

'nav' => 'M',

),

);

}

function match_request($request)

{

$handle = qa_request_part(1);

     

if(isset($handle) && qa_request_part(0) == 'user' && qa_request_part(2) == 'something'){

        return true;

    } else {

        return false;

    }

}

when I go to "user/username/something" error appears stating page not found. Where I am doing wrong? 

Q2A version: q2a 1.7.5 customized
commented Aug 11 by pupi1985
What you're "doing wrong" is trying to overwrite a core route. Page modules can't overwrite any core route, that's why they execute later in the bootstrap process

1 Answer

+1 vote
answered Aug 10 by JackSiro
edited Aug 11 by JackSiro
 
Best answer

I can help you with this because it is something that I have had to overcome while working on my blog post plugin.  Please check out on this plugin. In fact simply download it from github https://github.com/JackSiro/Q2A-Blog-Post-Plugin and check the user layer file and see how to achieve what you are thinking of. All you need is to override the user pages and templates using your own layer in your plugin where you will then declare your own template and add your preferred url. 

function doctype()

{

if (qa_request_part(0) == 'user') {

$handle = qa_request_part(1);

$usersection = qa_request_part(2);

if (!strlen($handle)) {

$handle = qa_get_logged_in_handle();

qa_redirect(!empty($handle) ? 'user/' . $handle : 'users');

}

if (QA_FINAL_EXTERNAL_USERS) {

$userid = qa_handle_to_userid($handle);

if (!isset($userid))

return include QA_INCLUDE_DIR . 'qa-page-not-found.php';

$usershtml = qa_get_users_html(array($userid), false, qa_path_to_root(), true);

$userhtml = @$usershtml[$userid];

} else $userhtml = qa_html($handle);

if ($usersection == 'articles') {

$this->template = 'user-something';

$this->content = $this->qa_user_something($handle, $userhtml);//custom content 

}

}

qa_html_theme_base::doctype();

}

All the best sir and fellow developer.

commented Aug 10 by esqeudero
I followed your code however it does not work for me. By the way on your user-layer.php you overrider nav_list function. In there your wrote double case for $this->template 'user-articles'.
Also I noted that you have never set template as 'user-articles' by code qa_set_template('user-articles'). So how your request maps to that template? Maybe you have done some core hacks.

*** I am able to add sub navigation on user page. However, my problem is that I could not map my page.php to that navigation. Neither match_request has worked for dynamic urls.
commented Aug 10 by esqeudero
edited Aug 10 by esqeudero
I managed to do it with your way. However, there one problem. the body template name appears as "template-not-found", and head_scripts and head_css are not loaded.

<head>
<meta charset="utf-8">
<title>Some Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../../qa-theme/SnowFlat/qa-styles.css?1.8.0">
<link rel="search" type="application/opensearchdescription+xml" title="Q&amp;A" href="../../opensearch.xml">
<script src="../../qa-theme/SnowFlat/js/snow-core.js?1.8.0"></script>
</head>
<body class="qa-template-not-found qa-theme-snowflat qa-body-js-on">
commented Aug 11 by JackSiro
Good to learn that my code helped you though I would like to point to you that the plugin has no core hacks at all. It simply uses overrides and custom codes only. If you saw 'qa-template-no-template' on the html source code just wish to state that that is just a minor issue. It arises to be a template is custom code. But you can try to use the function "qa_set_template('user-articles')" function and see it will work out as you intended in the final html code. I have used it elsewhere and it has always worked but there i only used "$this->template = 'user-articles'" to set custom template. Am sorry as for me I was majorly concerned with creating a custom page and show it on the user nav list as well as when it is opened but I will use the real function next time. Thanks for highlighting that to me and also for selecting my answer. God bless you.
commented Aug 11 by esqeudero
I tired qa_set_template('user-someting'); but it wont work.

Jack you should also add if($class == 'nav-sub') in order your code to not conflict with other navigations.

function nav_list($navigation, $class, $level=null)
    {
        if($class == 'nav-sub') {
            switch ( $this->template ) {
            case 'user':
            case 'user-wall':
            case 'user-activity':
            case 'user-answers':
            case 'user-articles':
                $navigation['articles'] = array(
                    'label' => qa_lang('bp_lang/nav_articles'),
                    'url' => qa_path_html('user/'.qa_request_part(1).'/articles'),
                );
                break;               
            case 'account':
            case 'favorites':
                $navigation['articles'] = array(
                    'label' =>qa_lang('bp_lang/nav_articles'),
                    'url' => qa_path_html('user/'.qa_get_logged_in_handle().'/articles'),
                );
                break;
            }
           
        }

        qa_html_theme_base::nav_list($navigation, $class, $level=null);
    }
commented Aug 11 by JackSiro
sorry sir for confusing you a bit. I forgot that I was only using qa_set_template in my core hacks. However this function does not work in custom code. Instead use $this->template as in my code in the answer I have just redited. I even rectified my plugin on github. Infact not that this is done in the doctype function only
...