Jump to content

Use size function within functions.php


alexm
 Share

Recommended Posts

Hi Guys,
 
Just wondering whether I'm doing something wrong or you just can't use size function for images inside of my global functions.php.
 
I have an about page and on this page am going to render directors of the company. 
So I have made a function so as not to keep rewriting the same code of course.
 
If I use:

$alex = $users->get("name=alex");
$image = $alex->images->first;
    
echo "<img src=\"{$image->size(369, 369)->url}\" />";

 
from within the about template there is no problem at all.

Now if I do the equivalent from within the functions file I get:
Error: Call to a member function size() on a non-object (line 249 of /Path/To/My/Website/site/templates/includes/functions.php)

Any thoughts please?

Thanks

Alex

Link to comment
Share on other sites

You can definitely use size() in functions. If you take a closer look at the error you're getting, it's not saying that you can't use size(), but that you can't use size() "on a non-object", meaning in essence that $image (or whatever you're calling the size() method on) isn't defined or isn't valid object (it should be an instance of PageImage in your case).

Without seeing your code this is just a guess, but it would seem that this is a scope issue, i.e. you're fetching the $image outside function and then trying to use it inside or something like that. Since $users->get() shouldn't work within function context at all (you should use wire('users')->get.. there) I'm wondering what exactly you mean by "equivalent" solution here :)

  • Like 2
Link to comment
Share on other sites

Hi Teppo, 

I'm just being lazy when I say equivalent. 

Here is what it looks like so far

function renderDirectorProfile($person) {
    if ($person) {
        
        $director = wire('users')->get("name=$person");
        $directorsImage = $director->images->first();
        
        $o .= "\n<div class='member span4'>" . 
              "\n\t<img src='{$directorsImage->size(369, 369)->url}' alt='$director->fullname' />" . 
              "\n\t<h2>$director->fullname</h2>" . 
              "\n\t<span='position'>$director->position</span>" . 
              "\n\t$director->body" . 
              "\n</div>";
    } else {
        $o = "<p>Couldn't find director.</p>";
    }
    
    return $o;
}

Thanks

Link to comment
Share on other sites

@WillyC is right; the problem is most likely that you're calling renderDirectorProfile() ..

a) for user that doesn't exist or

b) for user that exists but doesn't have any images available.

Adding proper checks for $user->id and count($user->images) / $user->images->count() before trying to output content is strongly recommended:

function renderDirectorProfile($person) {
    if ($person) {
        $director = wire('users')->get("name=$person");
    }
    if ($person && $director->id) {
        $o = "\n<div class='member span4'>";
        if (count($director->images)) {
            $directorsImage = $director->images->first();
            $o .= "\n\t<img src='{$directorsImage->size(369, 369)->url}' alt='$director->fullname' />";
        }
        $o .= "\n\t<h2>$director->fullname</h2>" . 
              "\n\t<span='position'>$director->position</span>" . 
              "\n\t$director->body" . 
              "\n</div>";
    } else {
        $o = "<p>Couldn't find director.</p>";
    }

    return $o;
}
  • Like 4
Link to comment
Share on other sites

So you were both right that one of the directors didn't have an image assigned which was breaking it.

So I have amended the code but now the director with no image renders now with no image of course and then it fails and throws an error for the last two.

If I change the usernames to one's which don't exist nothing displays at all and nor does the <p>Couldn't find director.</p> message.

I'm confused

Link to comment
Share on other sites

Ok! typo.

They are rendering with images. 

However the <p>Couldn't find director.</p> doesn't show when setting a user that doesn't exist. I'll have to take a look into that.

It's working though! 

Thanks teppo and WillyC
 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...