Jump to content

Where should I put PageArray Extensions?


MadHatter
 Share

Recommended Posts

Hi,

I've thrown together a quick class that extends PageArray to allow me to append unique pages to a PageArray (so if the appended PageArray contains pages which already in the PageArray then they are ignored). I've put the class inside _init.php but I just want to check what the best place to set this class would be as I'm not sure this is the right place to save it.

Thanks,

MadHatter

Link to comment
Share on other sites

Just checked the cheetsheet to see if you can do the job with in-built tools. Why not use unique method after appending a page to a PageArray?

And on your question. If you are extending PageArray class, I think you better do it via a module. If you're just adding a function or a class you can put it in _functions.inc file (or any name you like) and include it either in _init.php (I assume you prepemding each template with this file like in delayed output aproach described here), or include it on per template basis in template file.

Edited by Ivan Gretsky
Link to comment
Share on other sites

Yes, that would make more sense. It would still be nice to know where to put extended classes though.

EDIT: Just tried the unique() function, it works but doesn't let me sort the results in the way I'd like. I need to do an initial search to get a PageArray and then add the results of the second PageArray to the first, ignoring duplicates. unique() seemed to be returning the results in the same order each time, which is not desirable. I've extended the PageArray like so:

class UniquePageArray extends PageArray {
	public function appendUnique($pageArray){
		foreach($pageArray as $page){
			if(!$this->has($page)){
				$this->append($page);
			}
		}
	}
}

This has the advantage of having the appended Pages appear at the end of the PageArray.

Link to comment
Share on other sites

Well, PageArray is already unique.

Considering this example, you'll end up with 1 entry in PageArray:

$pa = new PageArray();
$pa->add($pages->get("/about/"));
$pa->add($pages->get("/about/"));
echo $pa;
  • Like 1
Link to comment
Share on other sites

@Soma is it still unique if you append one PageArray to the other? I've just tried reverting back to the append() function but it's still showing my results in the wrong order (i.e randomizing all the results instead of showing me my exact results before the extended search results). If I use the appendUnique() function then I get them in the order I need them.

@kongondo The sort function only helps for sorting by a field, what I'm trying to do is find results where the title of the page matches the query string, randomzing it, then searching the summary and content for the same string, randominzing that, then adding that to the end of the first result. There are more searches to take into account (due to some ridiculous client demands) so I need to append the results of each one to the end of the PageArray and check each is unique.

My extended Class seems to work fine, my question was really about where to place the code for the Class.

Link to comment
Share on other sites

The issue with this is that the results from the first search will get replaced when the second PageArray is appended if there is an overlap in the results. For example, one of my searches returns back 1 result intially, then the second search returns back another 4. The single result form the first search is in the second search so it gets moved in the final PageArray due to being replaced. With my extended class there is no removing of pages so it maintains the correct ordering I require.

Link to comment
Share on other sites

Could be slow if you have a lot of pages, but for a reasonable number this might work.

foreach($pageArray2 as $p){
    if ($pageArray1->has($p)) continue;
    $pageArray1->append($p);
}

*untested and written in the browser.

  • Like 1
Link to comment
Share on other sites

I see. Yes that's somewhat different to what WireArray does, but you're way is fine then.

You can add your own method to PageArray using hook then instead of a new class.

wire()->addHook("WireArray::addUnique", null, function($event){
    $item = $event->arguments(0);
    if($item instanceof WireArray){
        foreach($item as $p){
            if(!$event->object->has($p)) $event->object->add($p);
        }
    } else {
        if(!$event->object->has($item)) $event->object->add($p);
    }
});

$pa = new PageArray();
$pa->add($pages->find("template=basic-page"));
$pa->addUnique($pages->get("/about/")->children);

$content .= $pa;
  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...