Jump to content

How to do getRandom or equivalent for repeater?


MarcC
 Share

Recommended Posts

So I tried using $page->repeater->getRandom() in some template code but it didn't seem to work. Should this work? If not, what is the recommended way of working with a single, randomly-chosen repeater item?

Thanks!

Link to comment
Share on other sites

Having a repeater field called "rep" on my home template I added this to template file:

echo $page->rep->getRandom()->name;

... and on each page load it echoed the name of one random item. Actually tested it with both 2.2 and 2.3 and neither had any problems with this.

So, what exactly is your code doing / not doing when it doesn't seem to work?

  • Like 1
Link to comment
Share on other sites

Not sure what the issue might be here, but just wanted to post the different options for getting random items:

  • getRandom() - With no arguments, returns 1 random item (unless the array is empty)
  • getRandom(n) - Returns n random items, where n is greater than 1.
  • Like 1
Link to comment
Share on other sites

  • 10 years later...

10 years later!

Searching for a getRandom() solution from a repeater, I stumbled upon this thread.
I have the repeater_headline_movie_text with the field media_manager_v_single. My goal is to render only one repeater item randomly.
This is my bad code:

<? foreach ($page->repeater_headline_movie_text->getRandom() as $item) : ?>
					<article class="video_article">
						<h3><?= $item->headline; ?></h3>
						<?php
						$out = $item->media_manager_v_single->each(function ($resource) {
							return "<video src='{$resource->media->url}' poster='{$resource->media->poster}' width='720' height='408'  controls></video>";
						});
						echo $out;
						?>

						<div class="textwrapper">
							<?= $item->body_second; ?>
						</div>
					</article>
				<? endforeach; ?>

 

 

Link to comment
Share on other sites

getRandom() by default returns a single item (Repeater item in your case). So the foreach tries to iterate over that single Repeater item's properties instead of an array of items. There are three solutions:

  1. Drop the foreach and just do "$item = $page->repeater_headline_movie_text->getRandom();" (Might error out if the repeater is empty)
  2. Set the $alwaysArray argument for getRandom: "foreach ($page->repeater_headline_movie_text->getRandom(1, true) as $item)"
  3. Or use "$page->repeater_headline_movie_text->findRandom(1)" instead, which always returns a RepeaterPageArray
  • Like 2
  • Thanks 1
Link to comment
Share on other sites

1 hour ago, BitPoet said:

getRandom() by default returns a single item (Repeater item in your case). So the foreach tries to iterate over that single Repeater item's properties instead of an array of items. There are three solutions:

  1. Drop the foreach and just do "$item = $page->repeater_headline_movie_text->getRandom();" (Might error out if the repeater is empty)
  2. Set the $alwaysArray argument for getRandom: "foreach ($page->repeater_headline_movie_text->getRandom(1, true) as $item)"
  3. Or use "$page->repeater_headline_movie_text->findRandom(1)" instead, which always returns a RepeaterPageArray

Thanks a lot @BitPoet

Nr 3 works fine ?

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

×
×
  • Create New...