matt-j-m / grav-plugin-aura

The Aura Plugin for Grav CMS adds meta tags and structured data to your pages for visually appealing and informative search results and social media shared links.
MIT License
26 stars 7 forks source link

Error with new Grav 1.7.3 #15

Closed X-Ryl669 closed 3 years ago

X-Ryl669 commented 3 years ago

I have a multilanguage blog. When I add a item page to a blog, and click save, it breaks with this information, preventing first page saving:

/public_html/user/plugins/aura/aura.php:125

                $aura->generateTwitterMeta();
            }

            // LinkedIn
            if ($this->grav['config']->get('plugins.aura.output-linkedin')) {
                $aura->generateLinkedInMeta();
            }

            // Generate Aura metadata
            $metadata = [];
            foreach ($aura->webpage->metadata as $tag) {
                if (array_key_exists('property', $tag)) {
                    $metadata[$tag['property']] = $tag['content'];
                } else if (array_key_exists('name', $tag)) {
                    $metadata[$tag['name']] = $tag['content'];
                }
            }

            $original = $page->getOriginal();
=> HERE
            if (!isset($original->header()->aura) && isset($page->header()->metadata) && is_array($page->header()->metadata)) {
                // Page has not been saved since installation of Aura and includes some custom metadata
                foreach ($page->header()->metadata as $key => $val) {
                    if (!array_key_exists($key, $metadata)) {
                        // A new value has not been supplied via Aura, salvage existing metadata
                        $metadata[$key] = $val;
                        $page->header()->aura['metadata'] = array($key => $val);
                    }
                }
            }

            $page->header()->metadata = array_merge($metadata, isset($page->header()->aura['metadata']) ? $page->header()->aura['metadata'] : []);

        }

        /**
         * Insert meta tags and structured data to head of each page
         *
         * @param Event $e
         */
        public function onPageInitialized()

Arguments

    "Call to a member function header() on null"

$original is null in that case, since it's the first save of the page. Changing the test to read:

            if ($original !== null && !isset($original->header()->aura) && isset($page->header()->metadata) && is_array($page->header()->metadata)) {

works.

X-Ryl669 commented 3 years ago

Well, it fails then on second saving with Save Failed: Indirect modification of overloaded property Grav\Common\Page\Header::$aura has no effect

X-Ryl669 commented 3 years ago

I don't understand the code here:

$page->header()->aura['metadata'] = array($key => $val);

It's in a loop, so it'll be overwritten by the last value anyway. It's causing the exception above since you're not allowed to update a member like this when the member is a reference. In addition, there is no other aura['metadata'] use anywhere so the later array_merge does nothing. In the end, I've removed this line and it works.

mkocus commented 3 years ago

Got exactly same issue.

X-Ryl669 commented 3 years ago

Here's my code that works (on end of onAdminSave:

       // Generate Aura metadata
        $metadata = [];
        foreach ($aura->webpage->metadata as $tag) {
            if (array_key_exists('property', $tag)) {
                $metadata[$tag['property']] = $tag['content'];
            } else if (array_key_exists('name', $tag)) {
                $metadata[$tag['name']] = $tag['content'];
            }
        }

        $original = $page->getOriginal();
        $aura_metadata = [];
        if ($original !== null && !isset($original->header()->aura) && isset($page->header()->metadata) && is_array($page->header()->metadata)) {
            // Page has not been saved since installation of Aura and includes some custom metadata
            foreach ($page->header()->metadata as $key => $val) {
                if (!array_key_exists($key, $metadata)) {
                    // A new value has not been supplied via Aura, salvage existing metadata
                    $metadata[$key] = $val;
                }
            }
        }

        $page->header()->metadata = $metadata; 
    }
giucas-falchetto commented 3 years ago

I can confirm the error is caused by calling $original->header() when $original is null

$original = $page->getOriginal();
        if (!is_null($original)) {  // added this
            if (!isset($original->header()->aura) && isset($page->header()->metadata) && is_array($page->header()->metadata)) {
                // Page has not been saved since installation of Aura and includes some custom metadata
                foreach ($page->header()->metadata as $key => $val) {
                    if (!array_key_exists($key, $metadata)) {
                        // A new value has not been supplied via Aura, salvage existing metadata
                        $metadata[$key] = $val;
                        $page->header()->aura['metadata'] = array($key => $val);
                    }
                }
            }
        } // added this
matt-j-m commented 3 years ago

Thanks for logging the issue and for your suggestions @X-Ryl669. I've updated and created a new release which you can manually install now or update via gpm shortly. Cheers.

01Kuzma commented 3 years ago

Now the plug-in adds Home | in og:title before the title itself. Does someone has observed the same?