8000 Implement a "store in session" settings for forms by fritzmg · Pull Request #8245 · contao/contao · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implement a "store in session" settings for forms #8245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms 8000 of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from

Conversation

fritzmg
Copy link
Contributor
@fritzmg fritzmg commented Mar 26, 2025

A recent question/discussion on Slack whether and how long Contao forms store their data in the session made me think: I rarely ever use the {{form_session_data::*}} insert tags (or previously {{post::*}}). Yet, we always store the form data in the session, albeit briefly.

Thus I was thinking: wouldn't it make sense to only do this, when required? If you do not need this then your vanilla Contao instance might never need a session (and thus CSRF token) cookie in the front end at all.

This PR adds such a setting, similar to tl_form.storeValues.


It also adds a migration so that all existing forms have this setting enabled, for BC.

For new forms you then only enable this setting, if you need the data from this form in a {{form_session_data::*}} insert tag.

@fritzmg fritzmg added this to the 5.6 milestone Mar 26, 2025
@fritzmg fritzmg self-assigned this Mar 26, 2025
@fritzmg fritzmg requested a review from leofeyer as a code owner March 26, 2025 19:33
Comment on lines +48 to +58
$platform = $this->connection->getDatabasePlatform();
$schemaManager = $this->connection->createSchemaManager();
$fromTable = $schemaManager->introspectSchema()->getTable('tl_form');
$toTable = clone $fromTable;
$toTable->addColumn('storeSession', Types::BOOLEAN, ['default' => false]);

$tableDiff = $schemaManager->createComparator()->compareTables($fromTable, $toTable);

foreach ($platform->getAlterTableSQL($tableDiff) as $query) {
$this->connection->executeQuery($query);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only run the specific ALTER TABLE query that is required for the migration. Any other changes should be shown to the user on the CLI so they can decide whether or not they want to apply them.

Copy link
Contributor Author
@fritzmg fritzmg May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will not work in this case.

  1. We want to leave the setting disabled by default.
  2. For any existing forms need to enable the setting.
  3. For the migration to know when to run it determines whether the storeSession field has already been added or not.
  4. If the field was not added, it will add the field and set it to enabled.
  5. If the field was already added, the migration will not run.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-->

All of that is already implemented. You just have to replace lines 48 to 58 with a simple ALTER TABLE … statement as we do in other migrations:

$this->connection->executeStatement('
ALTER TABLE
tl_user_group
ADD
frontendModules BLOB DEFAULT NULL
');

Copy link
Contributor Author
@fritzmg fritzmg May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code you referenced runs this ALTER TABLE, nothing else. I deliberately did not use a direct statement due to issues like #7985.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I cannot merge the PR, I'm afraid. A concept change requires previous discussion and must be implemented for all migrations, not just for one.

Copy link
Contributor Author
@fritzmg fritzmg May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#8165 did not need compareTables() as it creates a new table using Doctrine DBAL. This PR on the other hand adds a new column to an existing table using Doctrine DBAL.

This PR and #8165 are the same in the sense that they are both using Doctrine DBAL schemas in order to alter the database, rather than executing ALTER TABLE or CREATE TABLE manually.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree with @leofeyer here. A simple ALTER TABLE statement should always work, we use that all over the place (I also just used this in #8365)

Copy link
Contributor Author
@fritzmg fritzmg May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I do not want to look up / remember what statement I need to execute when I need to create a column with the configuration ['type' => 'boolean', 'default' => false] for example. Ideally the database abstraction layer should take care of this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I will need to look up what the DBAL abstraction notation is, I don't need to look up SQL for that 😂

Copy link
Contributor Author
@fritzmg fritzmg May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the DBAL abstraction it will always automatically use the correct type. Imagine if MySQL introduces a native boolean type and DBAL implement this for that platform. If we don't use the abstraction this migration will execute a different statement than our DCA migration will do, since the DCA uses the abstraction and this migration would not.

@fritzmg fritzmg added the up for discussion Issues and PRs which will be discussed in our monthly calls. label May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature up for discussion Issues and PRs which will be discussed in our monthly calls.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0