Search code examples
pimcore

pimcore workflow management - disabling Save and publish


In pimcore I tried implementing the workflow management for objects. Workflow is working fine. But, save and publish button are still appearing, how can I remove these features if workflow is enabled. 1. Disabling Save, publish, unpublish and delete to the user if workflow is enabled. 2. Removing same options (save, publish, unpublish and delete) on right clicking the object.


Solution

  • If you want to disable buttons you have to overwrite pimcore object.js(pimcore/static6/js/pimcore/object/object.js) and tree.js(pimcore/static6/js/pimcore/object/tree.js). First create plugin. Then create object.js in static(Remember to add js paths to plugin.xml) and add code:

    pimcore.registerNS("pimcore.object.object");
    
    pimcore.object.object = Class.create(pimcore.object.object, {
    
    getLayoutToolbar : function () {
    
        if (!this.toolbar) {
    
            var buttons = [];
    
            this.toolbarButtons = {};
    
            this.toolbarButtons.save = new Ext.SplitButton({
                text: t('save'),
                iconCls: "pimcore_icon_save",
                scale: "medium",
                handler: this.save.bind(this, "unpublish"),
                menu:[{
                    text: t('save_close'),
                    iconCls: "pimcore_icon_save",
                    handler: this.unpublishClose.bind(this)
                }]
            });
    
            this.toolbarButtons.publish = new Ext.SplitButton({
                text: t('save_and_publish'),
                iconCls: "pimcore_icon_publish",
                scale: "medium",
                handler: this.publish.bind(this),
                menu: [{
                    text: t('save_pubish_close'),
                    iconCls: "pimcore_icon_save",
                    handler: this.publishClose.bind(this)
                },
                    {
                        text: t('save_only_new_version'),
                        iconCls: "pimcore_icon_save",
                        handler: this.save.bind(this, "version")
                    },
                    {
                        text: t('save_only_scheduled_tasks'),
                        iconCls: "pimcore_icon_save",
                        handler: this.save.bind(this, "scheduler","scheduler")
                    }
                ]
            });
    
            this.toolbarButtons.unpublish = new Ext.Button({
                text: t('unpublish'),
                iconCls: "pimcore_icon_unpublish",
                scale: "medium",
                handler: this.unpublish.bind(this)
            });
    
            this.toolbarButtons.remove = new Ext.Button({
                tooltip: t("delete"),
                iconCls: "pimcore_icon_delete",
                scale: "medium",
                handler: this.remove.bind(this)
            });
    
            this.toolbarButtons.rename = new Ext.Button({
                tooltip: t('rename'),
                iconCls: "pimcore_icon_key pimcore_icon_overlay_go",
                scale: "medium",
                handler: function () {
                    var options = {
                        elementType: "object",
                        elementSubType: this.data.general.o_type,
                        id: this.id,
                        default: this.data.general.o_key
                    };
                    pimcore.elementservice.editElementKey(options);
                }.bind(this)
            });
    
    
    
    
    
    
            //This code is for save&publish buttons
            if (this.isAllowed("save")) {
                buttons.push(this.toolbarButtons.save);
            }
            if (this.isAllowed("publish")) {
                buttons.push(this.toolbarButtons.publish);
            }
            if (this.isAllowed("unpublish") && !this.data.general.o_locked) {
                buttons.push(this.toolbarButtons.unpublish);
            }
    
    
    
    
    
    
            buttons.push("-");
    
            if(this.isAllowed("delete") && !this.data.general.o_locked) {
                buttons.push(this.toolbarButtons.remove);
            }
            if(this.isAllowed("rename") && !this.data.general.o_locked) {
                buttons.push(this.toolbarButtons.rename);
            }
    
            var reloadConfig = {
                xtype: "splitbutton",
                tooltip: t('reload'),
                iconCls: "pimcore_icon_reload",
                scale: "medium",
                handler: this.reload.bind(this, this.data.currentLayoutId)
            };
    
            if (this.data["validLayouts"] && this.data.validLayouts.length > 1) {
                var menu = [];
                for (var i = 0; i < this.data.validLayouts.length; i++) {
                    var menuLabel = ts(this.data.validLayouts[i].name);
                    if (Number(this.data.currentLayoutId) == this.data.validLayouts[i].id) {
                        menuLabel = "<b>" + menuLabel + "</b>";
                    }
                    menu.push({
                        text: menuLabel,
                        iconCls: "pimcore_icon_reload",
                        handler: this.reload.bind(this, this.data.validLayouts[i].id)
                    });
                }
                reloadConfig.menu = menu;
            }
    
            buttons.push(reloadConfig);
    
            if (pimcore.elementservice.showLocateInTreeButton("object")) {
                if (this.data.general.o_type != "variant" || this.data.general.showVariants) {
                    buttons.push({
                        tooltip: t('show_in_tree'),
                        iconCls: "pimcore_icon_show_in_tree",
                        scale: "medium",
                        handler: this.selectInTree.bind(this, this.data.general.o_type)
                    });
                }
            }
    
            buttons.push({
                tooltip: t("show_metainfo"),
                iconCls: "pimcore_icon_info",
                scale: "medium",
                handler: this.showMetaInfo.bind(this)
            });
    
            buttons.push("-");
            buttons.push({
                xtype: 'tbtext',
                text: t("id") + " " + this.data.general.o_id,
                scale: "medium"
            });
    
            buttons.push("-");
            buttons.push({
                xtype: 'tbtext',
                text: ts(this.data.general.o_className),
                scale: "medium"
            });
    
            // version notification
            this.newerVersionNotification = new Ext.Toolbar.TextItem({
                xtype: 'tbtext',
                text: '&nbsp;&nbsp;<img src="/pimcore/static6/img/flat-color-icons/medium_priority.svg" style="height: 16px;" align="absbottom" />&nbsp;&nbsp;'
                    + t("this_is_a_newer_not_published_version"),
                scale: "medium",
                hidden: true
            });
    
            buttons.push(this.newerVersionNotification);
    
            //workflow management
            pimcore.elementservice.integrateWorkflowManagement('object', this.id, this, buttons);
    
            // check for newer version than the published
            if (this.data.versions.length > 0) {
                if (this.data.general.o_modificationDate < this.data.versions[0].date) {
                    this.newerVersionNotification.show();
                }
            }
    
            this.toolbar = new Ext.Toolbar({
                id: "object_toolbar_" + this.id,
                region: "north",
                border: false,
                cls: "main-toolbar",
                items: buttons,
                overflowHandler: 'scroller'
            });
    
            this.toolbar.on("afterrender", function () {
                window.setTimeout(function () {
                    if (!this.data.general.o_published) {
                        this.toolbarButtons.unpublish.hide();
                    } else if (this.isAllowed("publish")) {
                        this.toolbarButtons.save.hide();
                    }
                }.bind(this), 500);
            }.bind(this));
        }
    
        return this.toolbar;
    }
    });
    

    You have to do the same with tree.js:

    pimcore.object.tree = Class.create({
    onTreeNodeContextmenu: function (tree, record, item, index, e, eOpts ) {
        e.stopEvent();
    
        tree.select();
    
        var menu = new Ext.menu.Menu();
    
        var perspectiveCfg = this.perspectiveCfg;
    
        var object_types = pimcore.globalmanager.get("object_types_store_create");
    
        var objectMenu = {
            objects: [],
            importer: [],
            ref: this
        };
    
        var groups = {
            importer: {},
            objects: {}
        };
    
        var tmpMenuEntry;
        var tmpMenuEntryImport;
        var $this = this;
    
        object_types.each(function (classRecord) {
    
            if ($this.config.allowedClasses && !in_array(classRecord.get("id"), $this.config.allowedClasses)) {
                return;
            }
    
            tmpMenuEntry = {
                text: classRecord.get("translatedText"),
                iconCls: "pimcore_icon_object pimcore_icon_overlay_add",
                handler: $this.addObject.bind($this, classRecord.get("id"), classRecord.get("text"), tree, record)
            };
    
            // add special icon
            if (classRecord.get("icon") != "/pimcore/static6/img/flat-color-icons/timeline.svg") {
                tmpMenuEntry.icon = classRecord.get("icon");
                tmpMenuEntry.iconCls = "";
            }
    
            tmpMenuEntryImport = {
                text: classRecord.get("translatedText"),
                iconCls: "pimcore_icon_object pimcore_icon_overlay_add",
                handler: $this.importObjects.bind($this, classRecord.get("id"), classRecord.get("text"), tree, record)
            };
    
            // add special icon
            if (classRecord.get("icon") != "/pimcore/static6/img/flat-color-icons/timeline.svg") {
                tmpMenuEntryImport.icon = classRecord.get("icon");
                tmpMenuEntryImport.iconCls = "";
            }
    
    
            // check if the class is within a group
            if(classRecord.get("group")) {
                if(!groups["objects"][classRecord.get("group")]) {
                    groups["objects"][classRecord.get("group")] = {
                        text: classRecord.get("group"),
                        iconCls: "pimcore_icon_folder",
                        hideOnClick: false,
                        menu: {
                            items: []
                        }
                    };
                    groups["importer"][classRecord.get("group")] = {
                        text: classRecord.get("group"),
                        iconCls: "pimcore_icon_folder",
                        hideOnClick: false,
                        menu: {
                            items: []
                        }
                    };
                    objectMenu["objects"].push(groups["objects"][classRecord.get("group")]);
                    objectMenu["importer"].push(groups["importer"][classRecord.get("group")]);
                }
    
                groups["objects"][classRecord.get("group")]["menu"]["items"].push(tmpMenuEntry);
                groups["importer"][classRecord.get("group")]["menu"]["items"].push(tmpMenuEntryImport);
            } else {
                objectMenu["objects"].push(tmpMenuEntry);
                objectMenu["importer"].push(tmpMenuEntryImport);
            }
        });
    
    
        var isVariant = record.data.type == "variant";
    
        if (record.data.permissions.create) {
            if (!isVariant) {
                if (perspectiveCfg.inTreeContextMenu("object.add")) {
                    menu.add(new Ext.menu.Item({
                        text: t('add_object'),
                        iconCls: "pimcore_icon_object pimcore_icon_overlay_add",
                        hideOnClick: false,
                        menu: objectMenu.objects
                    }));
                }
            }
    
            if (record.data.allowVariants && perspectiveCfg.inTreeContextMenu("object.add")) {
                menu.add(new Ext.menu.Item({
                    text: t("add_variant"),
                    iconCls: "pimcore_icon_variant",
                    handler: this.createVariant.bind(this, tree, record)
                }));
            }
    
            if (!isVariant) {
    
                if (perspectiveCfg.inTreeContextMenu("object.addFolder")) {
                    menu.add(new Ext.menu.Item({
                        text: t('add_folder'),
                        iconCls: "pimcore_icon_folder pimcore_icon_overlay_add",
                        handler: this.addFolder.bind(this, tree, record)
                    }));
                }
    
                if (perspectiveCfg.inTreeContextMenu("object.importCsv")) {
                    menu.add({
                        text: t('import_csv'),
                        hideOnClick: false,
                        iconCls: "pimcore_icon_object pimcore_icon_overlay_upload",
                        menu: objectMenu.importer
                    });
                }
    
                menu.add("-");
    
                //paste
                var pasteMenu = [];
    
                if (perspectiveCfg.inTreeContextMenu("object.paste")) {
                    if (pimcore.cachedObjectId && record.data.permissions.create) {
                        pasteMenu.push({
                            text: t("paste_recursive_as_childs"),
                            iconCls: "pimcore_icon_paste",
                            handler: this.pasteInfo.bind(this, tree, record, "recursive")
                        });
                        pasteMenu.push({
                            text: t("paste_recursive_updating_references"),
                            iconCls: "pimcore_icon_paste",
                            handler: this.pasteInfo.bind(this, tree, record, "recursive-update-references")
                        });
                        pasteMenu.push({
                            text: t("paste_as_child"),
                            iconCls: "pimcore_icon_paste",
                            handler: this.pasteInfo.bind(this, tree, record, "child")
                        });
    
    
                        if (record.data.type != "folder") {
                            pasteMenu.push({
                                text: t("paste_contents"),
                                iconCls: "pimcore_icon_paste",
                                handler: this.pasteInfo.bind(this, tree, record, "replace")
                            });
                        }
                    }
                }
    
                if (!isVariant) {
                    if (pimcore.cutObject && record.data.permissions.create) {
                        pasteMenu.push({
                            text: t("paste_cut_element"),
                            iconCls: "pimcore_icon_paste",
                            handler: function () {
                                this.pasteCutObject(pimcore.cutObject,
                                    pimcore.cutObjectParentNode, record, this.tree);
                                pimcore.cutObjectParentNode = null;
                                pimcore.cutObject = null;
                            }.bind(this)
                        });
                    }
    
                    if (pasteMenu.length > 0) {
                        menu.add(new Ext.menu.Item({
                            text: t('paste'),
                            iconCls: "pimcore_icon_paste",
                            hideOnClick: false,
                            menu: pasteMenu
                        }));
                    }
                }
            }
        }
    
        if (!isVariant) {
            if (record.data.id != 1 && record.data.permissions.view && perspectiveCfg.inTreeContextMenu("object.copy")) {
                menu.add(new Ext.menu.Item({
                    text: t('copy'),
                    iconCls: "pimcore_icon_copy",
                    handler: this.copy.bind(this, tree, record)
                }));
            }
    
            //cut
            if (record.data.id != 1 && !record.data.locked && record.data.permissions.rename && perspectiveCfg.inTreeContextMenu("object.cut")) {
                menu.add(new Ext.menu.Item({
                    text: t('cut'),
                    iconCls: "pimcore_icon_cut",
                    handler: this.cut.bind(this, tree, record)
                }));
            }
        }
    
        //publish
        if (record.data.type != "folder" && !record.data.locked) {
            if (record.data.published && record.data.permissions.unpublish && perspectiveCfg.inTreeContextMenu("object.unpublish")) {
                menu.add(new Ext.menu.Item({
                    text: t('unpublish'),
                    iconCls: "pimcore_icon_unpublish",
                    handler: this.publishObject.bind(this, tree, record, 'unpublish')
                }));
            } else if (!record.data.published && record.data.permissions.publish && perspectiveCfg.inTreeContextMenu("object.publish")) {
                menu.add(new Ext.menu.Item({
                    text: t('publish'),
                    iconCls: "pimcore_icon_publish",
                    handler: this.publishObject.bind(this, tree, record, 'publish')
                }));
            }
        }
    
    
        if (record.data.permissions["delete"] && record.data.id != 1 && !record.data.locked && perspectiveCfg.inTreeContextMenu("object.delete")) {
            menu.add(new Ext.menu.Item({
                text: t('delete'),
                iconCls: "pimcore_icon_delete",
                handler: this.remove.bind(this, tree, record)
            }));
        }
    
        if (record.data.permissions.rename && record.data.id != 1 && !record.data.locked && perspectiveCfg.inTreeContextMenu("object.rename")) {
            menu.add(new Ext.menu.Item({
                text: t('rename'),
                iconCls: "pimcore_icon_key pimcore_icon_overlay_go",
                handler: this.editObjectKey.bind(this, tree, record)
            }));
        }
    
    
        // advanced menu
        var advancedMenuItems = [];
        var user = pimcore.globalmanager.get("user");
    
        if (record.data.permissions.create && perspectiveCfg.inTreeContextMenu("object.searchAndMove")) {
            advancedMenuItems.push({
                text: t('search_and_move'),
                iconCls: "pimcore_icon_search pimcore_icon_overlay_go",
                handler: this.searchAndMove.bind(this, tree, record)
            });
        }
    
        if (record.data.id != 1 && user.admin) {
            var lockMenu = [];
            if (record.data.lockOwner && perspectiveCfg.inTreeContextMenu("object.unlock")) { // add unlock
                lockMenu.push({
                    text: t('unlock'),
                    iconCls: "pimcore_icon_lock pimcore_icon_overlay_delete",
                    handler: function () {
                        pimcore.elementservice.lockElement({
                            elementType: "object",
                            id: record.data.id,
                            mode: "null"
                        });
                    }.bind(this)
                });
            } else {
                if (perspectiveCfg.inTreeContextMenu("object.lock")) {
                    lockMenu.push({
                        text: t('lock'),
                        iconCls: "pimcore_icon_lock pimcore_icon_overlay_add",
                        handler: function () {
                            pimcore.elementservice.lockElement({
                                elementType: "object",
                                id: record.data.id,
                                mode: "self"
                            });
                        }.bind(this)
                    });
                }
    
                if (perspectiveCfg.inTreeContextMenu("object.lockAndPropagate")) {
                    lockMenu.push({
                        text: t('lock_and_propagate_to_childs'),
                        iconCls: "pimcore_icon_lock pimcore_icon_overlay_go",
                        handler: function () {
                            pimcore.elementservice.lockElement({
                                elementType: "object",
                                id: record.data.id,
                                mode: "propagate"
                            });
                        }.bind(this)
                    });
                }
            }
    
            if(record.data.locked) {
                // add unlock and propagate to children functionality
                if (perspectiveCfg.inTreeContextMenu("object.unlockAndPropagate")) {
                    lockMenu.push({
                        text: t('unlock_and_propagate_to_children'),
                        iconCls: "pimcore_icon_lock pimcore_icon_overlay_delete",
                        handler: function () {
                            pimcore.elementservice.unlockElement({
                                elementType: "object",
                                id: record.data.id
                            });
                        }.bind(this)
                    });
                }
            }
    
            if (lockMenu.length > 0) {
                advancedMenuItems.push({
                    text: t('lock'),
                    iconCls: "pimcore_icon_lock",
                    hideOnClick: false,
                    menu: lockMenu
                });
            }
        }
    
        menu.add("-");
    
        if(advancedMenuItems.length) {
            menu.add({
                text: t('advanced'),
                iconCls: "pimcore_icon_more",
                hideOnClick: false,
                menu: advancedMenuItems
            });
        }
    
        if (perspectiveCfg.inTreeContextMenu("object.reload")) {
            menu.add({
                text: t('refresh'),
                iconCls: "pimcore_icon_reload",
                handler: this.reloadNode.bind(this, tree, record)
            });
        }
    
        pimcore.helpers.hideRedundantSeparators(menu);
    
        pimcore.plugin.broker.fireEvent("prepareObjectTreeContextMenu", menu, this, record);
    
        menu.showAt(e.pageX+1, e.pageY+1);
    },
    });