neosin Posted March 19, 2018 Share Posted March 19, 2018 I have a page with an image field that is used by a template with the same field name. If a user creates a page using the template and does not fill in the image field, then it should use the image field from the page. I have this working. The problem I am having is getting the path to the image if the user leaves it blank and it uses the "default" assigned image. How do I get the path of the default image? I can get it's filename but can't figure out how to get the path for it since it comes from a different page. thank you Link to comment Share on other sites More sharing options...
3fingers Posted March 19, 2018 Share Posted March 19, 2018 // pseudo code, change field name with yours $image_to_be_filled = $page->your_image_field; if (count($image_to_be_filled) == 0) { //here I'm searching by template, but it's up to you to build the selector you need/prefer $default_image = $page->get('template=the_template_holding_your_default_image')->field_of_your_default_image; //from here do whatever you want with the $default_image variable ... } 4 Link to comment Share on other sites More sharing options...
neosin Posted March 19, 2018 Author Share Posted March 19, 2018 @3fingers thank you sir 1 Link to comment Share on other sites More sharing options...
Robin S Posted March 19, 2018 Share Posted March 19, 2018 There is a feature for this built into image fields (Details tab): 2 Link to comment Share on other sites More sharing options...
neosin Posted March 19, 2018 Author Share Posted March 19, 2018 45 minutes ago, Robin S said: There is a feature for this built into image fields (Details tab): yes I am aware of this however in my template code I could not find the way to get the image path from the default page field. From another thread I found the path is $page->field_name->httpUrl which works as expected since it inherits the fields. What I was missing is the httpURL parameter thank you Link to comment Share on other sites More sharing options...
Robin S Posted March 19, 2018 Share Posted March 19, 2018 15 minutes ago, neosin said: yes I am aware of this however in my template code I could not find the way to get the image path from the default page field. Not sure what you mean here. When you use the feature I mentioned to select a page that will hold the default value for the image field, the image field will have the value of the default field when it is empty on a particular page. The value will be a Pageimage object or a Pageimages object depending on the field settings (and assuming you have one or more images in the field on the page used for the default value). You shouldn't need to do anything different to get the object or any of its properties (url, etc) than you would do if the field was populated on the page. In other words, you can do something like... $page->image->size(400,300)->url ...and it will work the same regardless if the image is coming from an image that is actually in the field on that page, or the default value when the field is empty. 2 Link to comment Share on other sites More sharing options...
szabesz Posted March 20, 2018 Share Posted March 20, 2018 @neosin Hi, maybe this discussion can give you some ideas how to handle "default images": Link to comment Share on other sites More sharing options...
Alpine418 Posted February 14, 2022 Share Posted February 14, 2022 I cannot see the default value settings for image fields on the latest PW versions. Did I miss something? Link to comment Share on other sites More sharing options...
Robin S Posted February 14, 2022 Share Posted February 14, 2022 5 hours ago, rjgamer said: I cannot see the default value settings for image fields on the latest PW versions. Did I miss something? It's on the "Advanced" tab: 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now