LinkViewHelper

ViewHelper link.action

Der ViewHelper link.action hat das Problem, dass ich die URL Parameter durchschleifen möchte, was geht mit addQueryString:

<f:link.action
pageUid="279"
pluginName="productlist"
action="list"
addQueryString="TRUE"
argumentsToBeExcludedFromQueryString="{product}"
>Zurück zur Liste</f:link.action>

Aber ich möchte hier die Produkt uid nicht im Link haben. Es soll gehen mit argumentsToBeExcludedFromQueryString - allerdings schaffe ich es nicht, den Parameter korrekt zu übergeben. Wenn ich es wie oben angebe kommt der Fehler:

The argument "argumentsToBeExcludedFromQueryString" was registered with type "array", but is of type "object" in view helper "Tx_Fluid_ViewHelpers_Link_ActionViewHelper

Wenn ich "{product}" oder "{'product'}" oder  "{1:product}" oder  "{product:1}" usw. angebe kommt immer ein Fehler in der Art von oben (...type string usw).

Der Einzige Ausweg war einen eigenen ViewHelper zu schreiben rsys:Link.

Nachtrag 26.05.14: Herzlichen Dank an Uwe Schmelzer für den Hinweis auf eine Lösung des Problems, ohne extra einen ViewHelper zu schreiben: "Der Trick war, den Value des Arrays mit Quotes zu versehen.":

<f:link.action
pageUid="279"
pluginName="productlist"
action="list"
addQueryString="TRUE"
argumentsToBeExcludedFromQueryString="{0:'product'}"
>Zurück zur Liste</f:link.action>

Wenn ich heute darüber nachdenke erscheint mir das auch Logisch: es soll nicht das ganze Objekt übergeben werden, sondern der Name des Objekts, deshalb die Quotes.

Erstellt: 11/2011| Geändert: 10/2015

LinkViewHelper

Diese ViewHelper hätte eigentlich LinkActionViewHelper heissen müssen. Er erweitert den normalen LinkActionViewHelper dahingehend dass parameter aus der URL gelöscht werden die keine Einträge haben (z.B. tx_rsysproductbase_productlist[searchProduct]= würde eliminiert), damit sich keine unnötig langen URLs ergeben. Wenn es Parameter namens "searchProduct" gab und alle Parameter leer sind wird wieder ein dummy Parameter hinzugefügt (['searchProduct']['active'] = 1) um im Controller zu wissen dass es sich um eine Suchanfrage handelte.

Ferner werden alle Parameter aus GET und POST zusammengewürfelt und ggf. an den Link angehängt um Werte aus einem geposteten Suchformular beim Link nicht zu verlieren.

Hier der Aufruf

<rsys:Link
    pageUid="282"
    action="show"
    controller="Product"
    pluginName="productlist"
    arguments="{
        product : product
    }">
    <rsys:image src="uploads/tx_rsysproductbase/{product.image}" alt="Bild" width="130px" limit="1" wrap="<div class='image'>|</div>" />
</rsys:Link>

Listing

<?php

/*                                                                        *
 * This script belongs to the FLOW3 package "Fluid".                      *
 *                                                                        *
 * It is free software; you can redistribute it and/or modify it under    *
 * the terms of the GNU Lesser General Public License as published by the *
 * Free Software Foundation, either version 3 of the License, or (at your *
 * option) any later version.                                             *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
 * General Public License for more details.                               *
 *                                                                        *
 * You should have received a copy of the GNU Lesser General Public       *
 * License along with the script.                                         *
 * If not, see www.gnu.org/licenses/lgpl.html                    &nbsp; *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */

/**
 *
 * = Examples =
 *
 * <code title="default notation">
 * {namespace rsys=Tx_Rsysproductbase_ViewHelpers}
 * <rsys:Detail.ShowSortlink value="{product.title}" label="tx_rsysproductbase_domain_model_product.title"/>
 * </code>
 * <output>
 * row with label and value wrapped by TS plugin.tx_rsysproductbase.Detail.label_wrap
 * </output>
 *
 * @license www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
 * @api
 */
class Tx_Rsysproductbase_ViewHelpers_LinkViewHelper extends Tx_Fluid_ViewHelpers_Link_ActionViewHelper {

    /**
     * Disable the escaping interceptor because otherwise the child nodes would be escaped before this view helper
     * can decode the text's entities.
     *
     * @var boolean
     */
    protected $escapingInterceptorEnabled = FALSE;




