Jump to content

[solved] when default field value is an image file


neosin
 Share

Recommended Posts

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

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

 

  • Like 4
Link to comment
Share on other sites

45 minutes ago, Robin S said:

There is a feature for this built into image fields (Details tab):

2018-03-20_101047.png.073dd302842ba47d1c083781c45671c5.png

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

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.

  • Like 2
Link to comment
Share on other sites

  • 3 years later...

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