1

I'm writing a module that loops over lines in a CSV file to save them as entries. For every entry I save with elements->saveElement($element);, SEOMatic will queue a job that regenerates the sitemap for this type of entries. I'm importing more than 1000 lines this way, so there'd be more than 1000 jobs in the queue, all doing the same thing. Is there a way to disable this hook so it wouldn't get triggered for every entry?

Here's a part of the code right now:

while (($csv = fgetcsv($handle)) !== false) {
    $entry = Entry::find()->siteId($siteId)->section('products')->where(['content.field_productId' => $csv[0]])->one();

    if (!$entry) {
        $entry = new Entry();
        $entry->sectionId = Craft::$app->sections->getSectionByHandle('products')->id;
        $entry->typeId = 1;
        $entry->siteId = $siteId;
    }


    ...
    ...
    ...


    try {
        Craft::$app->elements->saveElement($entry, true, true, false);
    } catch (Throwable $e) {
        throw new RuntimeException($e);
    }
}

try {
    Craft::$app->elements->resaveElements(Entry::find()->siteId($siteId)->section('products'), false, true, true);
} catch (Throwable $e) {
    throw new RuntimeException($e);
}
3
  • Not sure if that will do it but there seems to be a config setting to prevent that - github.com/nystudio107/craft-seomatic/blob/v3/src/… – Oli Mar 5 '20 at 15:17
  • @Oli This doesn't seem to help, I'm setting it to false before saving, but it still adds a job for every line. – Joorren Mar 5 '20 at 15:43
  • @Oli at the end it did actually work, but I was using it a bit wrong. What I'm doing right now is setting the regenerateSitemapsAutomatically to false in the beginning of the job. At the end of the job I set it back to true and then resave the first element inside the same section. – Joorren Mar 6 '20 at 8:07
2

By setting the regenerateSitemapsAutomatically to false, SEOMatic won't regenerate a sitemap for every entry saved. At the end of the job, I set it back to true and save the first element inside of the same section.

Changes to the original code:

Seomatic::$settings->regenerateSitemapsAutomatically=false; // Disable it

while (($csv = fgetcsv($handle)) !== false) {
    $entry = Entry::find()->siteId($siteId)->section('products')->where(['content.field_productId' => $csv[0]])->one();

    if (!$entry) {
        $entry = new Entry();
        $entry->sectionId = Craft::$app->sections->getSectionByHandle('products')->id;
        $entry->typeId = 1;
        $entry->siteId = $siteId;
    }


    ...
    ...
    ...


    try {
        Craft::$app->elements->saveElement($entry);
    } catch (Throwable $e) {
        throw new RuntimeException($e);
    }
}

try {
    Seomatic::$settings->regenerateSitemapsAutomatically=true; // Enable it again
    $firstElement = Entry::find()->siteId($siteId)->section('products')->one();
    Craft::$app->elements->saveElement($firstEntry); // Save first entry again
} catch (Throwable $e) {
    throw new RuntimeException($e);
}

Thanks to Oli for the suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.