Jump to content

MediaManager: cannot access ->media properties in hook


froot
 Share

Recommended Posts

 
I'm trying to create a hook that would populate my SEO-Markup fields automatically with some field values of the page upon saving.
For some reason I cannot access the MediaManager image field's ->media property and its properties, it returns null.
I do see this in the tracy console though…
 
image.png.ba8fac05a7b97e936f45450ebe820baf.png
 
 
 
//ready.php

$wire->addHookBefore('Pages::saveReady', function($event) use($sanitizer) {

    $page = $event->arguments[0];

    if (!$page->id) return;
    if ($page->isChanged('seo_title') || $page->isChanged('seo_image') || $page->isChanged('seo_description')) return; 


    // SEO image :D

    if ($page->seo_image == '') {
        $filled = false;
	    bd($page->mema_images); // see screenshot
        bd($page->mema_images->count); // 3
        if ($page->mema_images->count > 0) {
            $imageURL = $page->mema_images->first->media->url;
            bd($imageURL); // null :(
            $filled = true;
        }

        if ($filled == true) {
            $imageURL = strval($imageURL);
            $page->seo_image = $imageURL;
            $page->message('SEO image was filled because it was empty :D');
            $filled = false;
        }
    }


    // SEO description :D - this works fine

    if ($page->seo_description == '') {
        $filled = false;
        $description = '';

        if ($page->preview != '') {
            $filled = true;
            $description = $page->preview;
        } 

        if ($filled == true) {
            $description = SEOcleanUp($description, $sanitizer);
            $page->seo_description = $description;
            $page->message('SEO description was filled because it was empty :D');
            $filled = false;
        }
    } 

    $event->arguments(0, $page);
        
});

 

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

×
×
  • Create New...