Generate Frontend Links in a backend module

For a tiny backend module fe_users should be informed via email about some special topics on the website.
Thus, i had to create frontend links from backend.

A came up with a simple solution. I installed the extension pagepath from Dimitry Dulepov and created a dedicated viewhelper to generate the frontend links from backend.

The viewhelper:

class Tx_MyExt_ViewHelpers_Be_Link_PagePathViewHelper extends Tx_Fluid_ViewHelpers_Be_AbstractBackendViewHelper {

    /**
     * Get the fullpath to a fe-page from backend. Used with the extension pagepath from Dimitry Dulepov
     * @param integer $pageId
     * @param array $parameters
     * @return mixed
     */
    public function render($pageId = NULL, $parameters = array()) {
        if (t3lib_extMgm::isLoaded('pagepath', TRUE)) {
            require_once(t3lib_extMgm::extPath('pagepath', 'class.tx_pagepath_api.php'));
            if (NULL === $pageId) {
                $pageId = $this->renderChildren();
            }
            return tx_pagepath_api::getPagePath($pageId, $parameters);
        }
        return NULL;
    }
}

The code in the fluid-template looks like that:

{namespace myhelpers=Tx_MyExt_ViewHelpers}
<myhelpers:be.link.pagePath>{settings.thePid}</myhelpers:be.link.pagePath>

Additionally i would like to have short urls in the template, so i created another view helper:

class Tx_MyExt_ViewHelpers_Uri_ShortUrlViewHelper extends Tx_Fluid_ViewHelpers_Uri_PageViewHelper {

    /**
     *
     * @param string $url
     * @param integer $urlLength
     * @param string $index_script_url
     * @param boolean $absolute
     * @return string
     */
    public function render($url = NULL, $urlLength = 10, $index_script_url = 'index.php', $absolute = TRUE) {
        if (NULL === $url) {
            $url = $this->renderChildren();
        }
        $index_script_url = $absolute ? t3lib_div::getIndpEnv('TYPO3_SITE_URL') : $index_script_url;
        return t3lib_div::makeRedirectUrl($url, $urlLength, $index_script_url);
    }
}

And here is how the fluid-template looks like:

{namespace myhelpers=Tx_MyExt_ViewHelpers}
<myhelpers:uri.shortUrl><myhelpers:be.link.pagePath>{settings.thePid}</myhelpers:be.link.pagePath></myhelpers:uri.shortUrl>

If you need to create a multidimensional parameters array, e.g. to create a link to the single view of your extbase extension, you have to create a wrapper viewhelper for that:

<?php

/* * *************************************************************
 *  Copyright notice
 *
 *  (c) 2011 Sebastian Schreiber <me@schreibersebastian.de>, Sebastian Schreiber
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU
 * General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 * ************************************************************* */

/**
 *
 *
 * @package ssch_joborama_profile
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
 *
 */
class Tx_MyExt_Link_WhateverLinkViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {

    /**
     *
     * @var Tx_MyExt_ViewHelpers_Be_Link_PagePathViewHelper
     */
    protected $pagePathViewHelper = NULL;

    /**
     *
     * @param Tx_MyExt_ViewHelpers_Be_Link_PagePathViewHelper $pagePathViewHelper
     */
    public function injectPagePathViewHelper(Tx_MyExt_ViewHelpers_Be_Link_PagePathViewHelper $pagePathViewHelper) {
        $this->pagePathViewHelper = $pagePathViewHelper;
    }

    /**
     * @param integer $pid The pid for the single view
     * @param Tx_MyExt_Domain_Model_Whatever $whateverModel
     */
    public function render($pid, Tx_MyExt_Domain_Model_Whatever $whateverModel= NULL) {
        if (NULL === $whateverModel) {
            $whateverModel= $this->renderChildren();
        }
        if ($whateverModel instanceof Tx_MyExt_Domain_Model_Whatever) {
            $parameters = array();
            $parameters['tx_myext_pi1'] = array();
            $parameters['tx_myext_pi1']['uid'] = $whateverModel->getUid();
            $parameters['tx_myext_pi1']['controller'] = 'MyController';
            $parameters['tx_myext_pi1']['action'] = 'MyAction';
            return $this->pagePathViewHelper->render($pid, $parameters);
        }
    }

}

The code in the fluid-template looks like that:

{namespace myhelpers=Tx_MyExt_ViewHelpers}
<myhelpers:link.whateverLink pid="{settings.singlePid}">{whateverModel}</myhelpers:link.whateverLink>

Tiny but practical solution.

Veröffentlicht von

avatar

Sebastian Schreiber

Ich bin 1980 in Bergisch Gladbach geboren. Nach dem Abitur und Zivildienst habe ich 2000 an der Fachhochschule Lippe & Höxter das Studium der Medienproduktin begonnen und nach längeren Aufenthalten in Valencia und Berlin 2003 das Studium mit dem Abschluss Bachelor of Science in Medienproduktion abgeschlossen. Nach einer Festanstellung kurz nach dem Studium in einer kleinen Webagentur in Köln mit Schwerpunkt TYPO3 bin ich nun seit 2008 freiberuflicher Webentwickler.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.