Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically you need to create a function in your Portfolio Page Controller (or in whatever page you need this logic to be).</p> <p>Here are 2 examples. First one just gets all the existing <code>FeaturedImage</code> from the database and returns then in random order:</p> <pre><code>function AllFeaturedImages() { return FeaturedImage::get()-&gt;sort('RAND()'); } </code></pre> <p>And this one get all the <code>FeaturedImage</code> from the page's children's children and return them in random order:</p> <pre><code>function DescendantFeaturedImages() { $featuredImages = array(); foreach ($this-&gt;Children() as $child) { foreach ($child-&gt;Children() as $grandChild) { $images = $grandChild-&gt;FeaturedImage(); if ( $images ) { $featuredImages = array_merge( $featuredImages, $images-&gt;toArray() ); } } } shuffle($featuredImages); return ArrayList::create($featuredImages); } </code></pre> <p>If the <code>FeaturedImage</code> relation is just a <code>has_one</code>, this changes a little bit:</p> <pre><code>function DescendantFeaturedImages() { $featuredImages = array(); foreach ($this-&gt;Children() as $child) { foreach ($child-&gt;Children() as $grandChild) { $image = $grandChild-&gt;FeaturedImage(); if ( $image ) { array_push( $featuredImages, $image ); } } } shuffle($featuredImages); return ArrayList::create($featuredImages); } </code></pre> <p>Then in your Portfolio Page template you can just loop through the <code>FeaturedImage</code> by calling the function name. So here, either <code>$AllFeaturedImages</code> or <code>$DescendantFeaturedImages</code>. In your case you'll get something like:</p> <pre><code>&lt;% loop $DescendantFeaturedImages %&gt; &lt;img src="$URL"/&gt; &lt;% end_loop %&gt; </code></pre> <p>I could find one example in the SilverStirpe tutorials using a Controller function: <a href="http://doc.silverstripe.org/framework/en/tutorials/2-extending-a-basic-site" rel="nofollow">http://doc.silverstripe.org/framework/en/tutorials/2-extending-a-basic-site</a></p> <p>Let me know how this goes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload