Jump to content

Custom OG tags in module front controller


jonsecu

Recommended Posts

Hi... I am developing a module that has a front controller that can be accessed through an URL like this:

http://website.com/index.php?fc=module&module=mymodulename&controller=default&id_index=1

When I point my URL to this address the controller, located at modules/mymodulename/controllers/front/default.php on my prestashop instalation, fetches the id_index parameter on the URL and uses it to retrieve some information from the database that is passed to smarty vars to display it on the webpage. This works well. The front view is located at modules/mymodulename/views/templates/front/default.tpl

 

The file default.php looks like this:

<?php
if (!defined('_PS_VERSION_'))
    exit;

class MymodulenameDefaultModuleFrontController extends
ModuleFrontController   {
    public function initContent() {
        global $smarty;
        parent::initContent();

        // Code to retrieve php vars from database
        // Code...
        // Code...

        // Smarty assigns
        // Display data on webpage
        $smarty->assign('name', $name);
        $smarty->assign('msg', $msg);
        
        // OG tags
        $smarty->assign('ogtitle', $ogtitle);
        $smarty->assign('ogimage', $ogimage);
        $smarty->assign('ogdescription', $ogdescription);
        $smarty->assign('ogurl', $ogurl);

        // Call default view
        $this->setTemplate('default.tpl');
    }
}
?>

What I want to do is to somehow use a hook (header hook?) where I can retrieve the OG tags smarty vars to dynamically change them depending on the id_index parameter passed on the URL. I want to do something like this on the header:

<head>
<!-- header code -->
<!-- header code -->
<!-- header code -->

<!-- OG TAGS -->
<meta property="og:title" content="{$ogcontent}" />
<meta property="og:type" content="article" />
<meta property="og:image" content="{$ogimage}" />
<meta property="og:url" content="{$ogurl}" />
<meta property="og:description" content="{$ogdescription}" />

<!-- header code -->
<!-- header code -->
</head>

I tried using the header hook public function hookDisplayHeader($params) available at my modules main php file mymodulename.php. However, even if the hook is properly attached, at the moment this code executes I have not still the og tags smarty assignments.

 

Is there a way to assign smarty vars on default.php and then hook them to the <head> section of the front module view?

 

I`ll truly appreciate any help. I am using prestashop 1.5.3.1. Thanks!

Link to comment
Share on other sites

 

Hi misthero... yes with this code on the main php file of the module,

public function hookDisplayHeader($params)
{
$this->context->controller->addCSS($this->_path.'filename.css', 'all');
$this->context->controller->addJS($this->_path.'filename.js', 'all');
return $this->display(__FILE__, 'module-header.tpl');
}

I can hook some vars to the module-header.tpl, but I would have to make the smarty assignments precisely here, on the main php file of the module; which I cant, since the index to retrieve the information from the database is passed on the URL of the front view with an URL like this:

http://website.com/index.php?fc=module&module=mymodulename&controller=default&id_index=1

So I have the php vars on the front module php controller located at:

modules/mymodulename/controllers/front/default.php

And it is on this default.php file where I make the asignments to the smarty vars, not on the module's main php controller. If I make the asignments on the main php controller and try to retrieve them on module-header.tpl it doesnt work. For instance, I get this:

<meta property="og:description" content="" />

Instead of this:

<meta property="og:description" content="This is the description that I asigned to a smarty var on default.php" />

Any idea please?

 

Thanks!

Edited by jonsecu (see edit history)
Link to comment
Share on other sites

I don't know the rest of your code but i guess you could do something like this

public function hookDisplayHeader($params) {
  if (Tools::getValue('id_index')) { //this is how you get the id_index parameter
    $description = $this->functionToGetDescription(Tools::getValue('id_index')); //pass it to a function that make a database query and get the correct description
    $this->smarty->assign('ogdescription',$description); //assign the value to smarty
    return $this->display(__FILE__, 'module-header.tpl');
  }

it is a very general example, you have to adapt it to your code, but this way you can access the variable {$ogdescription} from module-header.tpl

Link to comment
Share on other sites

  • 2 months later...

I don't know the rest of your code but i guess you could do something like this

public function hookDisplayHeader($params) {
  if (Tools::getValue('id_index')) { //this is how you get the id_index parameter
    $description = $this->functionToGetDescription(Tools::getValue('id_index')); //pass it to a function that make a database query and get the correct description
    $this->smarty->assign('ogdescription',$description); //assign the value to smarty
    return $this->display(__FILE__, 'module-header.tpl');
  }

it is a very general example, you have to adapt it to your code, but this way you can access the variable {$ogdescription} from module-header.tpl

 

Hi misthero... thank you very much. It worked perfectly. At the end it was like this:

public function hookDisplayHeader($params) {
        $this->context->controller->addCSS($this->_path . 'present.css', 'all');
        $this->context->controller->addJS($this->_path . 'present.js', 'all');

        // Recuperar metadata para etiquetas og
        if (Tools::getValue('id_regalo')) { 
            $meta = getMetaFromId(Tools::getValue('id_regalo'));
            $this->context->controller->addJS('http://s7.addthis.com/js/250/addthis_widget.js');
            $this->smarty->assign('meta', $meta); //assign the value to smarty
        }

        return $this->display(__FILE__, 'presentforme-header.tpl');
    }

I just checked if id_regalo was posted on the request with the Tools class... then used function to retrieve the $meta array from id_regalo. Then assigned it to a smarty var and that did the trick.

 

Thanks!

Edited by jonsecu (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...