Goldinteractive / craft3-sitecopy

With this plugin you can copy the content of an entry to another site.
MIT License
22 stars 6 forks source link

Illegal string offset 'siteId' #1

Closed riasvdv closed 6 years ago

riasvdv commented 6 years ago
 in /Users/rias/Code/resengo/vendor/goldinteractive/craft3-sitecopy/src/services/SiteCopy.php at line 96

Getting this error when trying to copy to another site. It seems the syncElementContent function is being called twice from examining the logs:

The first time $supportedSites contains this:

2018-11-08 10:24:05 [-][1][vtaqcgs5b7voa6iqvok5aqmel8][trace][site-copy] [
    [
        'siteId' => '1',
        'enabledByDefault' => '1',
    ],
    [
        'siteId' => '2',
        'enabledByDefault' => '0',
    ],
    [
        'siteId' => '3',
        'enabledByDefault' => '0',
    ],
    [
        'siteId' => '4',
        'enabledByDefault' => '1',
    ],
]

But the second time it's only this:

2018-11-08 10:24:06 [-][1][vtaqcgs5b7voa6iqvok5aqmel8][trace][site-copy] [
    '1',
    '2',
    '3',
    '4',
]

Which causes the illegal string offset

christianruhstaller commented 6 years ago

This is strange, because the $entry->getSupportedSites() should return either an empty array or an array with the structure:

 /**
     * @inheritdoc
     */
    public function getSupportedSites(): array
    {
        $section = $this->getSection();
        $sites = [];

        foreach ($section->getSiteSettings() as $siteSettings) {
            if ($section->propagateEntries || $siteSettings->siteId == $this->siteId) {
                $sites[] = [
                    'siteId' => $siteSettings->siteId,
                    'enabledByDefault' => $siteSettings->enabledByDefault
                ];
            }
        }

        return $sites;
    }

So even if the function would be called twice, it should not return the array you are experiencing. This works only for entries of course.

Maybe something else calls the event EVENT_BEFORE_SAVE_ELEMENT and triggers it and falls flat at the line.

Could you explain a little bit more about the setup? What type of section? Which fields do you use? For now I will add a check, if the element is an entry and if not do not even call the function.

christianruhstaller commented 6 years ago

I couldn't reproduce it (I think Matrix is the culprit). But I still added the check, it's cleaner than just hope everything works. :) https://github.com/Goldinteractive/craft3-sitecopy/commit/592d727390ef619f84980bd007e67e7263e08383

This should fix your issue. Just update the plugin.

riasvdv commented 6 years ago

Works! Seems like Matrix is indeed triggering the event. Thanks for the quick fix