Diamonte Diamonte - 2 months ago 21
JSON Question

Prestashop cms page json content

I'm trying to get CMS page content from my friend's wordpress with api.
This works just fine and I can customise the content with css.

BUT I know you should NOT insert html inside controller, is there a way to do .tpl file for this which is then called for title and content
OR
even better if you could somehow get this content to admin as well, but I think it is currently impossible ?

However, other cms pages should be working as normal.

I have currently in CmsController.php inside function initContent:

if($this->cms->meta_title == 'mycmspage')
{
$ch = curl_init();
$timeout = 5;
$url = 'http://friendurl.com/wp-json/wp/v2/pages/xxx';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);

$obj = json_decode($data, TRUE);
$this->cms->content = '<h2 class="myheader">'.$obj['title']['rendered'].'</h2><article class="myarticle">'. $obj['content']['rendered'].'</article>';
}


using prestashop 1.6.1

Answer

Yes you can set smarty variables and get template content.

$obj = json_decode($data, TRUE);
$this->context->smarty->assign(array(
    'mycms_title' => $obj['title']['rendered'],
    'mycms_content' => $obj['content']['rendered']
));
$this->cms->content = $this->context->smarty->fetch('path_to_your_tpl_file');

Something like this can be achieved with admin controllers as well. Depends on the type of admin page you are on (list, view, add, edit etc.) but by default it's a type of list. So you can override the renderList() method to display it.

AdminYourModController extends ModuleAdminController {
    public function renderList() {
        // your curl code
        $obj = json_decode($data, TRUE);
        $this->context->smarty->assign(array(
            'mycms_title' => $obj['title']['rendered'],
            'mycms_content' => $obj['content']['rendered']
        ));

        return $this->context->smarty->fetch('path_to_your_tpl_file');
    }
}