4. Adding new operations

We currently have only one operation HelloWorld. Generally multiple operations are needed, let’s add a new one.

In the previous part 3. Passing variables to templates we have created a form, it will be used to call the operation SelectMonth.

Modify the form to add an hidden input to set the operation and add a submit button:

templates/HelloWorld.html.twig
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{% UIForm Standard {sId:'myform'} %}
    Select Month:
    {% UISelect ForSelect {sName:'month'} %}
        {% for index,sMonth in aQuarter %}
            {% UISelectOption ForSelectOption {sValue: index, sLabel: sMonth, bSelected:false} %}
        {% endfor %}
    {% EndUISelect %}
    {% UIInput ForHidden {sName:'operation', sValue:'SelectMonth'} %}
    {% UIContentBlock Standard {DataAttributes: {role: 'actions'}} %}
        {% UIButton ForPrimaryAction {sLabel:'Ok', bIsSubmit:true} %}
    {% EndUIContentBlock %}
{% EndUIForm %}

The output of this template is:

../../_images/form.png

Now add the operation in the controller:

src/Controller/MyModuleController.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php

// ...

class MyModuleController extends Controller
{
    // ...

    public function OperationSelectMonth()
    {
        $aMonths = ['January', 'February', 'March'];
        $iMonth = utils::ReadParam('month', 0);
        $aParams['sSelectedMonth'] = $aMonths[$iMonth];
        $this->DisplayPage($aParams);
    }
}

The corresponding template must be created templates/SelectMonth.html.twig

templates/SelectMonth.html.twig
1
2
3
4
5
{% UITitle ForPage {sTitle:'Selected month'} %}{% EndUITitle %}

{% UIContentBlock Standard {DataAttributes: {role: 'Information'}} %}
    The selected month is {{ sSelectedMonth }}
{% EndUIContentBlock %}

The output of this operation is:

../../_images/SelectMonth.png