arambalakjian / DataObject-as-Page

A SilverStripe module for displaying DataObjects as Pages
53 stars 27 forks source link

SS3.0.5 Multiple DOAPs on Same Site #33

Closed aledbrown closed 11 years ago

aledbrown commented 11 years ago

This is a great module. I'm not getting on too well with SS3.1 beta. It's breaking too many things for me so I'm building in SS3.0.5 and hoping I can upgrade when 3.1 is final.

I've downloaded the latest version of DOAP for 3.1 then I've downgraded the function names to work with 3.0.5. As a test for my issue I also downloaded the 3.0 branch of DOAP and it happens on that too.

If you make two DOAP areas on a site. I built one, it was working perfectly. Then I built another and carefully made sure the item class names were correct. It was working fine. At the end of the day I went back to test the original DOAP I'd made and the $Link generated was to the /show/item of the newer DOAP area of the site not to the URL it should have been.

If I reordered the site tree so that the original DOAP came 1st it would work the other way around. It also worked if I alternately unpublished the ListingPages from the site. That had me thinking.

I traced the problem to this function in DataObjectAsPage.php:

public function getListingPage(){

    $listingClass = $this->stat('listing_page_class');

    if(Controller::curr()->ClassName == $listingClass)
    {
        $listingPage = Controller::curr();
    }
    else
    {
        $listingPage = $listingClass::get()->First();
    }

    return $listingPage;        
}

The get()->First(); bit is causing my issue.

So what I've done as a temporary fix so that I don't change code in the module is override that function in my DOAP sub class Item.php files.

I've set it to:

public function getListingPage(){
    $listingClass = $this->stat('listing_page_class');
    $listingPage = Controller::curr();
    return $listingPage;
}

I've done that on the Item classes where I'm having this issue.

There's no need to change the actual $Link function because it calls getListingPage() anyway.

That seems to have fixed my issue. Hope this info might help someone else.

I don't want to do a pull request on the module because I don't really understand the significance of the original function.

T3nd4n commented 11 years ago

The same problem here, Thank's Aledbrown that help me. But with this fix, doesn't work anymore the template for the DataObjectAsPage and render's with a default template.

mierla commented 11 years ago

So Aledbrown's fix helped me on 3.1, until I tried to link back to the page from a different class template - it now routes from that controller.

AHHA! I seem to have fixed it: I had private static $listingpageclass = 'TheClass', and upon examining the code, I only found references to $listing_page_class. I changed both my DOAP classes to $listing_page_class, and now both function as expected, even without Aledbrown's fix.

So really, it's just a little documentation change that needs to happen.

arambalakjian commented 11 years ago

Hi Guys,

Sorry I haddn't got back to you earlier about this.

It looks like the problem was origonally caused by not setting the $listing_page_class value correctly, hence it was falling back to just getting the first listing page it could.

If you make two different DOAP extensions then you need two different listing page classes too, each being set to their respective DOAP $listing_page_class variable.

That should solve this issue and is the reason for the getListingPageFunction() :)

Aram

aledbrown commented 11 years ago

Hi Aram

I've been testing this issue with Three DOAP's on a site today (SS3.1). All with correctly assigned $listing_class variables as per the SSBits tutorial. I commented out my overwritten getListingPage() function and things fell apart.

Your comment mentions setting $listing_page_class which I did not have. I only had $listing_class which was set correctly on each DOAP.

I commented out the $listing_class and added $listing_page_class as below:

//static $listing_class = 'NewsListingPage'; static $listing_page_class = 'NewsListingPage';

Did a dev/build and a flush and it does indeed work as you suggest with v3.1 with any number of DOAPs on the same site. :)

The original documentation does not have $listing_page_class though and so that's where our confusion has come from.

The code after "mysite/code/Product.php (Extends DataObjectAsPage)" on http://www.ssbits.com/tutorials/2012/dataobject-as-pages-the-module/ needs a slight tweak, I think?

I hope that helps.

What I'm struggling with is getting filters to work with DOAP. Would it be possible to make a short tutorial to show how a few simple filters can be used on the same DOAP?

Many thanks Aled