Jump to content
louisstephens

Get Image Url in nested repeater

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.

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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"]]

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Robin S
      This is a module I made as an experiment a while ago and never got around to releasing publicly. At the time it was prompted by discussions around using Repeater fields for "page builder" purposes, where the depth feature could possibly be used for elements that would be nested inside other elements. I thought it would be useful to enforce some depth rules and translate the depth data into a multi-dimensional array structure.
      I'm not using this module anywhere myself but maybe it's useful to somebody.
      Repeater Depth Helper
      This module does two things relating to Repeater fields that have the "Item depth" option enabled:
      It enforces some depth rules for Repeater fields on save. Those rules are:
      The first item must have a depth of zero. Each item depth must not be more than one greater than previous item depth. It provides a RepeaterPageArray::getDepthStructure helper method that returns a nested depth structure for a Repeater field value.
      Helper method
      The module adds a RepeaterPageArray::getDepthStructure method that returns a multi-dimensional array where the key is the page ID and the value is an array of nested "child" items, or null if there are no nested children.
      Example

      The module doesn't make any assumptions about how you might want to use the depth structure array, but here is a way you might use it to output a nested unordered list.
      // Output a nested unordered list from a depth structure array function outputNestedList($depth_structure, $repeater_items) { $out = "<ul>"; foreach($depth_structure as $page_id => $nested_children) { $out .= "<li>" . $repeater_items->get("id=$page_id")->title; // Go recursive if there are nested children if(is_array($nested_children)) $out .= outputNestedList($nested_children, $repeater_items); $out .= "</li>"; } $out .= "</ul>"; return $out; } $repeater_items = $page->my_repeater; $depth_structure = $repeater_items->getDepthStructure(); echo outputNestedList($depth_structure, $repeater_items);
       
      https://github.com/Toutouwai/RepeaterDepthHelper
      https://modules.processwire.com/modules/repeater-depth-helper/
    • By horst
      Hi, on a site I want to disable access to original images and only allow to access thumbnails and watermarked image variations.
      EDIT:
      A good solution for protecting original images can be found a bit down in this thread:
       
      Old content of this initial post:
       
    • By Guy Incognito
      I seem to be running into a repeated fatal error in a fresh PW install version 3.0.148. I can't quite put my finger on the pattern but it seems to be around deleting image fields or removing images from certain image fields.
      This is the trace from the log generated by trying to save a page and delete an image from an image field:
      Fatal Error: Uncaught Error: Cannot access protected property Pageimage::$original in /wire/core/PageimageVariations.php:256 Stack trace: 1. /wire/core/Pageimage.php(1327): PageimageVariations->getInfo() 2. /wire/core/Wire.php(386): Pageimage->___isVariation() 3. /wire/core/WireHooks.php(823): Wire->_callMethod() 4. /wire/core/Wire.php(450): WireHooks->runHooks() 5. /wire/core/Pageimage.php(1369): Wire->__call() 6. /wire/core/Pageimage.php(399): Pageimage->getOriginal() 7. /wire/core/WireData.php(333): Pageimage->get() 8. /wire/core/PageimageVariations.php(256): Pro Line 256 of /wire/core/PageimageVariations.php Earlier in the day I was experimenting with custom fields for images for the first time and kept running into this error, thinking it was me using this feature wrong and not having time to read up I deleted the custom image fields template and went about my business. So now I don't know if I triggered an issue or whether it was never related to the custom image fields in the first place?
      Any ideas?
    • By ICF Church
      Hi 👋
      Anyone else having this problem?
      Requirements:
      - Repeater (matrix & normal) with mutlilanguage fields (text, textarea…) 
      - Backend language set to something other than default (ie. German) 
      Reproduce:
      - Add a new repeater Item (ajax, I found no way to possible to disable it with matrix)

      (Notice how the default language tab is active instead of the backend language…)
      - Write something into the (default language) field
      - Try to save, if field is required, this will not work. If not required, then when reloading, the content will be inside the backend language field, instead of the default language field who was (presumably) active
      Analysis:
      When  loading  a new repeater element with ajax, the default langue tab is active, but the backend language inputfield is visible (with no visual indication). When writing into the field, it will populate the backend language. When manually clicking on the default language tab (which is already active), the field will switch to the actual default language field (which is [now] empty) (that can now be populated…)
      Also Notice, the labels of the elements to be added are in default language as well instead of the translated label (images instead of Bilder)…
      ProcessWire 3.0.148, Profields 0.0.5…
      Is it my system configuration, or does anyone else have the same issue? This is a screen recording of the problem:
      Issue: https://github.com/processwire/processwire-issues/issues/1179

      Screen Recording 2020-02-25 at 14.18.31.mov
    • By neonwired
      I have a front end form for creating new pages, repeater and repeater matrix field don't seem to save any data. I was considering handling the data manually but can't seem to get anything useful from the post data, are there any methods i can use?
×
×
  • Create New...