Jump to content

Get Image Url in nested repeater


louisstephens
 Share

Recommended Posts

Ill be honest, I am a bit unsure how accomplish this. I have a repeater (dev_repeater) that contains an image field set to 1 image. Nested within this repeater, is another repeater (dev_child_repeater) that allows a user to add in some urls. However, there is also a hidden field that I am trying to pass the parent repeater's image path. 

I know I can output all the data by using:

<?php
	foreach($page->dev_repeater as $repeater) {
		foreach($repeater as $url) {
			# do some stuff
		}
	}
?>

For the life of me, I can not figure out how to obtain the image url in my php to pass to a variable inside the nested foreach loop. Hopefully this made some sense.

Link to comment
Share on other sites

<?php
	foreach($page->dev_repeater as $repeater) {
		$image_field = $repeater->image_field;
		foreach($repeater->dev_child_repeater as $url) {
			$url->hidden_field = $image_field->url;
		}
	}
?>

I´m guessing something like this should be working right?

Link to comment
Share on other sites

Hi,

this is not so clear to me in part "pass variable to hidden input"?
If need to save image path value to hidden input, than maybe it's better to go with hook in backend (after page save or other).
Another option coud be to save image path value to hidden input in runtime (???), if that is the case, than try something like this:

<?php
	foreach($page->dev_repeater as $repeater) {
		$image_field = $repeater->image_field;
		foreach($repeater->dev_child_repeater as $url) {
			
			// some other stuff, link etc...
			
			// check and save hidden_field value inside runtime (???)			
			if($url->hidden_field == ""){							
				$url->of(false);
				// $url->save(); uncomment if save on second reload
				$url->hidden_field = $image_field->url; // <= this?
				$url->save('hidden_field');		
			} else {
				// hidden_field has some value
				echo $url->hidden_field;
			}
		}
	}
?>

But also if there is any option to avoid nested repeater that would be better, and also try to think about using hooks in backend to avoid save in runtime. To me it's ok when need to save "page view counters" or some other events triggered by front-end users/vistors.
Sorry if I don't understand your question well.

Regards.

Link to comment
Share on other sites

Sorry @OLSA for the confusion. I was pretty frustrated and my question probably got garbled. Hopefully I can clear up what I am trying to do, as I switched gears a bit now that I have had time to think.

The setup: 

1. Repeater - dev_repeater
    - Image field (set to allow one) - dev_image
2. Nested Repeater - dev_button_repeater
	- Text Field - URL text to display on front-end
    - URL Field - dev_url
        - allows user to enter url (ie http://google.com), but if the item needs a data-attribute (for some ajax request), then they can use a hanna code [[data]] to add the attribute to the URL on the front end

I guess what I am trying to do, in the hanna code, is get the image url so it can be present in the data attribute. Perhaps a nested repeater is not the way to go, but I didnt know of another way to allow the user to "add" multiple items all with different values.

Link to comment
Share on other sites

The $page inside the Hannah code should refer to dev_button_repeater item, so you can search where it belongs to like this:

wire('pages')->get(preg_replace("for-page-",$page->name));

That should return the dev_repeater item of the iteration. 

Thoug  don't have very clear how you are using the hannah code. Maybe like this it would work?

Use the parent repeater's image if attr is data-image and no value is set.

[[data attr="data-image"]]

Set a custom data attribute:

[[data attr="data-example" value="Something something"]]

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