<p>By setting the <code>regenerateSitemapsAutomatically</code> 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.</p>

<p>Changes to the original code: </p>

<pre class="lang-php prettyprint-override"><code>Seomatic::$settings-&gt;regenerateSitemapsAutomatically=false; // Disable it

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

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


    ...
    ...
    ...


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

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

<p>Thanks to Oli for the suggestion.</p>

CC BY-SA 4.0
2020-03-06T08:13:08.277Z
2020-03-06T08:21:06.227Z
2020-03-06T08:21:06.227Z
Joorren
<p>I'm writing a module that loops over lines in a CSV file to save them as entries. For every entry I save with <code>elements-&gt;saveElement($element);</code>, 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?</p>

<p>Here's a part of the code right now:</p>

<pre class="lang-php prettyprint-override"><code>while (($csv = fgetcsv($handle)) !== false) {
    $entry = Entry::find()-&gt;siteId($siteId)-&gt;section('products')-&gt;where(['content.field_productId' =&gt; $csv[0]])-&gt;one();

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


    ...
    ...
    ...


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

try {
    Craft::$app-&gt;elements-&gt;resaveElements(Entry::find()-&gt;siteId($siteId)-&gt;section('products'), false, true, true);
} catch (Throwable $e) {
    throw new RuntimeException($e);
}
</code></pre>

CC BY-SA 4.0
2020-03-05T15:17:24.403Z
Not sure if that will do it but there seems to be a config setting to prevent that - https://github.com/nystudio107/craft-seomatic/blob/v3/src/config.php#L45
Oli
CC BY-SA 4.0
2020-03-05T15:43:18.557Z
@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
CC BY-SA 4.0
2020-03-06T08:07:01.593Z
@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
<plugin-development><entries><modules><plugin-seomatic><extends>
Not triggering SEOMatic on save element
# Not triggering SEOMatic on save element

(Asked by: username_0 on 2020-03-05)

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);
}

**Tags:** plugin-development, entries, modules, plugin-seomatic, extends

> **Comments:**
> **username_1**: Not sure if that will do it but there seems to be a config setting to prevent that - https://github.com/nystudio107/craft-seomatic/blob/v3/src/config.php#L45  
> **username_0**: @username_1 This doesn't seem to help, I'm setting it to false before saving, but it still adds a job for every line.  
> **username_0**: @username_1 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.  

---

## 1 answers

### Answer by username_0 on 2020-03-06. Score: 2 | Accepted answer

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 username_1 for the suggestion.


---

