[TASK] Added hook to manipulate docHeader buttons in ModuleTemplate API 69/44569/3
authorFrans Saris <franssaris@gmail.com>
Fri, 6 Nov 2015 09:39:39 +0000 (10:39 +0100)
committerFrank Nägler <frank.naegler@typo3.org>
Fri, 6 Nov 2015 13:00:24 +0000 (14:00 +0100)
Resolves: #71362
Releases: master
Change-Id: I317659e5ca91d2c2e91f2459119b68970ad5b13b
Reviewed-on: https://review.typo3.org/44569
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Frank Nägler <frank.naegler@typo3.org>
Tested-by: Frank Nägler <frank.naegler@typo3.org>
typo3/sysext/backend/Classes/Template/Components/ButtonBar.php
typo3/sysext/core/Documentation/Changelog/master/Feature-69814-ModuleTemplateAPI.rst

index b134091..e4ed423 100644 (file)
@@ -165,7 +165,15 @@ class ButtonBar
         foreach ($this->buttons as $position => $_) {
             ksort($this->buttons[$position]);
         }
-        // @todo do we want to provide a hook here?
+        // Hook for manipulating the docHeaderButtons
+        if (!empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Backend\Template\Components\ButtonBar']['getButtonsHook'])) {
+            foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Backend\Template\Components\ButtonBar']['getButtonsHook'] as $funcRef) {
+                $params = array(
+                    'buttons' => $this->buttons
+                );
+                $this->buttons = GeneralUtility::callUserFunction($funcRef, $params, $this);
+            }
+        }
         return $this->buttons;
     }
 }
index 7950c27..b3e1a36 100644 (file)
@@ -12,6 +12,7 @@ This means we have about 80 DocHeaders which are equal but not the same.
 
 The main challenge is to provide extension developers with all tools they need to build decent backend modules while maintaining control of the docHeader itself.
 
+
 Solution
 ========
 
@@ -35,6 +36,7 @@ Parts of a docHeader Currently a typical docHeader is split up into the followin
   * Left Button Bar
   * Right Button Bar
 
+
 API Components
 ==============
 
@@ -95,4 +97,46 @@ Examples of usages
         $menuItem->setActive(TRUE);
     }
     $languageMenu->addMenuItem($menuItem);
-    $this->moduleTemplate->getDocHeaderComponent()->getModuleMenuRegistry()->addMenu($languageMenu);
\ No newline at end of file
+    $this->moduleTemplate->getDocHeaderComponent()->getModuleMenuRegistry()->addMenu($languageMenu);
+
+
+ButtonBar Hook
+==============
+
+The old module rendering knew a ``$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/template.php']['docHeaderButtonsHook']`` hook
+to manipulate buttons. A similar hook is available in ModuleTemplate API as ``$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Backend\Template\Components\ButtonBar']['getButtonsHook']``.
+
+**Registering your own hook**
+
+.. code-block:: php
+
+    $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['Backend\Template\Components\ButtonBar']['getButtonsHook']['MyExt'] =
+        \MyVendor\MyExt\Hooks\ButtonBarHook::class . '->getButtons';
+
+**Example usage of the hook**
+
+.. code-block:: php
+
+    class ButtonBarHook {
+
+        /**
+         * Get buttons
+         *
+         * @param array $params
+         * @param ButtonBar $buttonBar
+         * @return array
+         */
+        public function getButtons(array $params, ButtonBar $buttonBar) {
+            $buttons = $params['buttons'];
+
+            $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
+            $button = $buttonBar->makeLinkButton();
+            $button->setIcon($iconFactory->getIcon('my-custom-icon', Icon::SIZE_SMALL));
+            $button->setTitle('My custom docHeader button');
+            $button->setOnClick('alert("Hook works");return false;');
+
+            $buttons[ButtonBar::BUTTON_POSITION_LEFT][1][] = $button;
+
+            return $buttons;
+        }
+    }