Skip to content

Commit

Permalink
[!!!][BUGFIX] Respect different rendering for content elements
Browse files Browse the repository at this point in the history
If a plugin is registered as content element, the TypoScript rendering path
is different than when being registered as regular plugin.

We now respect that and additionally throw an exception in case the
rendering path cannot be resolved.

This can be considered as breaking. Before the URL returned nothing
and the page generating this URL kept working. Now the page generating
the invalid URL will be broken as an exception is thrown in this case.

Fixes #19
  • Loading branch information
helhum committed Jun 24, 2016
1 parent 2f5573c commit a7dd7d8
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 50 deletions.
24 changes: 19 additions & 5 deletions Classes/Configuration/RecordRenderingConfigurationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ public function __construct(RenderingContext $renderingContext)
* @param string $contextRecord
*
* @return string[]
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
*/
public function configurationFor($extensionName, $pluginName, $contextRecord = 'currentPage')
{
list($tableName, $uid) = $this->resolveTableNameAndUidFromContextString($contextRecord);
$pluginSignature = $this->buildPluginSignature($extensionName, $pluginName);

$renderingPath = $this->resolveRenderingPath($pluginSignature);
return array(
'record' => $tableName . '_' . $uid,
'path' => 'tt_content.list.20.' . $pluginSignature
'path' => $renderingPath
);
}

Expand Down Expand Up @@ -87,9 +88,7 @@ protected function resolveTableNameAndUidFromContextString($contextRecord)
$tableNameAndUid = array('pages', $this->renderingContext->getFrontendController()->id);
}
}

// TODO: maybe check if the record is available

return $tableNameAndUid;
}

Expand All @@ -114,4 +113,19 @@ protected function buildPluginSignature($extensionName, $pluginName)

return strtolower($extensionName . '_' . $pluginName);
}
}

