Skip to content

Instantly share code, notes, and snippets.

@josefglatz
Created November 8, 2014 12:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josefglatz/7ab9a912fb0b581a555d to your computer and use it in GitHub Desktop.
Save josefglatz/7ab9a912fb0b581a555d to your computer and use it in GitHub Desktop.
TYPO3CMS tt_content_drawItem hook example (TYPOCMS 6.2)
<?php
namespace J18\J18thmsimkup\Hooks;
/***************************************************************
* Copyright notice
*
* (c) 2014 GLATZ, Josef <jousch@gmail.com>
* 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 2 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!
***************************************************************/
use TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\Utility\IconUtility;
use \TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
/**
* Preprocesses the preview rendering of content elements.
* - CType menu - menu_type galleryteaser
* - CType menu - menu_type productselector
*
* @author GLATZ, Josef <jousch@gmail.com>
* @package TYPO3
* @subpackage tx_j18thmsimkup
*/
class PageLayoutViewDrawItem implements PageLayoutViewDrawItemHookInterface {
/**
* Preprocesses the preview rendering of a content element.
*
* @param \TYPO3\CMS\Backend\View\PageLayoutView $parentObject Calling parent object
* @param boolean $drawItem Whether to draw the item using the default functionalities
* @param string $headerContent Header content
* @param string $itemContent Item content
* @param array $row Record row of tt_content
* @return void
*/
public function preProcess(\TYPO3\CMS\Backend\View\PageLayoutView &$parentObject, &$drawItem, &$headerContent, &$itemContent, array &$row) {
if ($row['CType'] === 'menu' && $row['menu_type'] === 'galleryteaser') {
$records = GeneralUtility::trimExplode(',', $row['pages']);
$headerContent = '<strong>Gallery Teasers</strong><br>';
$itemContent .= '<div style="display:block; padding:10px 0 20px 10px;">';
foreach ($records as $key => $record) {
$recordId = array_pop(GeneralUtility::trimExplode('_', $record));
$childRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', 'pages', 'uid = ' . (int) $recordId);
if ($childRecord['tx_s5singlegalleries_cover'] > 0) {
$recordCover = $parentObject->thumbCode($childRecord, 'pages', 'tx_s5singlegalleries_cover');
} else {
$recordCover = '<div class="typo3-message message-warning" id="ext-gen55">
<div class="message-body" id="ext-gen56"> Cover image is missing! </div></div>';
}
$recordEdit = $parentObject->getIcon('pages', $childRecord);
$recordPath = BackendUtility::getRecordPath($childRecord['uid'], '', 20);
if (ExtensionManagementUtility::isLoaded('pagepath', TRUE)) {
$recordFrontendUrl = \tx_pagepath_api::getPagePath((int) $childRecord['uid']);
}
if ($key > 0) {
$itemContent .= '<hr style="margin-top: 10px; margin-bottom: 10px;" />';
}
$itemContent .= '<div style="display:inline-block; margin-right: 10px;">' . $recordCover . '</div>' .
'<div style="display:inline-block; vertical-align:top;"><b>' . $recordEdit . htmlspecialchars($childRecord['title']) . '</b>' . ($childRecord['nav_title'] ? ' (' . htmlspecialchars($childRecord['nav_title']) . ')' : '') . '<br>';
$itemContent .= '<small><a href="' . $recordFrontendUrl . '" target="_blank" title="Opens gallery in new browser window/tab">' . $recordFrontendUrl . '</a></small><br>';
$itemContent .= '<small style="color: #c2c2c2">' . $recordPath . '</small><br>';
$itemContent .= '</div>';
}
$itemContent .= '</div>';
$drawItem = FALSE;
}
if ($row['CType'] === 'menu' && $row['menu_type'] === 'websiteselector') {
$records = GeneralUtility::trimExplode(',', $row['pages']);
$headerContent = '<strong>Product/Website Selector</strong><br>';
$itemContent .= '<div style="display:block; padding:10px 0 20px 10px;">';
foreach ($records as $key => $record) {
$recordId = array_pop(GeneralUtility::trimExplode('_', $record));
$childRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', 'pages', 'uid = ' . (int) $recordId);
if ($childRecord['media'] > 0) {
$recordCover = $parentObject->thumbCode($childRecord, 'pages', 'media');
} else {
$recordCover = '<div class="typo3-message message-warning" id="ext-gen55">
<div class="message-body" id="ext-gen56"> Image is missing! </div></div>';
}
if ($childRecord['tx_j18thmsimkup_productselecttitle']) {
$recordTitle = htmlspecialchars($childRecord['tx_j18thmsimkup_productselecttitle']);
} else {
$recordTitle = '<div class="typo3-message message-warning" id="ext-gen55" style="display: inline-block">
<div class="message-body" id="ext-gen56">Productselector Link Label for \'<span style="font-style: italic">' . htmlspecialchars($childRecord['title']) . '</span>\' is not set! </div></div>';
}
$recordEdit = $parentObject->getIcon('pages', $childRecord);
if (ExtensionManagementUtility::isLoaded('pagepath', TRUE)) {
$recordFrontendUrl = \tx_pagepath_api::getPagePath((int) $childRecord['uid']);
}
if ($key > 0) {
$itemContent .= '<hr style="margin-top: 10px; margin-bottom: 10px;" />';
}
$itemContent .= '<div style="display:inline-block; margin-right: 10px;">' . $recordCover . '</div>' .
'<div style="display:inline-block; vertical-align:top;"><b>' . $recordEdit . $recordTitle . '</b><br>';
$itemContent .= '<small><a href="' . $recordFrontendUrl . '" target="_blank" title="Opens gallery in new browser window/tab">' . $recordFrontendUrl . '</a></small><br>';
$itemContent .= '</div>';
}
$itemContent .= '</div>';
$drawItem = FALSE;
}
}
}
?>
@rowild
Copy link

rowild commented Nov 16, 2014

I would like to suggest to show an example of how you call the script in ext_localconf.php, too. Sources on the web show something like this:

[ext_localconf.php]

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem'][$_EXTKEY] = 
   \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($_EXTKEY).'Hooks/PageLayoutViewDrawItem.php:PageLayoutViewDrawItem';

However, I would like to know how you do it :-)

Thanks!
Robert

@josefglatz
Copy link
Author

For sure, I call the script through [ext_localconf.php]

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem'][$_EXTKEY] =
    'EXT:' . $_EXTKEY . '/Classes/Hooks/PageLayoutViewDrawItem.php:J18\J18thmsimkup\Hooks\PageLayoutViewDrawItem';

@josefglatz
Copy link
Author

I've updated to newer "syntax":

    $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawItem'][$_EXTKEY] =
        'J18\\J18thmsimkup\\Hooks\\PageLayoutViewDrawItem';

In this scenario, the hook must implement an interface, therefore no method name must be given. (If I understand it correctly.)

@rowild
Copy link

rowild commented Nov 17, 2014

Thanks, Jousch, for showing the possibilities and sharing!!!

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