1

I am diving into Craft plugin development and there is one thing that I want to achieve, but I didn't find any documentation.

I managed to save specific new entries into a channel using saveElement(), but I have no clue about how I can create a new channel.

The idea is to build a plugin that uses a specific channel (for example a "ToDo list items") and I want that channel to be created once the plugin is installed.

Just as a matter of clarifying, for people who know about WordPress. The idea is to "register a Custom Post Type".

I am not providing any "what did you try?" code because I don't even know if this is possible. And of course I am not looking for "the code", but rather for "the concept" :)

UPDATE

Thanks to MortizLost's great answer, I managed to achieve what I want. Leaving it here in case it helps someone sometime:

$section_exists = Craft::$app->sections->getSectionByHandle('news');

if($section_exists){
    echo 'Section news already exists';
}else{

    $section = new Section([
        'name' => 'News',
        'handle' => 'news',
        'type' => Section::TYPE_CHANNEL,
        'siteSettings' => [
            new Section_SiteSettings([
                'siteId' => Craft::$app->sites->getPrimarySite()->id,
                'enabledByDefault' => true,
                'hasUrls' => true,
                'uriFormat' => 'news/{slug}',
                'template' => 'news/_entry',
            ]),
        ]
    ]);

    $section_was_created = Craft::$app->sections->saveSection($section);

    if($section_was_created){
        echo 'Section news has been created';
    }else{
        echo 'An error occurred while trying to create the news section';
    }

}
3

When trying to figure out how to do something the core does programmatically, a good place to start is the core itself. In particular, you want to look at Craft's controllers which handle all interactions with the UI and Craft's services which provide a lot of methods for common tasks.

In this case, you're looking for craft\controllers\SectionsController::actionSaveSection and craft\services\Sections::saveSection (note for posterity: those links go directly to the 3.7 branch, make sure to look up the corresponding methods in the current version of Craft).

Looking through the controller method, you can figure out how to create a new section and save it:

use craft\models\Section;

$sectionsService = Craft::$app->getSections();

$section = new Section();
$section->name = 'My new section';
$section->handle = 'my_new_section';
$section->type = Section::TYPE_CHANNEL;
$section->enableVersioning = true;
$section->propagationMethod = Section::PROPAGATION_METHOD_ALL;
$section->previewTargets = [];

// set other properties and options …

$success = $sectionsService->saveSection($section);
if (!$success) {
    // saving the section failed, display or handle errors
}

Look through the source code linked above to find all the properties that are available or required.


As a sidenote, if you create a new section in a plugin – for example, in the install handler – you'll want to make sure your plugin plays nicely with the project config. As far as I can tell, Craft handles this automatically, but make sure to properly test if your new section and all its properties end up in the project config so your plugin will still work when propagating the config to different installations / environments.

2
  • 1
    Two million thanks. Still a bit lost in craft concepts. Really appreciate your help. Sep 23 at 10:03
  • 1
    @ÁlvaroFranz You're welcome! Yeah I know the feeling, Craft's documentation is a bit lacking when it comes to extending the core. Digging through the core's controllers and services is a gold-mine in information that isn't covered in the documentation ^^
    – MoritzLost
    Sep 23 at 10:45

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.