Jump to content

Returning a repeater item page ID (when not logged in)


a-ok
 Share

Recommended Posts

I'm wanting to retrieve a repeater (via it's page ID) and was currently using this (with $testID being the returned repeater item page ID)

$result = $pages->get("id=$testID");

This works fine when I am logged in (as superuser) but when logged out it returns null (I guess because it's an admin page). I have tried doing this with no such luck...

$result = $pages->get("id=$testID, include=all");

Any ideas? Thanks.

Link to comment
Share on other sites

15 minutes ago, abdus said:

Getting a page with its id should return that page regardless of its status or access settings. I am able to get a repeater page by its id using


$pages->get(1039);

https://processwire.com/api/ref/pages/get/

Using $pages->find(), however, takes page access and status into account

I thought as much but this seems to only work when I'm logged in...

<?php if ($this->input->post->action == 'randomTest') : // Get random test ?>

	<?php

		$pageID = $sanitizer->selectorValue($this->input->post->pageID);
		$testID = $sanitizer->selectorValue($this->input->post->testID);

		$result = $pages->get("id=$testID");

		$text = $result->typefaces_detail_tests_text; // Random test text
		$size = $result->typefaces_detail_tests_size; // Related font size from random test text

		if ($result->next->id) {
			$next = $result->next(); // Next test element in the repeater
		} else {
			$next = $result->siblings->first; // If next doesn't exist (end of repeater rows) then back to start
		}

		return json_encode(['html' => $text, 'size' => $size, 'next' => $next->id]);

	?>

<?php endif; ?>
$('.typeface__toolbar .reload a.trigger').on('click', function(e) {

			e.preventDefault();

			var $this = $(this),
			pageID = $this.attr('data-page-id'),
			testID = $this.attr('data-test-id');

			$.ajax({
				url: rootURL + 'ajax/',
				type: 'POST',
				dataType: 'json',
				data: {
					'action': 'randomTest',
					'pageID': pageID,
					'testID': testID
				},
				success: function(data) {
					console.log(data);
				},
				error: function (data) {
					console.log(data);
	      		}
			}).done(function(data) {
				console.log(testID);
			});

		});

 

Link to comment
Share on other sites

I had a situation when I needed to get next repeater item and I also was able to get it working only with "include=all". I'm not sure, but it looks like normal behavior as these pages are under "admin" branch.

Link to comment
Share on other sites

4 minutes ago, abdus said:

Does $testID gets posted correctly? There has to be a problem with the page id. Can you check the item id again (Admin > Repeaters > some-page-name > repeater-item)?

Yep, it all works when I'm logged in... but when logged out it doesn't. Hmm...

Link to comment
Share on other sites

This seems to now work... partially

if ($result->next("include=all")) {
			$next = $result->next("include=all"); // Next test element in the repeater
		} else {
			$next = $result->siblings->first; // If next doesn't exist (end of repeater rows) then back to start
		}

Only issue is that the if condition isn't returning false or NULL to then go back to start.. it seems to return '0'.

Link to comment
Share on other sites

This is what I had to do...

if ($result->next("include=all")->id != 0) {
	$next = $result->next("include=all")->id; // Next test element in the repeater
} else {
	$next = $result->siblings("include=all")->first->id; // If next doesn't exist (end of repeater rows) then back to start
}

 

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