Jump to content

How to create a temporary variable containing all images of children pages


Kiwi Chris
 Share

Recommended Posts

I have a structure where sub-pages have a headerImage field, but not all sub-pages have the image set.

I want to get all of these images and pass them off to a function to render a slideshow.

I've tried:

$carouselPages = $pages->get("name=categories")->children();
$page->carouselImages = new \ProcessWire\Pageimages();
foreach ($carouselPages as $cp) {
    if ($cp->headerImage) {
        $page->carouselImages->add($cp->headerImage);
    }
}

I know if I don't save $page, then $page->carouselImages will be temporary, and I can do this kind of thing ok with text values.
But I get an error on the second line.

How is the best way to handle what I'm trying to do here?

Link to comment
Share on other sites

45 minutes ago, Kiwi Chris said:

But I get an error on the second line.

You need to supply a Page object as an argument to Pageimages(). For example:

$page->carouselImages = new \ProcessWire\Pageimages($page);

Seems like an unusual way to build a carousel though - are you using some kind of template/view separation? If not, you don't need to add the carousel images as a property of $page - you can just get your carousel pages and build the carousel markup from them directly.

$carouselPages = $pages->get("name=categories")->children("headerImage!=''");
foreach($carouselPages as $carouselPage) {
    // output your carousel markup using $carouselPage->headerImage
}

 

  • Like 1
Link to comment
Share on other sites

3 minutes ago, Robin S said:

You need to supply a Page object as an argument to Pageimages(). For example:


$page->carouselImages = new \ProcessWire\Pageimages($page);

Thanks

Seems like an unusual way to build a carousel though - are you using some kind of template/view separation? If not, you don't need to add the carousel images as a property of $page - you can just get your carousel pages and build the carousel markup from them directly.

The reason I've done it this way, is I have a carousel function that takes a PageImage object as input, as in some instances I'll use it to generate a carousel from the images field of a page, but other times I'm wanting to create the pageImages object from the headerImage field of subpages.

I'm wanting to recycle the carousel code as a function as detail pages will generate a carousel from their $page->images field, but a main page will generate its carousel from the $page->headerImage field of children.


//Generate carousel directly from $page->images field
renderCarousel($page->images);
//Generate carousel from headerImage field from all subpages
foreach ($carouselPages as $cp) {
    if ($cp->headerImage) {
        $page->carouselImages->add($cp->headerImage);
    }
}
renderCarousel($page->carouselImages);

 

 

Link to comment
Share on other sites

4 hours ago, Kiwi Chris said:

The reason I've done it this way, is I have a carousel function that takes a PageImage object as input, as in some instances I'll use it to generate a carousel from the images field of a page, but other times I'm wanting to create the pageImages object from the headerImage field of subpages.

I'm wanting to recycle the carousel code as a function as detail pages will generate a carousel from their $page->images field, but a main page will generate its carousel from the $page->headerImage field of children.


//Generate carousel directly from $page->images field
renderCarousel($page->images);
//Generate carousel from headerImage field from all subpages
foreach ($carouselPages as $cp) {
    if ($cp->headerImage) {
        $page->carouselImages->add($cp->headerImage);
    }
}
renderCarousel($page->carouselImages);

Why not tweak your renderCarousel() function to accept both type of arrays (regular & Pageimages)? Pageimages class is tightly bound to Page class, and not really fit for use in isolation, but you can use any kind of array to do your work.

<?php namespace ProcessWire;

function renderCarousel($images)
{
    if ($images instanceof WireArray) {
        // like when $images = $page->images
        $images = $images->getArray(); // turn into regular arrays
    }
    if (!reset($images) instanceof Pageimage) {
        throw new WireException('$images must be an array of images');
    }

    $out = "";
    foreach ($images as $img) {
        $out .= "<img src='$img->url' alt='$img->description'/>";
    }

    $out = "<div class='carousel'>$out</div>";
    return $out;
}


$images = [];
foreach ($carouselPages as $cp) array_merge($images, $cp->images->getArray());

// OR
$images = new WireArray();
foreach ($carouselPages as $cp) $images->import($cp->images);


echo renderCarousel($images);

 

 

 

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...