/**
* @param string $pluginSignature
* @return string
* @throws ConfigurationBuildingException
*/
protected function resolveRenderingPath($pluginSignature)
{
$typoScriptRenderingSetup = $this->renderingContext->getFrontendController()->tmpl->setup['tt_content.'];
if (isset($typoScriptRenderingSetup[$pluginSignature . '.']['20'])) {
return sprintf('tt_content.%s.20', $pluginSignature);
} elseif(isset($typoScriptRenderingSetup['list.']['20.'][$pluginSignature])) {
return sprintf('tt_content.list.20.%s', $pluginSignature);
}
throw new ConfigurationBuildingException(sprintf('Could not determine rendering location for plugin signature "%s"', $pluginSignature), 1466779430);
}}
2 changes: 2 additions & 0 deletions Classes/ViewHelpers/Uri/AjaxActionViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class AjaxActionViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractView
* @param string $contextRecord The record that the rendering should depend upon. e.g. current (default: record is fetched from current Extbase plugin), tt_content:12 (tt_content record with uid 12), pages:15 (pages record with uid 15), 'currentPage' record of current page
*
* @return string Rendered link
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
*/
public function render($action = null, array $arguments = array(), $controller = null, $extensionName = null, $pluginName = null, $pageUid = null, $section = '', $format = '', $linkAccessRestrictedPages = false, array $additionalParams = array(), $absolute = false, $addQueryString = false, array $argumentsToBeExcludedFromQueryString = array(), $addQueryStringMethod = null, $contextRecord = 'current')
{
Expand Down Expand Up @@ -100,6 +101,7 @@ public function render($action = null, array $arguments = array(), $controller =
* @param string $contextRecord
*
* @return string[]
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
*/
public function buildTypoScriptRenderingConfiguration($extensionName, $pluginName, $contextRecord)
{
Expand Down
63 changes: 41 additions & 22 deletions Classes/ViewHelpers/Widget/LinkViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* Public License for more details. *
* */

use Helhum\TyposcriptRendering\Configuration\RecordRenderingConfigurationBuilder;
use Helhum\TyposcriptRendering\Renderer\RenderingContext;

/**
*
*/
Expand All @@ -24,6 +27,13 @@ class LinkViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedVi
*/
protected $tagName = 'a';


/**
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
* @inject
*/
protected $configurationManager;

/**
* Initialize arguments
*
Expand All @@ -40,18 +50,6 @@ public function initializeArguments()
$this->registerArgument('addQueryStringMethod', 'string', 'Method to be used for query string');
}

/**
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
* @inject
*/
protected $configurationManager;

/**
* @var \TYPO3\CMS\Extbase\Service\ExtensionService
* @inject
*/
protected $extensionService;

/**
* Render the Uri.
*
Expand All @@ -62,12 +60,12 @@ public function initializeArguments()
* @param string $section The anchor to be added to the URI
* @param string $format The requested format, e.g. ".html
* @param bool $ajax true if the URI should be to an Ajax widget, false otherwise.
* @param string $contextRecord The record that the rendering should depend upon. e.g. current (default: record is fetched from current Extbase plugin), tt_content:12 (tt_content record with uid 12), pages:15 (pages record with uid 15), 'currentPage' record of current page
*
* @return string The rendered link
*
* @api
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
*/
public function render($pluginName, $extensionName, $action = null, $arguments = array(), $section = '', $format = '', $ajax = true)
public function render($pluginName, $extensionName, $action = null, array $arguments = array(), $section = '', $format = '', $ajax = true, $contextRecord = 'current')
{
if ($ajax === true) {
$uri = $this->getAjaxUri();
Expand All @@ -83,19 +81,26 @@ public function render($pluginName, $extensionName, $action = null, $arguments =
* Gets the URI for an Ajax Request.
*
* @return string the Ajax URI
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
*/
protected function getAjaxUri()
{
list($table, $uid) = explode(':', $this->configurationManager->getContentObject()->currentRecord);
$pluginName = $this->arguments['pluginName'];
$extensionName = $this->arguments['extensionName'];
$pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
$contextRecord = $this->arguments['contextRecord'];
$arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : array();
$ajaxContext = array(
'record' => $table . '_' . $uid,
'path' => 'tt_content.list.20.' . str_replace('tx_', '', $pluginNamespace)
);
$additionalParams['tx_typoscriptrendering']['context'] = json_encode($ajaxContext);
if ($contextRecord === 'current') {
if (
$pluginName !== $this->controllerContext->getRequest()->getPluginName()
|| $extensionName !== $this->controllerContext->getRequest()->getControllerExtensionName()
) {
$contextRecord = 'currentPage';
} else {
$contextRecord = $this->configurationManager->getContentObject()->currentRecord;
}
}
$renderingConfiguration = $this->buildTypoScriptRenderingConfiguration($extensionName, $pluginName, $contextRecord);
$additionalParams['tx_typoscriptrendering']['context'] = json_encode($renderingConfiguration);

$uriBuilder = $this->controllerContext->getUriBuilder();
$argumentPrefix = $this->controllerContext->getRequest()->getArgumentPrefix();
Expand Down Expand Up @@ -149,4 +154,18 @@ protected function getWidgetUri()

return $uriBuilder->build();
}

/**
* @param string $extensionName
* @param string $pluginName
* @param string $contextRecord
*
* @return string[]
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
*/
public function buildTypoScriptRenderingConfiguration($extensionName, $pluginName, $contextRecord)
{
$configurationBuilder = new RecordRenderingConfigurationBuilder(new RenderingContext($GLOBALS['TSFE']));
return $configurationBuilder->configurationFor($extensionName, $pluginName, $contextRecord);
}
}
62 changes: 40 additions & 22 deletions Classes/ViewHelpers/Widget/UriViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@
* Public License for more details. *
* */

use Helhum\TyposcriptRendering\Configuration\RecordRenderingConfigurationBuilder;
use Helhum\TyposcriptRendering\Renderer\RenderingContext;

/**
*
*/
class UriViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{
/**
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
* @inject
*/
protected $configurationManager;

/**
* Initialize arguments
*
Expand All @@ -30,18 +39,6 @@ public function initializeArguments()
$this->registerArgument('addQueryStringMethod', 'string', 'Method to be used for query string');
}

/**
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager
* @inject
*/
protected $configurationManager;

/**
* @var \TYPO3\CMS\Extbase\Service\ExtensionService
* @inject
*/
protected $extensionService;

/**
* Render the Uri.
*
Expand All @@ -52,12 +49,12 @@ public function initializeArguments()
* @param string $section The anchor to be added to the URI
* @param string $format The requested format, e.g. ".html
* @param bool $ajax true if the URI should be to an Ajax widget, false otherwise.
* @param string $contextRecord The record that the rendering should depend upon. e.g. current (default: record is fetched from current Extbase plugin), tt_content:12 (tt_content record with uid 12), pages:15 (pages record with uid 15), 'currentPage' record of current page
*
* @return string The rendered link
*
* @api
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
*/
public function render($pluginName, $extensionName, $action = null, $arguments = array(), $section = '', $format = '', $ajax = true)
public function render($pluginName, $extensionName, $action = null, array $arguments = array(), $section = '', $format = '', $ajax = true, $contextRecord = 'current')
{
if ($ajax === true) {
return $this->getAjaxUri();
Expand All @@ -70,19 +67,26 @@ public function render($pluginName, $extensionName, $action = null, $arguments =
* Get the URI for an Ajax Request.
*
* @return string the Ajax URI
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
*/
protected function getAjaxUri()
{
list($table, $uid) = explode(':', $this->configurationManager->getContentObject()->currentRecord);
$pluginName = $this->arguments['pluginName'];
$extensionName = $this->arguments['extensionName'];
$pluginNamespace = $this->extensionService->getPluginNamespace($extensionName, $pluginName);
$contextRecord = $this->arguments['contextRecord'];
$arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : array();
$ajaxContext = array(
'record' => $table . '_' . $uid,
'path' => 'tt_content.list.20.' . str_replace('tx_', '', $pluginNamespace)
);
$additionalParams['tx_typoscriptrendering']['context'] = json_encode($ajaxContext);
if ($contextRecord === 'current') {
if (
$pluginName !== $this->controllerContext->getRequest()->getPluginName()
|| $extensionName !== $this->controllerContext->getRequest()->getControllerExtensionName()
) {
$contextRecord = 'currentPage';
} else {
$contextRecord = $this->configurationManager->getContentObject()->currentRecord;
}
}
$renderingConfiguration = $this->buildTypoScriptRenderingConfiguration($extensionName, $pluginName, $contextRecord);
$additionalParams['tx_typoscriptrendering']['context'] = json_encode($renderingConfiguration);

$uriBuilder = $this->controllerContext->getUriBuilder();
$argumentPrefix = $this->controllerContext->getRequest()->getArgumentPrefix();
Expand Down Expand Up @@ -136,4 +140,18 @@ protected function getWidgetUri()

return $uriBuilder->build();
}

/**
* @param string $extensionName
* @param string $pluginName
* @param string $contextRecord
*
* @return string[]
* @throws \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
*/
public function buildTypoScriptRenderingConfiguration($extensionName, $pluginName, $contextRecord)
{
$configurationBuilder = new RecordRenderingConfigurationBuilder(new RenderingContext($GLOBALS['TSFE']));
return $configurationBuilder->configurationFor($extensionName, $pluginName, $contextRecord);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ protected function setUp()
{
$this->typoScriptControllerMock = $this->getMock('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', array(), array(), '', false);
$this->configurationBuilder = new RecordRenderingConfigurationBuilder(new RenderingContext($this->typoScriptControllerMock));
$this->typoScriptControllerMock->tmpl = new \stdClass();
$this->typoScriptControllerMock->tmpl->setup = array(
'tt_content.' => array(
'list.' => array(
'20.' => array(
'news_pi1' => 'USER',
'news_pi1.' => array(),
)
),
'news_pi2.' => array(
'20' => 'USER',
'20.' => array()
)
)
);
}

/**
Expand All @@ -65,6 +80,12 @@ public function pluginContextDataProvider()
'pages:1',
array('record' => 'pages_1', 'path' => 'tt_content.list.20.news_pi1'),
),
'page specified, content element' => array(
'News',
'Pi2',
'pages:1',
array('record' => 'pages_1', 'path' => 'tt_content.news_pi2.20'),
),
'tt_content specified' => array(
'News',
'Pi1',
Expand Down Expand Up @@ -132,10 +153,21 @@ public function buildingConfigurationWorks($extensionName, $pluginName, $recordC

/**
* @test
* @expectedException \Helhum\TyposcriptRendering\Configuration\ConfigurationBuildingException
*/
public function buildingConfigurationThrowsExceptionIfInvalidTypesAreGiven()
{
$this->expectException('Helhum\\TyposcriptRendering\\Configuration\\ConfigurationBuildingException');
$this->expectExceptionCode(1416846201);
$this->configurationBuilder->configurationFor('foo', 'PiBar', array());
}

/**
* @test
*/
public function buildingConfigurationThrowsExceptionIfRenderingConfigIsNotFound()
{
$this->expectException('Helhum\\TyposcriptRendering\\Configuration\\ConfigurationBuildingException');
$this->expectExceptionCode(1466779430);
$this->configurationBuilder->configurationFor('News', 'Pi3', 'current');
}
}

0 comments on commit a7dd7d8

Please sign in to comment.