Jump to content

->url on image not giving full path, only folder with no filename


Recommended Posts

Posted

When I do $page->BackgroundImage->url it gives the path without the filename.  Is that normal?  I was thinking it gives both.

If I do  $page->BackgroundImage->url . $page->BackgroundImage I can get it to show both.

What am I doing wrong?

Thanks!

Posted

That is what I would have thought also, but it is set to "Single Item (Null if empty)" with 1 as the max images.

Looks like it is returning an array no matter what I do.

ss.jpg

Posted

I do further down the page.  This is the flow.

addHookBefore (save)

      $event->message("background-image: " . $page->BackgroundImage->url);
     //Gives  background-image: /site/assets/files/1312/

addHookAfter (save)

     setOutputFormatting(false);

(save a different page) 

Posted

var_dump($page->of()); doesnt seem to do anything.

If I take off the "->of()" it will print out the page info.

Posted

Well, that's weird - here it returns true or false.

Anyway, we are getting off track. Either turn on output formatting before $event->message, or simply add first() before url and you should be fine. Does that work or not?

  • Like 1
Posted

I was able to use output buffering and get it to: result: bool(false)

ob_start();
var_dump($page->of());
$result = ob_get_clean();
$event->message("result: " . $result);

 

 

Posted

I am in a module that is hooking before page save so maybe it would be best if I leave output format off?  I am wondering if processwire is expecting that?  

I dont mind treating it like an array as long as it is expected behavior and not a bug that will be fixed later!

Thanks for all your help!

Posted

It's not a bug - when saving, output formatting has to be off.

PS - you should get familiar with Tracy:

bd($page->of()) is a lot easier than messing around with ob_start and var_dump :)

  • Like 1
Posted (edited)
1 hour ago, joer80 said:

I am in a module that is hooking before page save so maybe it would be best if I leave output format off?

Actually, output formatting is always off in a module. If output formatting is off, Image fields will always return an array, no matter your settings. So...first(), last(), eq()....are your go to guys...

Edited by kongondo
  • Like 1
Posted
4 minutes ago, kongondo said:

Actually, output formatting is always off in a module.

Only if it's executed before or after template rendering, which is most of the time, but not always. That's why I go with this in places, where I cannot be sure about the state of output formatting:

$page->getUnformatted('image')->first()->url

 

  • Like 2
Posted
2 minutes ago, LostKobrakai said:

Only if it's executed before or after template rendering, which is most of the time, but not always.

Aah, good to know ^-^.

  • Like 1

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.
×
×
  • Create New...