Benutzer: Gast
Eintrag: Extbase: TypolinkViewHelper

Extbase: TypolinkViewHelper

von: marcel
Tags: Typo3, Extbase
Kreise: Typo3

TypolinkViewHelper

Code: PHP
<?php
namespace VENDOR\Exttitle\ViewHelpers;
/*                                                                        *
 * 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 http://www.gnu.org/licenses/lgpl.html                      *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */

/**
 */
class TypolinkViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
  /**
   * @param string $parameter
   * @param string $target
   * @param int $noCache
   * @param int $useCacheHash
   * @param array $additionalParams
   * @param string $ATagParams
   * @return mixed
   */
  public function render($parameter, $target = '', $noCache = 0, $useCacheHash = 1, $additionalParams = array(), $ATagParams = '') {
    $typoLinkConf = array(
      'parameter' => $parameter,
    );

    if ($target) {
      $typoLinkConf['target'] = $target;
    }

    if ($noCache) {
      $typoLinkConf['no_cache'] = 1;
    }

    if ($useCacheHash) {
      $typoLinkConf['useCacheHash'] = 1;
    }

    if (count($additionalParams)) {
      $typoLinkConf['additionalParams'] = \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl('', $additionalParams);
    }

    if (strlen($ATagParams)) {
      $typoLinkConf['ATagParams'] = $ATagParams;
    }

    $linkText = $this->renderChildren();

    $textContentConf = array(
      'typolink.' => $typoLinkConf,
      'value'     => $linkText
    );

    return $GLOBALS['TSFE']->cObj->cObjGetSingle('TEXT', $textContentConf);
  }
}
?>

Im TCA kann man ein Feld vom Typ Input mit dem TypoLink-Wizard definieren. Gespeichert wird je nach Art des Links der Parameter, der für das TypoScript typolink.parameter = gedacht ist:

  • 12 (numerisch = interne Seite)
  • http://www.example.org (Protokoll = externe Seite)
  • fileadmin/user_upload/myFile.pdf (Pfad = Verweis auf Datei)
  • mailto:name@example.org (E-Mail)


Mit dem ViewHelper kann man den gespeicherten Wert direkt verarbeiten.

TCA

Code: PHP
'my_link_input_field' => array(
  'exclude' => 0,
  'label' => 'myLink',
  'config'      => array(
    'type'     => 'input',
    'size'     => '15',
    'max'      => '255',
    'checkbox' => '',
    'eval'     => 'trim',
    'wizards'  => array(
      '_PADDING' => 2,
      'link'     => array(
        'type'         => 'popup',
        'title'        => 'Link',
        'icon'         => 'link_popup.gif',
        'script'       => 'browse_links.php?mode=wizard',
        'JSopenParams' => 'height=300,width=500,status=0,menubar=0,scrollbars=1'
      )
    )
  ),
),
Code: XML XHTML XSLT HTML
<tx:Typolink parameter="{myLinkInputField}">my link-label</tx:Typolink>

Special thanks to the guys from the cicbase-project on github, where I found the viewhelper:

github.com/castiron/cicbase/blob/master/Classes/ViewHelpers/TypolinkViewHelper.php

Es gibt noch keine Kommentare.