PHP code example of terminal42 / contao-ajaxform

1. Go to this page and download the library: Download terminal42/contao-ajaxform library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

terminal42 / contao-ajaxform example snippets


namespace App\Migration;

use Contao\CoreBundle\Migration\AbstractMigration;
use Contao\CoreBundle\Migration\MigrationResult;
use Doctrine\DBAL\Connection;

class AjaxFormMigration extends AbstractMigration
{
    public function __construct(private readonly Connection $connection)
    {
    }

    public function shouldRun(): bool
    {
        $schemaManager = $this->connection->createSchemaManager();

        if (!$schemaManager->tablesExist(['tl_content', 'tl_form'])) {
            return false;
        }

        $columns = $schemaManager->listTableColumns('tl_form');

        if (!isset($columns['ajax'], $columns['confirmation'])) {
            return false;
        }

        $total = $this->connection->fetchOne('SELECT COUNT(*) FROM tl_content WHERE type=?', ['ajaxform']);

        return $total > 0;
    }

    public function run(): MigrationResult
    {
        $records = $this->connection->fetchAllAssociative('SELECT id, form, text FROM tl_content WHERE type=?', ['ajaxform']);

        foreach ($records as $record) {
            $this->connection->update('tl_content', ['type' => 'form'], ['id' => $record['id']]);
            $this->connection->update('tl_form', ['confirmation' => $record['text'], 'ajax' => 1], ['id' => $record['form']]);
        }

        return $this->createResult(true);
    }
}