    /**
     * Returns all the get and post arguments to be passed along by the pager.
     *
     * @return array All the get and post arguments.
     */
    public static function getAllGPArguments() {
        $postParameter = t3lib_div::_POST();
        $getParameter = t3lib_div::_GET();

        $mergedParameters = t3lib_div::array_merge_recursive_overrule($getParameter, $postParameter);

        return $mergedParameters;
    }

   

    /**
     * @param string $action Target action
     * @param array $arguments Arguments
     * @param string $controller Target controller. If NULL current controllerName is used
     * @param string $extensionName Target Extension Name (without "tx_" prefix and no underscores). If NULL the current extension name is used
     * @param string $pluginName Target plugin. If empty, the current plugin name is used
     * @param integer $pageUid target page. See TypoLink destination
     * @param integer $pageType type of the target page. See typolink.parameter
     * @param boolean $noCache set this to disable caching for the target page. You should not need this.
     * @param boolean $noCacheHash set this to supress the cHash query parameter created by TypoLink. You should not need this.
     * @param string $section the anchor to be added to the URI
     * @param string $format The requested format, e.g. ".html"
     * @param boolean $linkAccessRestrictedPages If set, links pointing to access restricted pages will still link to the page even though the page cannot be accessed.
     * @param array $additionalParams additional query parameters that won't be prefixed like $arguments (overrule $arguments)
     * @param boolean $absolute If set, the URI of the rendered link is absolute
     * @param boolean $addQueryString If set, the current query parameters will be kept in the URI
     * @param array $argumentsToBeExcludedFromQueryString arguments to be removed from the URI. Only active if $addQueryString = TRUE
     * @return string Rendered link
     * @author Sebastian Kurfürst <sebastian@typo3.org>
     * @author Bastian Waidelich <bastian@typo3.org>
     */
    public function render($action = NULL, array $arguments = array(), $controller = NULL, $extensionName = NULL, $pluginName = NULL, $pageUid = NULL, $pageType = 0, $noCache = FALSE, $noCacheHash = FALSE, $section = '', $format = '', $linkAccessRestrictedPages = FALSE, array $additionalParams = array(), $absolute = FALSE, $addQueryString = FALSE, array $argumentsToBeExcludedFromQueryString = array()) {
        $uriBuilder = $this->controllerContext->getUriBuilder();

        // add gp vars myself
        $this->linkArguments = Tx_Rsysproductbase_ViewHelpers_LinkViewHelper::getAllGPArguments();
       
        // unset some vars
        $pluginFullName = 'tx_rsysproductbase_' . $pluginName;
        unset($this->linkArguments['L']);
        unset($this->linkArguments['cHash']);
        unset($this->linkArguments[$pluginFullName]['__referrer']);
        unset($this->linkArguments[$pluginFullName]['__hmac']);
        // unset empty search params
        if(isset($this->linkArguments[$pluginFullName]['searchProduct'] )) {
            foreach ($this->linkArguments[$pluginFullName]['searchProduct'] as $key=>$value) {
                if(empty($value)) {
                    unset($this->linkArguments[$pluginFullName]['searchProduct'][$key]);
                }
            }
            if(empty($this->linkArguments[$pluginFullName]['searchProduct'] )) {
                // it cleared all parameters (search with no fields filled out: set dummy to know it was a search
                $this->linkArguments[$pluginFullName]['searchProduct']['active'] = 1;
            }
        }
       
        // unset given vars
        foreach ($argumentsToBeExcludedFromQueryString as $key=>$value) {
            if($value==1) {
                unset($this->linkArguments[$pluginFullName][$key]);
            }
        }
        // merge
        $additionalParams = array_merge($additionalParams, $this->linkArguments);


        $uri = $uriBuilder
        ->reset()
        ->setTargetPageUid($pageUid)
        ->setTargetPageType($pageType)
        ->setNoCache($noCache)
        ->setUseCacheHash(!$noCacheHash)
        ->setSection($section)
        ->setFormat($format)
        ->setLinkAccessRestrictedPages($linkAccessRestrictedPages)
        ->setArguments($additionalParams)
        ->setCreateAbsoluteUri($absolute)
        ->setAddQueryString($addQueryString)
        ->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString)
        ->uriFor($action, $arguments, $controller, $extensionName, $pluginName);

        $this->tag->addAttribute('href', $uri);
        $this->tag->setContent($this->renderChildren());
        $this->tag->forceClosingTag(TRUE);

        return $this->tag->render();
    }
}
?>

Erstellt: 09/2012| Geändert: 10/2015