View Issue Details

This issue affects 1 person(s).
 8
IDProjectCategoryView StatusLast Update
17494Bug reportsPluginspublic2021-08-05 11:19
Reporterollehar Assigned ToJHoeck  
PrioritynormalSeverityminor 
Status assignedResolutionopen 
Summary17494: New plugin event system to extend forms
Description

https://www.drupal.org/docs/drupal-apis/form-api/introduction-to-form-api

https://symfony.com/doc/current/form/create_form_type_extension.html

https://symfony.com/doc/current/form/events.html

We want to move the 2FA plugin to add a new tab to user account settings. For this we need a new event to push new form elements to the existing form, and also to save the new data at form submit.

Suggestion:

  • New event extendFormUserAccount (can be copied to other forms later, e.g. extendFormQuestionEdit, etc)
  • Input to form: List of form element objects

Form must be listened to at two places in the code: When rendering the form HTML, and when submitting. Only one event is needed if the element object contains information and logic on how to save and validate it.

JS validation will not be possible with this way. Extra JS needs to be registered for that to work.

abstract class PluginFormElement
{
private $name;
private $validationCallback;
private $saveCallback;
public function __construct(string $name, callback $validationCallback, callback $saveCallback);
public function toHtml();
public function validate($value);
public function save($value);
}

// type=input
class PluginFormElementInput
{
}

// type=textarea
class PluginFormElementTextarea
{
}

Etc for checkbox, radio, ...

TagsNo tags attached.
Bug heat8
Complete LimeSurvey version number (& build)
I will donate to the project if issue is resolved
Browser
Database type & version
Server OS (if known)
Webserver software & version (if known)
PHP Version
Git branch / Pull Request URLN/A

Relationships

related to 14290 closedDenisChenu Some form can not be submitted if csrfTokenName is updated 

Users monitoring this issue

There are no users monitoring this issue.

Activities

ollehar

ollehar

2021-08-04 13:44

reporter   ~65805

Ping @Mazi, @DenisChenu, @gabrieljenik for feedback, might be relevant for everyone who wants to write plugins.

gabrieljenik

gabrieljenik

2021-08-04 14:10

developer   ~65806

We want to move the 2FA plugin to add a new tab to user account settings.

Do we need to extend form? Can't we look for some other place or some screen redesign to avoid extending forms?

Not fan of extending forms as that means forms are shared.
Maybe I am missing something, but I think this is like an overkill for just integrating better plugins.
Can't we find a simpler solution approach?

ollehar

ollehar

2021-08-04 14:11

reporter   ~65807

You have any suggestions?

DenisChenu

DenisChenu

2021-08-04 14:36

developer   ~65809

See : https://gitlab.com/SondagesPro/mailing/smtpByUser : hacking with JS to add the form + hacking beforeControllerAction to save the form value …

Else :

Not fan of extending forms as that means forms are shared.

Yes : it can be great to have a "save only this plugin" data BUT : we need to show the form access in user data and show it like an user data

  1. Need to allow add "button" to access new plugin form (tab ? tab with lits of plugin)
  2. A clean form by plugin : admin/user/sa/personalsettings/plugin/myplugin ?

We need plugin from for

  • SurveyGroup
  • QuestionGroup
  • Question

And in my opinion : current quick plugin for survey is not the best : have a complete form (like @gabrieljenik write) seem better.
See https://gitlab.com/SondagesPro/TokenManagement/TokenUsersListAndManage

gabrieljenik

gabrieljenik

2021-08-04 14:45

developer   ~65812

You have any suggestions?
Try to create separate screen for account plugin settings.

Menu with Plugins registered for "account plugin settings".
Each menu item will load a screen with settings.
Can be rendered similar to the survey seetings.

Mazi

Mazi

2021-08-04 15:03

updater   ~65817

Thanks for bringing this to our attenton, Olle. Adding @bismark...

I second the idea of providing a better approach for adding your own form elements in a generic way. Thos JS hacks to add details to the interface can easily fail if minor HTML details get adjusted.

So +1 for having a better structured way to deal with it. Only must-have is to support this for LS3. We are using that version for >90% of our projects and want to use it for future projects as well. If there is no LS3 support, I think it is more or less useless for most partners.

ollehar

ollehar

2021-08-04 15:07

reporter   ~65819

See : https://gitlab.com/SondagesPro/mailing/smtpByUser : hacking with JS to add the form + hacking beforeControllerAction to save the form value …

Ooookey, so a non-hacky way is welcome, then. :)

ollehar

ollehar

2021-08-04 15:08

reporter   ~65820

Last edited: 2021-08-04 15:10

Only must-have is to support this for LS3

We don't have the man-power to support two versions. :( Already now we're really stretched thin.

ollehar

ollehar

2021-08-04 16:01

reporter   ~65836

Last edited: 2021-08-04 16:02

Drupal's form API: https://www.drupal.org/docs/drupal-apis/form-api/introduction-to-form-api

Not easily applicable for us, since we don't represent forms as objects internally.

ollehar

ollehar

2021-08-04 16:27

reporter   ~65837

Nice example from Symfony: https://symfony.com/doc/current/components/form.html#creating-a-simple-form

$form = $formFactory->createBuilder()
    ->add('task', TextType::class)
    ->add('dueDate', DateType::class)
    ->getForm();
DenisChenu

DenisChenu

2021-08-04 16:49

developer   ~65838

Ooookey, so a non-hacky way is welcome, then. :)

Yes : 100000% for a better solutiin. But my client don't wait years before a working solution on a stable version.

Else

Menu with Plugins registered for "account plugin settings".
A tab with each buttons ?

Each menu item will load a screen with settings.

Like ?r=admin/pluginhelper&sa=sidebody&plugin=myPlugin&method=actionSettings&surveyId=476427 but ?r=admin/pluginhelper&sa=sidebody&plugin=myPlugin&method=actionSettings&userid=2 ?

Can be rendered similar to the survey seetings.
Not like plugin current survey seetings ? Because : with a lot of settings : it's really not adapated
Sample (i need to rewrite reloadAnyResponse settings)

Maybe need

  1. A user plugins settings like current survey plugins setting for only one or two (or 3) settings
  2. A complete page dedicated system for one plugin ?
ollehar

ollehar

2021-08-04 16:55

reporter   ~65840

Last edited: 2021-08-04 17:11

Example implementation to review:

// Extended by all custom plugin forms.
// Put this in sibling folder as MenuItem etc used by plugins, that is, application/libraries/PluginFormElements
// Use namespace LimeSurvey\PluginFormElements
// Extend composer.json with new autoload: "LimeSurvey\PluginFormElements\": "application/libraries/PluginFormElements/",
// Re-generate autoload files after changing composer.json, using composer tool
// Use similar idioms as in MenuItems when possible.
// Add render of form elements in file: application/views/admin/user/personalsettings.php
// Add save new data in application/controllers/admin/useraction.php, around line 679?
// Maybe only support adding new elements inside new tabs, not within existing tabs.
abstract class PluginFormElementBase
{
public function save($request): bool;
public function validate($request): bool;
public function toHtml(): string;
public function getTabName(): string;
}

// Plugin code
$event = $pluginManager->get('extendFormUserAccount');
$input = new PluginFormElementInput();
$input->setSave(function ($request) {
// Do the save logic
});
$input->setValidate(function ($request) {
// Do the validation logic
});
$newFormElements = [
$input
];
$event->push($newFormElements);

// LS core code to render form
public function actionUserAccount()
{
$data = [];
$event = new Event('extendFormUserAccount');
$pluginManager->publish($event);
/* @var PluginFormElementBase[] /
$newFormElements = $event->get();
$elementsByTabs = $pluginManager->sortFormElementsByTabs($newFormElements);
$data['pluginFormElements'] = $newFormElements;
$this->render('viewfile', $data);
}

// Add to plugin manager (or should be separate function? Does not use "this")
public function sortFormElementsByTabs(array $newFormElements): array
{
$elementsByTabs = [];
foreach ($newFormElements as $newFormElement) {
$elementsByTabs[$newFormElement->getTabName()] = $newFormElement;
}
return $elementsByTabs;
}

// LS core code to save form data
public function actionSaveUserAccount()
{
// ... save all usual POST data

// Add plugin logic
$event = new Event('extendFormUserAccount');
$pluginManager->publish($event);
/** @var PluginFormElementBase[] */
$newFormElements = $event->get();
$request = Yii::app()->getRequest();
foreach ($newFormElements as $formElement) {
    if ($formElement->validate($request)) {
        if (!$formElement->save($request)) {
            throw new CHttpException("Could not save plugin form element: " . $somethingUseful);
        }
    }
}

}

// LS core view file
?>
<form action="saveUserAccount">
<!-- Check for tab name - if it does not exist, create it -->
<?php foreach ($pluginFormElements as $tabName => $pluginFormElement): ?>
<?php if (!in_array($tabName, $listOfCreatedTabs)): ?>
<!-- Render new tab -->
<!-- Add tab new to $listOfCreatedTabs -->
<?php endif; ?>
<?php endforeach; ?>
<input name="username" />
<?php foreach ($pluginFormElements['My profile'] as $pluginFormElement): ?>
<?= $pluginFormElement->toHtml(); ?>
<?php endforeach; ?>
<?php foreach ($pluginFormElements['My personal settings'] as $pluginFormElement): ?>
<?= $pluginFormElement->toHtml(); ?>
<?php endforeach; ?>
<!-- Etc for every tab -->
<!-- TODO: OK to not support all tabs? -->
<!-- TODO: Must document which tabs are supported for each form in the software, and from which LS version. -->
</form>

DenisChenu

DenisChenu

2021-08-04 16:58

developer   ~65841

For me : the important pmart before code and how to add it : it's

  1. usuability : user point of view (one tab by plugin can broke easily for example : 4 plugins => 8 tabs)
  2. statibility and security: dev point of view
  3. easy to add ;)
ollehar

ollehar

2021-08-04 17:05

reporter   ~65842

  1. usuability : user point of view (one tab by plugin can broke easily for example : 4 plugins => 8 tabs)

Not sure how to fix this? If you have big plugin, it's better to add separate pages, not extend existing forms.

  1. statibility and security: dev point of view

As before, we must make sure to keep compatibility inside major version. That's why it's important that you leave feedback as soon as possible, too.

  1. easy to add ;)

See my example above, or here (not 100% correct, draft):

// Plugin code
$event = $pluginManager->get('extendFormUserAccount');
$input = new PluginFormElementInput();
$input->setSave(function ($request) {
// Do the save logic
});
$input->setValidate(function ($request) {
// Do the validation logic
});
$newFormElements = [
$input
];
$event->push($newFormElements);

DenisChenu

DenisChenu

2021-08-04 18:23

developer   ~65843

About the easy to add : it's tertiary ;)

I made an error on a previous message.
About User point of view, maybe best is

  1. A tab "Plugin settings" for a list of simple plugin : 2/4 simple settings (or button for 3)
  2. A "add tab" plugin for more complex plugin (like 2FA maybe)
  3. Review admin/pluginhelper/sa/fullpagewrapper/plugin/XXX to allow add header for
    • Default menu bar
    • User part (title + close to come back to user settings) by option
    • Other Object poart when someone find times or need it (SurveyGroups/QuestionGroups etc …)

Else for 2FA : it's My account, not User object ? right ?

ollehar

ollehar

2021-08-04 20:45

reporter   ~65845

Else for 2FA : it's My account, not User object ? right ?

Yes, the account settings page.

  1. A tab "Plugin settings" for a list of simple plugin

We can't add a tab for plugin settings on every form. "Plugin settings" is not very user friendly either, as it doesn't tell the user what the tab is specifically about.

  1. Review admin/pluginhelper/sa/fullpagewrapper/plugin/XXX to allow add header for

Other feature? Not sure what you mean here.

Mazi

Mazi

2021-08-05 08:30

updater   ~65847

@ollehar, I totally understand that it is complicated to add this at two versions and that this requires some additional ressources. But if this will not be developed for LS3, then just forget about it and use the ressources for something else. We are using LS 3 for > 90% of our projects and my experience is that >80% of all users are using LS3, maybe 5-10% use LS 2.x and only the minor rest is using a newer version. Of course, that will change, but that will take years.
We are starting all new projects based on Limesurvey 3 and that includes several big customers, long term users of Limesurvey. For all of them it will be useless if LS3 is not supported.
Please listen more to the community's/partner's voice...!

DenisChenu

DenisChenu

2021-08-05 10:36

developer   ~65848

  1. Review admin/pluginhelper/sa/fullpagewrapper/plugin/XXX to allow add header for
    Other feature? Not sure what you mean here.

See https://gitlab.com/SondagesPro/TokenManagement/TokenUsersListAndManage system for settings.

We are inside survey. It great if we can have same page with : inside User, inside SurveyGrouo, inside QuestionGroup, inside Question

About @Mazi message : i'm between the 2 position .

  1. Something in 5 is better than nothing.
  2. Clearly : must hack i 3.X and redo for 5.X ( same > 80% of user are on 3.X). Only ine big in 5.X : client for SurveyGroups permission system.
  3. The worst is something you work on it but disappear when develop goes to master : it happens 3 times minimum for me.
ollehar

ollehar

2021-08-05 11:18

reporter   ~65849

@Mazi, the issue is not adding the feature in two version, the problem is maintaining them in two versions. :) Bug fixes, added functionality, divergence in HTML or elsewhere, etc. We really want to move LS3 to "security fixes only" and focus entirely on LS5, for the sanity of the team.

Anyway, this feature has been postponed since we found another quicker fix that works for us. Maybe we can outsource its implementation later, since it's something that's needed and wanted. :)

ollehar

ollehar

2021-08-05 11:19

reporter   ~65850

  1. The worst is something you work on it but disappear when develop goes to master : it happens 3 times minimum for me.

Unit tests... It's the only way.

Issue History

Date Modified Username Field Change
2021-08-04 13:33 ollehar New Issue
2021-08-04 13:43 ollehar Priority none => normal
2021-08-04 13:43 ollehar Description Updated
2021-08-04 13:43 ollehar Sync to Zoho Project Yes => |Yes|
2021-08-04 13:43 LimeBot Zoho Projects ID => 85781000001116001
2021-08-04 13:44 ollehar Note Added: 65805
2021-08-04 13:45 ollehar Description Updated
2021-08-04 13:45 ollehar Sync to Zoho Project Yes => |Yes|
2021-08-04 14:10 gabrieljenik Note Added: 65806
2021-08-04 14:11 ollehar Note Added: 65807
2021-08-04 14:36 DenisChenu Note Added: 65809
2021-08-04 14:36 DenisChenu File Added: Capture d’écran du 2021-08-04 14-28-08.png
2021-08-04 14:36 DenisChenu File Added: Capture d’écran du 2021-08-04 14-34-48.png
2021-08-04 14:39 DenisChenu Relationship added related to 14290
2021-08-04 14:45 gabrieljenik Note Added: 65812
2021-08-04 15:03 Mazi Note Added: 65817
2021-08-04 15:06 ollehar Assigned To => JHoeck
2021-08-04 15:06 ollehar Status new => assigned
2021-08-04 15:07 ollehar Note Added: 65819
2021-08-04 15:08 ollehar Note Added: 65820
2021-08-04 15:10 ollehar Note Edited: 65820
2021-08-04 16:01 ollehar Note Added: 65836
2021-08-04 16:01 ollehar Description Updated
2021-08-04 16:01 ollehar Sync to Zoho Project Yes => |Yes|
2021-08-04 16:02 ollehar Note Edited: 65836
2021-08-04 16:08 ollehar Description Updated
2021-08-04 16:08 ollehar Sync to Zoho Project Yes => |Yes|
2021-08-04 16:27 ollehar Note Added: 65837
2021-08-04 16:29 ollehar Description Updated
2021-08-04 16:29 ollehar Sync to Zoho Project Yes => |Yes|
2021-08-04 16:49 DenisChenu Note Added: 65838
2021-08-04 16:55 ollehar Note Added: 65840
2021-08-04 16:58 DenisChenu Note Added: 65841
2021-08-04 16:58 ollehar Note Edited: 65840
2021-08-04 17:00 ollehar Note Edited: 65840
2021-08-04 17:01 ollehar Note Edited: 65840
2021-08-04 17:03 ollehar Note Edited: 65840
2021-08-04 17:05 ollehar Note Added: 65842
2021-08-04 17:11 ollehar Note Edited: 65840
2021-08-04 18:23 DenisChenu Note Added: 65843
2021-08-04 20:45 ollehar Note Added: 65845
2021-08-05 08:30 Mazi Note Added: 65847
2021-08-05 10:36 DenisChenu Note Added: 65848
2021-08-05 10:36 DenisChenu File Added: Capture d’écran du 2021-08-05 10-32-28.png
2021-08-05 11:18 ollehar Note Added: 65849
2021-08-05 11:19 ollehar Note Added: 65850