Search code examples
typo3extbasetypo3-6.1.xrealurluribuilder

Preserve arguments in uri builder


Let's say that I have a page with URI:

 http://mydomain.loc/index.php?id=123&by=name&dir=desc

and need to add/update/remove some other param (let's name it offset) to it, so after all it will be:

 http://mydomain.loc/index.php?id=123&by=name&dir=desc&offset=321

Unfortunately when building new link with uriBuilder like this

$uriBuilder = $this->uriBuilder;
$uri = $uriBuilder->setArguments(array('offset' => 321))->build();

I get only index.php?id=123&offset=321 (no by and dir arguments anymore...)

How can I force uriBuilder to preserve these arguments ? (manual rewriting arguments by GeneralUtility::_GP(*) isn't possible, cause they are theoretically unknown)

Also $_GET array isn't good as I'm wrking with RealURL


Solution

  • The uriBuilder has a setAddQueryString method, that does exactly what you want. It merges the current query string into the arguments:

    $uri = $this->uriBuilder->setArguments(array('offset' => 321))->setAddQueryString(TRUE)->build();
    

    As a reference here is the method copied from the actual class out of the TYPO3 core:

    /**
     * If set, the current query parameters will be merged with $this->arguments. Defaults to FALSE.
     *
     * @param boolean $addQueryString
     * @return \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder the current UriBuilder to allow method chaining
     * @api
     * @see TSref/typolink.addQueryString
     */
    public function setAddQueryString($addQueryString) {
        $this->addQueryString = (boolean) $addQueryString;
        return $this;
    }