Semicolon
Semicolon

Reputation: 1914

SilverStripe properly removing page by code

This seems to be a difficult area in SS.

I'm trying to delete a page by code using: $page->doArchive();

This however, leaves a ghost of the page in the SiteTree menu with an option to "restore draft". This however breaks the functionality of my module and is not desired at all.

How do you completely remove a page so that user cannot see / recover it?

EDIT: Also tried:

DB::query("DELETE FROM SomePage WHERE ForeignID = {$this->ID}");
DB::query("DELETE FROM SomePage_Live WHERE ForeignID = {$this->ID}");
DB::query("DELETE FROM SomePage_versions WHERE ForeignID = {$this->ID}");

And even cleared the MySQL Table manually (phpmyadmin) and STILL there it shows the page (crossed out in menu) with a restore draft option.

EDIT2: This piece of messy code will delete all records and remove the ability to restore a draft. However, the deleted page icon is still shown in the sitetree and will dissappear after a refresh.

DB::query("DELETE FROM SomePage WHERE ForeignID = {$this->ID}");
DB::query("DELETE FROM SomePage_Live WHERE ForeignID = {$this->ID}");
DB::query("DELETE FROM SomePage_versions WHERE ForeignID = {$this->ID}");
DB::query("DELETE FROM SomePage_versions WHERE ForeignID = 0");

DB::query("DELETE FROM SiteTree WHERE ID = {$pageID}");
DB::query("DELETE FROM SiteTree_Live WHERE ID = {$pageID}");
DB::query("DELETE FROM SiteTree_versions WHERE RecordID = {$pageID}");

Question: How do prevent the "flash" of deleted page icon?

Upvotes: 0

Views: 610

Answers (1)

bummzack
bummzack

Reputation: 5875

SilverStripe doesn't delete, it uses archive, which you can do via $page->doArchive();. I think if you're writing a module, you should definitely use this way of deleting pages, as it's what somebody would expect. Unless your module is about completely removing records from the Database…

If you really need to clean up the Database and want to do things manually, you also need to hook into the site-tree JavaScript (entwine), so that it will properly refresh the site-tree once you delete a page.

Update:

Here's a small piece of code how you could force an update on some or all tree nodes.

Create a custom entwine script, let's save it at mysite/javascript/tree-hook.js

(function($) {
    $(function(){
        // ss.tree "namespace" is needed to be able to access the tree methods
        $.entwine('ss.tree', function($){
            // Match your custom button selector
            $(".myCustomButton").entwine({
                onmatch: function () {
                    this._super();
                    // bind a namespaced click handler (for easy removal)
                    $(this).on("click.treeHook", function (e) {
                        e.preventDefault();
                        e.stopImmediatePropagation();
                        // pass in all the tree node IDs you need to update
                        $(".cms-tree").updateNodesFromServer([/* IDs */]);
                    });
                },
                onunmatch: function () {
                    this._super();
                    $(this).off("click.treeHook");
                }
            })
        });
    });
})(jQuery);

Then load in the custom JS into the CMS by adding the following to your config.yml:

LeftAndMain:
  extra_requirements_javascript:
    - 'mysite/javascript/tree-hook.js'

For more information about entwine, consider looking at this beginners guide or the github repository

Upvotes: 3

Related Questions