Adding URL parameters to language switch from custom controller

Hello everyone,

I have created my own module for displaying shipping information. This works quite well, I have created a controller, that is available via /?cl=mytracking&parcel=PARCELID. When requested, the desired information are shown.

However, when a user changes the language of the page, the parameter parcel=PARCELID is lost from the URL. Since I want to distribute this link via mail, the use may not be signed in and, hence, the language may not be the one desired by the user. Therefore, it would be great if the user could change the language while still remaining on the same page.

Any ideas on how this is possible?

Hey dmaz,

if this is only related to the language but not to other links, it can be done easily by adding it to your template. I could imagine two ways:

  1. You could add the PARCELID as view variable and - if set - add it to the Links that are related to the language (in the template as there is the variable available if you set it in the controller)
  2. You could read the parameter “parcel” in smarty and add it the same way as (1) to the links

Personally I would prefer (1) as I could include security checks on the params in an easier way.

Hope this helps,
Thorsten

1 Like

Hi Thorten,

thank you for your reply! Unfortunately, this doesn’t work for me, as in my template I am only appending to oxidBlock_content and the language links are provided by the component oxcmp_lang. I could probably override all blocks where such links occur, but this is neither convenient nor future-proof, as I would have to update my module everytime another module or template changes its HTML structure.

Fortunately, I found a solution by implementing getLink() and oxIUrl in my controller:

<?php

/**
 * Class mytracking
 */
class mytracking extends oxUBase implements oxIUrl
{
    /** @var string */
    protected $_sThisParcel;

    /**
     * The render function
     * @throws Exception
     */
    public function render()
    {
        // [...]
        $this->_sThisParcel = $_GET['parcel'];
        // [...]

        return parent::render();
    }

    /**
     * @param null $iLang
     * @param array $aParams
     * @return string
     */
    public function getStdLink($iLang = null, $aParams = array())
    {
        $aParams['cl'] = 'mytracking';
        $aParams['lang'] = $iLang;
        $aParams['parcel'] = $this->_sThisParcel;

        return 'index.php?' . htmlspecialchars(http_build_query($aParams));
    }

    /**
     * @param int $iLang
     * @param bool $blAddId
     * @param bool $blFull
     * @return string
     */
    public function getBaseStdLink($iLang, $blAddId = true, $blFull = true)
    {
        $sUrl = '';
        if ($blFull) {
            //always returns shop url, not admin
            $sUrl = $this->getConfig()->getShopUrl($iLang, false);
        }

        return $sUrl . $this->getStdLink($iLang);
    }

    /**
     * @param null $iLang
     * @return string
     */
    public function getLink($iLang = null)
    {
        return $this->getBaseStdLink($iLang);
    }
}

This works as expected an both changing currencies as well as changing languages works with the generic links.

Thanks,
dmaz

1 Like