Skip to content

Instantly share code, notes, and snippets.

@dfeyer
Created October 29, 2013 12:03
Show Gist options
  • Save dfeyer/7213412 to your computer and use it in GitHub Desktop.
Save dfeyer/7213412 to your computer and use it in GitHub Desktop.
TYPO3 Neos Document Teaser TS Object This TS object can be used to extract a teaser from a given page
# DocumentTeaser Implementation
#
prototype(Ttree.OfficialWebsite:DocumentTeaser) {
@class = 'Ttree\\OfficialWebsite\\TypoScript\\DocumentTeaserImplementation'
maximumCharacters = 600
suffix = '...'
documentNode = ${documentNode}
}
<?php
namespace Ttree\OfficialWebsite\TypoScript;
/* *
* This script belongs to the TYPO3 Flow package "Ttree.OfficialWebsite". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License, either version 3 of the *
* License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use TYPO3\Eel\Helper\StringHelper;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Utility\Unicode\Functions;
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
use TYPO3\TypoScript\TypoScriptObjects\AbstractTypoScriptObject;
/**
* A Document Teaser Implementation
*
* @Flow\Scope("prototype")
*/
class DocumentTeaserImplementation extends AbstractTypoScriptObject {
/**
* @var NodeInterface
*/
protected $documentNode;
/**
* @var integer
*/
protected $maximumCharacters;
/**
* @var string
*/
protected $suffix;
/**
* @param \TYPO3\TYPO3CR\Domain\Model\NodeInterface $documentNode
*/
public function setDocumentNode($documentNode) {
$this->documentNode = $documentNode;
}
/**
* @param int $maxCharacters
*/
public function setMaximumCharacters($maxCharacters) {
$this->maximumCharacters = $maxCharacters;
}
/**
* @param string $suffix
*/
public function setSuffix($suffix) {
$this->suffix = $suffix;
}
/**
* Just return the processed value
*
* @return mixed
*/
public function evaluate() {
$stringToTruncate = '';
$documentNode = $this->tsValue('documentNode');
$maximumCharacters = $this->tsValue('maximumCharacters') ?: 600;
foreach ($documentNode->getNode('main')->getChildNodes('TYPO3.Neos.NodeTypes:Text') as $contentNode) {
/** @var NodeInterface $contentNode */
foreach ($contentNode->getProperties() as $propertyValue) {
if (!is_object($propertyValue) || method_exists($propertyValue, '__toString')) {
$stringToTruncate .= $propertyValue;
}
if (Functions::strlen($stringToTruncate) > $maximumCharacters) {
break;
}
}
}
$stringToTruncate = $this->stripUnwantedTags($stringToTruncate);
$stringHelper = new StringHelper();
return $stringHelper->crop($stringToTruncate, $maximumCharacters, $this->tsValue('suffix'));
}
/**
* @param string $content The original content
* @return string The stripped content
*/
protected function stripUnwantedTags($content) {
$content = preg_replace('/(?:<|&lt;)\/?([a-z]+) *[^\/(?:<|&lt;)]*?(?:>|&gt;)/', '', $content);
$content = str_replace('&nbsp;', ' ', $content);
return trim($content);
}
}
# This is an example how to use the object in your TypoScript2 template
include: DocumentTeaser.ts2
customerTeaser = Ttree.OfficialWebsite:DocumentTeaser {
documentNode = ${q(node).parents('[instanceof TYPO3.Neos:Page]').get(0)}
maximumCharacters = 240
@process.1 = ${'<b>' + value + '</b>'}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment