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{% UIForm Standard {sId:'myform'} %}
 7    Select Month:
 8    {% UISelect ForSelect {sName:'month'} %}
 9        {% for index,sMonth in aQuarter %}
10            {% UISelectOption ForSelectOption {sValue: index, sLabel: sMonth, bSelected:false} %}
11        {% endfor %}
12    {% EndUISelect %}
13    {% UIInput ForHidden {sName:'operation', sValue:'SelectMonth'} %}
14    {% UIContentBlock Standard {DataAttributes: {role: 'actions'}} %}
15        {% UIButton ForPrimaryAction {sLabel:'Ok', bIsSubmit:true} %}
16    {% EndUIContentBlock %}
17{% EndUIForm %}

The output of this template is:

../../_images/form.png

Now add the operation in the controller:

src/Controller/MyModuleController.php
 1<?php
 2
 3// ...
 4
 5class MyModuleController extends Controller
 6{
 7    // ...
 8
 9    public function OperationSelectMonth()
10    {
11        $aMonths = ['January', 'February', 'March'];
12        $iMonth = utils::ReadParam('month', 0);
13        $aParams['sSelectedMonth'] = $aMonths[$iMonth];
14        $this->DisplayPage($aParams);
15    }
16}

Disclaimer: The code is for tutorial only, don’t use it in production as no check is done on the entries

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

templates/SelectMonth.html.twig
1{% UITitle ForPage {sTitle:'Selected month'} %}{% EndUITitle %}
2
3{% UIContentBlock Standard {DataAttributes: {role: 'Information'}} %}
4    The selected month is {{ sSelectedMonth }}
5{% EndUIContentBlock %}

The output of this operation is:

../../_images/SelectMonth.png