[FEATURE] Info ViewHelpers for page and content information array 73/46473/13
authorClaus Due <claus@namelesscoder.net>
Wed, 3 Feb 2016 22:25:26 +0000 (23:25 +0100)
committerMarkus Klein <markus.klein@typo3.org>
Sat, 5 Mar 2016 10:24:51 +0000 (11:24 +0100)
Adds ViewHelpers ``f:info.page`` and ``f:info.content`` which return
or assign content or page information arrays respectively depending
on usage of the ``as`` argument as known from other helpers.

Change-Id: Idfa5b68b37e926593072ddf75e6d143a2c5037f8
Releases: master
Resolves: #38942
Reviewed-on: https://review.typo3.org/46473
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/core/Documentation/Changelog/master/Feature-38942-Content-AndPage-infoViewHelpers.rst [new file with mode: 0644]
typo3/sysext/fluid/Classes/ViewHelpers/Info/AbstractInfoViewHelper.php [new file with mode: 0644]
typo3/sysext/fluid/Classes/ViewHelpers/Info/ContentViewHelper.php [new file with mode: 0644]
typo3/sysext/fluid/Classes/ViewHelpers/Info/PageViewHelper.php [new file with mode: 0644]

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-38942-Content-AndPage-infoViewHelpers.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-38942-Content-AndPage-infoViewHelpers.rst
new file mode 100644 (file)
index 0000000..6a9b5d2
--- /dev/null
@@ -0,0 +1,29 @@
+====================================================
+Feature: #38942 - Content- and Page-info ViewHelpers
+====================================================
+
+Description
+===========
+
+ViewHelpers to return or assign as template variable the information arrays from page and content respectively.
+
+``f:info.content`` and ``f:info.page`` have been added, both of which support the ``as`` argument as known from other helpers.
+
+.. code-block:: xml
+
+       <f:info.content as="contentInfo">
+           The content element header is {contentInfo.header}.
+       </f:info.content>
+
+       <f:info.page as="pageInfo">
+           The page title is {pageInfo.title}
+       </f:info.page>
+
+       <f:alias map="{contentInfo: '{f:info.content()}', pageInfo: '{f:info.page()}'}">
+           The content element header is {contentInfo.header} and the page title is {pageInfo.title}
+       </f:alias>
+
+Impact
+======
+
+New ViewHelpers ``f:info.content`` and ``f:info.page`` become available.
diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/Info/AbstractInfoViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/Info/AbstractInfoViewHelper.php
new file mode 100644 (file)
index 0000000..41b4194
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+namespace TYPO3\CMS\Fluid\ViewHelpers\Info;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
+use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
+
+/**
+ * Base class for information retrieval ViewHelpers
+ */
+abstract class AbstractInfoViewHelper extends AbstractViewHelper
+{
+    /**
+     * Initialize/register arguments
+     *
+     * @return void
+     */
+    public function initializeArguments()
+    {
+        $this->registerArgument('as', 'string', 'Optional template variable to assign - if not used, ViewHelper returns the info array directly');
+    }
+
+    /**
+     * @return string|array
+     */
+    public function render()
+    {
+        return static::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
+    }
+
+    /**
+     * @return array
+     */
+    protected static function getData()
+    {
+        return [];
+    }
+
+    /**
+     * @return TypoScriptFrontendController
+     */
+    protected static function getTypoScriptFrontendController()
+    {
+        return $GLOBALS['TSFE'];
+    }
+
+    /**
+     * @param array $arguments
+     * @param \Closure $renderChildrenClosure
+     * @param RenderingContextInterface $renderingContext
+     * @return string|array
+     */
+    public static function renderStatic(
+        array $arguments,
+        \Closure $renderChildrenClosure,
+        RenderingContextInterface $renderingContext)
+    {
+        $data = static::getTypoScriptFrontendController()->cObj->data;
+        if (empty($arguments['as'])) {
+            return $data;
+        } else {
+            $provider = $renderingContext->getVariableProvider();
+            $provider->add($arguments['as'], $data);
+            $content = $renderChildrenClosure();
+            $provider->remove($arguments['as']);
+            return $content;
+        }
+    }
+}
diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/Info/ContentViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/Info/ContentViewHelper.php
new file mode 100644 (file)
index 0000000..3366248
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+namespace TYPO3\CMS\Fluid\ViewHelpers\Info;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+/**
+ * Content Information ViewHelper
+ *
+ * Fetches an array of page metadata contained in
+ * ``$GLOBALS['TSFE']->cObj->data`` and either returns
+ * the array or assigns it to template variables in
+ * tag content if using the ``as`` argument.
+ *
+ * = Examples =
+ *
+ * <code title="With as argument">
+ * <f:info.content as="contentInfo">
+ *     The content element header is {contentInfo.header}.
+ * </f:info.content>
+ * </code>
+ *
+ * <code title="Returning an array">
+ * <f:alias map="{contentInfo: '{f:info.content()}'}">
+ *     The content element header is {contentInfo.header}
+ * </f:alias>
+ * </code>
+ */
+class ContentViewHelper extends AbstractInfoViewHelper
+{
+    /**
+     * @return array
+     */
+    protected static function getData()
+    {
+        return static::getTypoScriptFrontendController()->cObj->data;
+    }
+}
diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/Info/PageViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/Info/PageViewHelper.php
new file mode 100644 (file)
index 0000000..c90ca28
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+namespace TYPO3\CMS\Fluid\ViewHelpers\Info;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+/**
+ * Page Information ViewHelper
+ *
+ * Fetches an array of page metadata contained in
+ * ``$GLOBALS['TSFE']->page`` and either returns
+ * the array or assigns it to template variables in
+ * tag content if using the ``as`` argument.
+ *
+ * = Examples =
+ *
+ * <code title="With as argument">
+ * <f:info.page as="pageInfo">
+ *     The page title is {pageInfo.title}.
+ * </f:info.page>
+ * </code>
+ *
+ * <code title="Returning an array">
+ * <f:alias map="{pageInfo: '{f:info.page()}'}">
+ *     The page title is {pageInfo.title}.
+ * </f:alias>
+ * </code>
+ */
+class PageViewHelper extends AbstractInfoViewHelper
+{
+    /**
+     * @return array
+     */
+    protected static function getData()
+    {
+        return static::getTypoScriptFrontendController()->page;
+    }
+}