This section covers module development and provides a step-by-step example to help you get started.

Let's create a simple module as an example with basic functionality. Although it is rudimentary, it will serve to explain the module development process for Scriptor CMS.

The most crucial element is your module class, which extends Scriptor's core module class:

<?php 
namespace Scriptor\Modules\TestModule;

use Scriptor\Core\Module;

class TestModule extends Module
{
    // ...
}

Save the class you are creating in a file with the same name as the class in the /site/modules/TestModule/ directory. For instance, a module with the TestModule class (as shown above) should be in a file named TestModule.php.

Now, we can utilize all the methods and functions inherited from the parent (Module) class within our TestModule.

First, our module needs a moduleInfo() method that allows us to install it using the Scriptor Module Manager:

<?php 
namespace Scriptor\Modules\TestModule;

use Scriptor\Core\Module;

class TestModule extends Module
{
    public static function moduleInfo() : array
    {
        return [
            'name' => 'TestModule',
            'description' => 'Scriptor\'s test module.',
            'version' => '1.1',
            'author' => 'Your Name',
            'author_website' => 'https://your-website.com'
        ];
    }
}

Currently, our module does not perform much. Let's add a sayHello() method that returns the string Hello $user->name, where $user->name represents the current user's name if the user is logged in, and if not, it will display "Guest".

<?php 
namespace Scriptor\Modules\TestModule;

use Scriptor\Core\Module;
use Scriptor\Core\Scriptor;
use Scriptor\Core\User;

class TestModule extends Module
{
    public static function moduleInfo() : array
    {
       return [
            'name' => 'TestModule',
            'description' => 'Scriptor\'s test module.',
            'version' => '1.1',
            'author' => 'Your Name',
            'author_website' => 'https://your-website.com'
        ];
    }

    public function sayHello() 
    {
        $site = Scriptor::getSite();

        if (isset($_SESSION['userid'])) {
            $user = $site->users()->getUser((int) $_SESSION['userid']);
        } else {
            $user = new User();
            $user->name = 'Guest';
        }

        return 'Hello '.$this->sanitizer->text($user->name);
    }
}

Inform Scriptor about Your Module

Since you have just created the file, Scriptor cannot detect your module yet. Therefore, we need to install it using the Module Manager. To access the Module Manager, click on the "Modules" button in Editor (Admin) area, which will take you to the Module Manager section. If you have followed the instructions correctly while creating the module, you should now see the "TestModule" listed. Click the "Install" button to install the module. Once installed, you can start using it.

To see the changes made by the Module Manager during installation, navigate to the data/settings/ directory and open the file named custom.scriptor-config.php in an editor. In the "modules" section, you should find a new entry that looks similar to the following:

return [
    'modules' => [
        'TestModule' => [
            'name' => 'TestModule',
            'file_name' => 'TestModule.php',
            'path' => 'modules/TestModule/TestModule',
            'namespace' => 'Scriptor\\Modules\\TestModule',
            'class' => 'Scriptor\\Modules\\TestModule\\TestModule',
            'position' => 0,
            'menu' => '',
            'display_type' => [],
            'icon' => null,
            'active' => true,
            'auth' => true,
            'autoinit' => true,
            'version' => '1.1',
            'description' => 'Scriptor\'s test module.',
            'author' => 'Your Name',
            'author_website' => 'https://your-website.com',
            'author_email_address' => ''
        ]
    ]
];

For more information about the individual parameters, refer to the documentation.

Loading the Module

Once the TestModule is accessible to Scriptor, you can call upon this module from any of your site template files by loading it:

$testModule = $site->loadModule('TestModule');

To avoid displaying error messages, it is advisable to check whether the specified module has been loaded:

$testModule = $site->loadModule('TestModule');
// Check if the module is loaded
if ($testModule) {
    // ...
}

Using the Module

The module is now ready for use. You can call the sayHello() method as follows:

$testModule = $site->loadModule('TestModule');
if ($testModule) {
    echo '<h3>' . $testModule->sayHello() . '</h3>';
}

The output would be <h3>Hello Lucas</h3> or the username of the currently logged-in user.

This concludes our basic introduction to developing a simple module.