Skip to content

Instantly share code, notes, and snippets.

@fazzyx
Last active November 7, 2016 14:16
Show Gist options
  • Save fazzyx/5dcf6448b666d064a69e27a01ceb906a to your computer and use it in GitHub Desktop.
Save fazzyx/5dcf6448b666d064a69e27a01ceb906a to your computer and use it in GitHub Desktop.
TYPO3 Fluid ViewHelper for content element "shortcut"
<?php
namespace NAMESPACE\ThemeMgCustom\ViewHelpers;
/*
*
* Copyright notice
*
* (c) 2015 Claus Fassing <[email protected]>, MEDIENGARAGE
*
* 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!
*/
/**
* <mg:Reference records="{panel.records}" field="header" />
* records = record ids, field = table field
*
* @author Claus Fassing <[email protected]>
*/
class ReferenceViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{
/**
*
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
*/
protected $configurationManager;
/**
*
* @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
*/
protected $cObj;
/**
* Inject configuration manager and get content object
*
* @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
*/
public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
{
$this->configurationManager = $configurationManager;
$this->cObj = $this->configurationManager->getContentObject();
}
/**
* Get content object of ce type 'shortcut'. Only return the field value
*
* @param string $records
* @param string $field
* @return string
*/
public function render($records, $field)
{
$recordList = explode(',', $records);
for($i=0;$i < count($recordList);$i++) {
$uids .= end(explode('_', $recordList[$i]));
if($i < count($recordList) - 1) {
$uids .= ',';
}
}
$conf = array(
'table' => 'tt_content',
'select.' => array(
'pidInList' => '0',
'uidInList' => $uids,
),
'renderObj' => 'COA',
'renderObj.' => array(
'10' => 'TEXT',
'10.' => array(
'field' => $field
)
)
);
$content = $this->cObj->cObjGetSingle('CONTENT', $conf);
return $content;
}
}
@chriwo
Copy link

chriwo commented Nov 7, 2016

Nice ViewHelper, but I would change three things:

  1. In line 69 change explode to GeneralUtility::trimExplode(',', $records, true). All empty value would be removed.
  2. In line 71 change explode to GeneralUtility::intExplode('_', $recordList[$i], true). All empty value would be removed and the values in $recordsList[$i] would be parse to an integer value
  3. Line 71 add the string comma to $uids at the end (e.g. $uids = GeneralUtility::intExplode('_', $recordList[$i], true) . ','; ) and remove line 72, 73 and 74. After the for-loop add $uids = rtrim($uids, ',') to remove the last comma.

The change of the explode function gives more security and the third thing a little more speed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment