Last active
October 31, 2019 17:44
-
-
Save xperseguers/2a11d915f6827adb74d8b988d97c595e to your computer and use it in GitHub Desktop.
Disable "delete" in TYPO3 context menu for pages under custom condition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('TYPO3_MODE') or die(); | |
if (TYPO3_MODE === 'BE') { | |
// You should use current timestamp (not this very value) or leave it empty | |
$GLOBALS['TYPO3_CONF_VARS']['BE']['ContextMenu']['ItemProviders'][1488274371] = | |
\Vendor\ExtensionKey\Backend\ContextMenu\PageViewRecordsElementItemProvider::class; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 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! | |
*/ | |
namespace Vendor\ExtensionKey\Backend\ContextMenu; | |
use TYPO3\CMS\Backend\ContextMenu\ItemProviders\AbstractProvider; | |
use TYPO3\CMS\Core\Imaging\Icon; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
/** | |
* This file goes to EXT:extension_key/Classes/Backend/ContextMenu/PageViewRecordsElementItemProvider.php. | |
*/ | |
class PageViewRecordsElementItemProvider extends AbstractProvider | |
{ | |
/** | |
* Checks if this provider may be called to post-process the list of context menu items (for pages). | |
* | |
* @return bool | |
*/ | |
public function canHandle(): bool | |
{ | |
return $this->table === 'pages'; | |
} | |
/** | |
* Returns the provider priority which is used for determining the order in which providers are processing items | |
* to the result array. Highest priority means provider is evaluated first. | |
* | |
* We want to be after @see \TYPO3\CMS\Backend\ContextMenu\ItemProviders\PageProvider::getPriority() anyway. | |
* | |
* BEWARE: Returned priority should logically not clash with another provider. | |
* Please check @see \TYPO3\CMS\Backend\ContextMenu\ContextMenu::getAvailableProviders() if needed. | |
* | |
* @return int | |
*/ | |
public function getPriority(): int | |
{ | |
return 90; | |
} | |
/** | |
* Registers the additional callback-module which will allow us to display a notification | |
* whenever the user tries to delete a protected page. | |
* | |
* @param string $itemName | |
* @return array | |
*/ | |
protected function getAdditionalAttributes(string $itemName): array | |
{ | |
return [ | |
// BEWARE!!! THIS MUST ALWAYS START WITH "TYPO3/CMS/" (and no "Vendor" segment here) | |
'data-callback-module' => 'TYPO3/CMS/ExtensionKey/ContextMenuActions', | |
// TODO: Add any other useful "data-" attribute you'd like to use in your JavaScript (e.g., localized messages) | |
]; | |
} | |
/** | |
* We do not "add" items to the context menu but will disable the deletion of pages | |
* if some condition is met. | |
* | |
* @param array $items | |
* @return array | |
*/ | |
public function addItems(array $items): array | |
{ | |
// TODO: implement some custom condition | |
// Current table is: $this->table | |
// Current UID is: $this->identifier | |
$disableDelete = true; | |
$itemName = 'delete'; | |
if ($disableDelete && isset($items[$itemName])) { | |
// Mark the "delete" item as disabled and show a notification message when clicked | |
/** @var \TYPO3\CMS\Core\Imaging\IconFactory $iconFactory */ | |
$iconFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Imaging\IconFactory::class); | |
$items[$itemName]['icon'] = $iconFactory->getIcon('actions-delete', Icon::SIZE_SMALL, 'overlay-hidden')->render(); | |
// We replace the action when we click the context menu item by our custom notification | |
$items[$itemName]['callbackAction'] = 'notifyCannotDelete'; | |
$items[$itemName]['additionalAttributes'] = $this->getAdditionalAttributes($itemName); | |
} | |
return $items; | |
} | |
} |
@xperseguers you should check if the item is present before modifying it. If user has no delete permission for a page, the delete item will not be there.
@tmotyl Good idea!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Purpose of this gist is to disable the "Delete" menu entry when showing the context menu of a "pages" record: