<p>You can use the <code>EVENT_AFTER_SAVE_SITE</code> event to create/update the new section settings</p>

<pre class="lang-PHP prettyprint-override"><code>Event::on(
    Sites::class,
    Sites::EVENT_AFTER_SAVE_SITE,
    function (SiteEvent $event) {
        if ($event-&gt;isNew) {

            ProjectConfigHelper::ensureAllSitesProcessed();
            $siteId = $event-&gt;site-&gt;id;

            // array of sections you want to propagate
            $sectionsToActivate = [
                'test',
            ];

            foreach ($sectionsToActivate as $sectionHandle){

                $section = Craft::$app-&gt;getSections()-&gt;getSectionByHandle($sectionHandle);
                if($section !== null){
                    /** @var \craft\models\Section_SiteSettings[] $siteSettings */
                    $siteSettings = $section-&gt;getSiteSettings();

                    // the site you want to clone from
                    $siteSettings[$siteId] = clone $siteSettings[1]; 
                    $siteSettings[$siteId]-&gt;id = null;
                    $siteSettings[$siteId]-&gt;siteId = $siteId;

                    // eventually you want to change some other settings ¯\_(ツ)_/¯
                    //$siteSettings[$siteId]-&gt;template = '';
                    //$siteSettings[$siteId]-&gt;uriFormat = '';
                    //$siteSettings[$siteId]-&gt;hasUrls = '';

                    $section-&gt;setSiteSettings($siteSettings);
                   Craft::$app-&gt;getSections()-&gt;saveSection($section);
                }
            }
        }
    }
);
</code></pre>

<p>Explanation: As soon as you create a new site, you'll loop all sections you want to activate and grab their "main" site settings, copy them and set the <code>id</code> to null in order to create a new record</p>

CC BY-SA 4.0
2019-06-13T19:26:27.110Z
Sorry for late reply, this worked! Thanks a lot.
Laurence Cope
CC BY-SA 4.0
2021-07-29T00:18:32.300Z
I need to implement this solution too. Any chance you can provide more info on how you got this working? Presuming you created a custom module. I'm not very experienced with that part of Craft yet.
Dr_Codswallop
CC BY-SA 4.0
2021-08-02T09:45:27.540Z
@Dr_Codswallop yes you need indeed a custom module for this, but to explain how to do that exactly would be too much for a comment. You could read about the basics here https://www.yiiframework.com/doc/guide/2.0/en/structure-modules
Robin Schambach
CC BY-SA 4.0
2019-03-20T11:04:54.643Z
2019-03-20T11:04:54.643Z
Robin Schambach
<p>My customer will have lots of sections and lots of entries in each, for each site. Over time they will add new language sites. </p>

<p>When a new site is added, there are no sections or entries for it. So we have to then edit each section to set the URL format and template, even though they will be the same for each site. </p>

<p>I would like to make it as easy and quick for the customer to add new sites. </p>

<p>So is there a way I can automatically set these values, enable the site, and propagate entries, for every section (or even certain sections), every time they create a new Site? </p>

<p>Thanks</p>

<p><a href="https://i.sstatic.net/c4hs7.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/c4hs7.png" alt="enter image description here"></a> </p>

CC BY-SA 4.0
2019-03-19T22:16:56.150Z
Yes it is but only with PHP. Do you search for a solution without custom code?
Robin Schambach
CC BY-SA 4.0
2019-03-20T09:57:20.413Z
Searching for solution with or without code.
Laurence Cope
<localization><sites>
Is there a way to auto-create a sections Site Settings and propagate when a new site is created?
# Is there a way to auto-create a sections Site Settings and propagate when a new site is created?

(Asked by: username_0 on 2019-03-19)

My customer will have lots of sections and lots of entries in each, for each site. Over time they will add new language sites.

When a new site is added, there are no sections or entries for it. So we have to then edit each section to set the URL format and template, even though they will be the same for each site.

I would like to make it as easy and quick for the customer to add new sites.

So is there a way I can automatically set these values, enable the site, and propagate entries, for every section (or even certain sections), every time they create a new Site?

Thanks

[![enter image description here](https://i.sstatic.net/c4hs7.png)](https://i.sstatic.net/c4hs7.png)

**Tags:** localization, sites
> **Comments:**
> **username_1**: Yes it is but only with PHP. Do you search for a solution without custom code?  
> **username_0**: Searching for solution with or without code.  

---

## 1 answers

### Answer by username_1 on 2019-03-20. Score: 3 | Accepted answer

You can use the EVENT_AFTER_SAVE_SITE event to create/update the new section settings
Event::on(
    Sites::class,
    Sites::EVENT_AFTER_SAVE_SITE,
    function (SiteEvent $event) {
        if ($event->isNew) {

            ProjectConfigHelper::ensureAllSitesProcessed();
            $siteId = $event->site->id;

            // array of sections you want to propagate
            $sectionsToActivate = [
                'test',
            ];

            foreach ($sectionsToActivate as $sectionHandle){

                $section = Craft::$app->getSections()->getSectionByHandle($sectionHandle);
                if($section !== null){
                    /** @var \craft\models\Section_SiteSettings[] $siteSettings */
                    $siteSettings = $section->getSiteSettings();

                    // the site you want to clone from
                    $siteSettings[$siteId] = clone $siteSettings[1]; 
                    $siteSettings[$siteId]->id = null;
                    $siteSettings[$siteId]->siteId = $siteId;

                    // eventually you want to change some other settings ¯\_(ツ)_/¯
                    //$siteSettings[$siteId]->template = '';
                    //$siteSettings[$siteId]->uriFormat = '';
                    //$siteSettings[$siteId]->hasUrls = '';

                    $section->setSiteSettings($siteSettings);
                   Craft::$app->getSections()->saveSection($section);
                }
            }
        }
    }
);

Explanation: As soon as you create a new site, you'll loop all sections you want to activate and grab their "main" site settings, copy them and set the id to null in order to create a new record

> **Comments:**
> **username_0**: Sorry for late reply, this worked! Thanks a lot.  
> **username_2**: I need to implement this solution too. Any chance you can provide more info on how you got this working? Presuming you created a custom module. I'm not very experienced with that part of Craft yet.  
> **username_1**: @Dr_Codswallop yes you need indeed a custom module for this, but to explain how to do that exactly would be too much for a comment. You could read about the basics here https://www.yiiframework.com/doc/guide/2.0/en/structure-modules  

---

