Jump to content

Adding several items to a PageArray


Robin S
 Share

Recommended Posts

I want to create a PageArray containing a number of pages that I will select by name (to use with the MarkupSimpleNavigation module).

I'm using the code below and it works fine...

$items = new PageArray();
$items->add( $pages->get("/page-1/") );
$items->add( $pages->get("/page-2/") );
$items->add( $pages->get("/page-3/") );
$items->add( $pages->get("/page-4/") );
$items->add( $pages->get("/page-5/") );

...but it seems a bit long-winded. Is there a way to add multiple pages to my PageArray in a single line? I'm not looking to create a PageArray via selector; I want to manually add pages by name.

Link to comment
Share on other sites

I know you said you didn't want to use a selector, but I'm just wondering why on earth? You are effectively doing the same as

$items = $pages->find('name=page-1|page-2|page-3|page-4|page-5, parent=/');

But your code will just execute slower.

  • Like 4
Link to comment
Share on other sites

@sforsman

Thanks - your suggestion is just what I need. I thought I wouldn't be able to use a selector because the pages I want to select don't share any common attribute (parent, template, etc). It didn't occur to me that I could just find a list of names.

@horst

Thanks for the reply - sforsman's suggestion is probably the solution I need but I'm interested to understand how the import method could be used. The import method accepts a PHP array or a WireArray - I take it you're suggesting a normal PHP array rather than a WireArray seeing as my question is how to get items into a PageArray (WireArray) in the first place.

I tried the import method and it worked with an array of page IDs...

$ids = array(1041, 1042, 1043, 1044, 1045);
$items = new PageArray();
$items->import($ids);

...but it didn't work with an array of page names...

$names = array("/page-1/", "/page-2/", "/page-3/", "/page-4/", "/page-5/");
$items = new PageArray();
$items->import($names);

Is there a way to use import with page names?

Link to comment
Share on other sites

The import methods does not search for pages. It does import either WireArrays of WireData objects (like Pages) or a simple php array of WireData objects. So it's not so important how those objects are group but the objects must be of the right type when importing.

  • Like 1
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...