Skip to content

Add a new Chamilo setting

Julio Montoya edited this page Aug 29, 2018 · 3 revisions

In Chamilo v2, it's more easy to add a new setting, you have to follow the next steps.

For this example, We want to convert a setting that appears originally in configuration.php in Chamilo 1.11.x

// Hide session link of course_block on index/userportal
$_configuration['remove_session_url'] = false ;

1. Locate the Schema

Because this is an option located in configuration.php, it means it's a global setting and it's related to a "Session", so we take a look to the current settings located in the folder:

src/CoreBundle/Settings

We find the class called "SessionSettingsSchema.php", in this Schema we add all the settings related to a Chamilo session.

2. Add the new setting

Because this is just a true/false (Yes/No) option we add the following:

public function buildSettings(SettingsBuilderInterface $builder)
{
    $builder
        ->setDefaults(
            [
                ...                    
                'remove_session_url' => 'false' // false is the default option
            ]
        )
    ;
}

public function buildForm(FormBuilderInterface $builder)
{
    $builder
       ->add('remove_session_url', YesNoType::class)
    ;
}

Because we are using the Symfony Form component, you can use different type form, for example: ChoiceType::class (select of options), TextType::class, etc. For the list of form types visit:

http://symfony.com/doc/current/reference/forms/types.html

In Chamilo we use a lot the "yes/no" option, so a new type form of "select/menu" was created for Chamilo called "YesNoType::class" (Chamilo\CoreBundle\Form\Type\YesNoType).

3. See the new added setting in the platform:

After adding those 2 lines you can find your new setting here:

public/admin/settings/session

And we will find the setting.

4. Use the new setting in the code

In the Chamilo legacy files (inside main) we can call the new setting using:

api_get_setting('session.remove_session_url')

In new Chamilo controllers you can use it:

public function indexAction(Request $request): Response
{
    $manager = $this->get('chamilo.settings.manager');
    $myNewSetting = $manager->getSetting('session.remove_session_url');
}

In Twig:

{{ chamilo_settings_get('session.remove_session_url') }}

Be careful with the value that it will return. The yes/no type returns a string so the possible values are the string: "true" or "false", so for our example we have to change the code to validate with a string instead for a bool.

See the change of this example:

https://github.com/chamilo/chamilo-lms/commit/46eb8c0ae7d634d1ba8ce9b0b6af98edb3439b32

Note: Settings translations are working, but they are not present yet in the code. We need to implement gettext.

Clone this wiki locally