Search code examples
typo3fluidview-helperstypo3-6.1.x

Is there a Fluid viewhelper to truncate an URL? If not, how do I make one?


In TYPO3's Fluid or in Fedext/vhs, is there a viewhelper that can convert

http://www.stackoverflow.com/questions/ask

into

www.stackoverflow.com

?

PS: that's the goal:

<f:format.raw><f:link.external uri="{item.link}">{item.just-display-the-domain}</f:link.external></f:format.raw>

EDIT (adapting the question to the answer I got): If I have to build a custom view helper, how do I proceed?


Solution

  • I really doubt if there would be any sensible reason for adding this kind of VH into the core, de facto, writing custom VH is like a piece of cake (when you finally realize it is) so simple formatters can be created by devs in their custom tool exts just in minutes.

    Ie. in TYPO3 4.x assuming that you have a custom extension with key urs all you need to do is create one proper class, containing render($params) method and extending Tx_Fluid_Core_ViewHelper_AbstractViewHelper class:

    /typo3conf/ext/urs/Classes/ViewHelpers/GetDomainViewHelper.php:

    <?php
    class Tx_Urs_ViewHelpers_GetDomainViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
    
        /**
         * @param $link string Each `allowed` param need to have its line in PHPDoc
         * @return string
         */
        public function render($link) {
            $link = str_replace('http://', '', $link);
            $link = str_replace('https://', '', $link);
            $segments = explode('/', $link);
            return trim($segments[0]);
        }
    }
    ?>
    

    Next in your templae declare its namespace and... that's all, you can use it:

    {namespace urs=Tx_Urs_ViewHelpers}
    
    <urs:getDomain link="http://stackoverflow.com/questions/20499453" />
    

    Take special attention about letter case in things like Tx_Urs_ViewHelpers... etc.

    More details in http://docs.typo3.org/typo3cms/ExtbaseFluidBook/8-Fluid/8-developing-a-custom-viewhelper.html

    In TYPO3 ver. 6.x

    Things works preaty similar the main change of course is new namespacing

    /typo3conf/ext/urs/Classes/ViewHelpers/GetDomainViewHelper.php:

    <?php
    namespace TYPO3\Urs\ViewHelpers;
    
    class GetDomainViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
    
        /**
         * @param $link string Each `allowed` param need to have its line in PHPDoc
         * @return string
         */
        public function render($link) {
            $link = str_replace('http://', '', $link);
            $link = str_replace('https://', '', $link);
            $segments = explode('/', $link);
            return trim($segments[0]);
        }
    }
    

    In templates:

    {namespace urs=TYPO3\Urs\ViewHelpers}
    
    <urs:getDomain link="http://stackoverflow.com/questions/20499453" />
    

    Of course in both cases instead of using hardcoded links you will use:

    <urs:getDomain link="{item.link}" />