PHP code example of kreatif / statamic-forms

1. Go to this page and download the library: Download kreatif/statamic-forms 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/ */

    

kreatif / statamic-forms example snippets


// Disable globally for all forms (can be overridden per form)
'disable_statamic_email' => false,

// Or per-form in handlers:
'handlers' => [
    'contact_form' => [
        'disable_statamic_email' => true, // This form's emails are handled by the addon
        'actions' => [...]
    ]
]

'logging' => [
    'enabled' => true,
    'channel' => null, // null uses default log channel
    'level' => 'info', // debug, info, warning, error
],

'email' => [
    'logo_url' => env('FORM_EMAIL_LOGO_URL', null),
    'organization_name' => env('FORM_EMAIL_ORG_NAME', config('app.name')),
    'sanitize_content' => true, // Prevent XSS in emails
],

'handlers' => [
    'contact_form' => [
        // Override global settings for this form
        'logo_url' => env('CONTACT_FORM_LOGO_URL'),
        'organization_name' => 'Your Company',
        'disable_statamic_email' => true,

        // Rate limiting (optional)
        'rate_limit' => [
            'enabled' => true,
            'max_attempts' => 5,
            'decay_minutes' => 60,
            'by' => 'ip', // 'ip', 'email', or 'session'
        ],

        // Define the actions to run for this form
        'actions' => [
            // ... actions are defined here ...
        ],
    ],
],

SendAdminNotificationAction::class => [
    'enabled' => true,
    'to' => '[email protected]',
    'queue' => true,
    'queue_connection' => 'redis',
    'queue_name' => 'emails',
    'delay' => 5, // Wait 5 seconds before processing
],

SendAdminNotificationAction::class => [
    'enabled' => true,
    'to' => '[email protected]',
    'when' => [
        'field' => 'notify_admin',
        'operator' => '=',
        'value' => true,
    ],
],

AddToIubendaAction::class => [
    'enabled' => true,
    'priority' => 10, // Run this first
],

'handlers' => [
    'contact_form' => [
        'rate_limit' => [
            'enabled' => true,
            'max_attempts' => 5,      // Max 5 submissions
            'decay_minutes' => 60,    // Per 60 minutes
            'by' => 'ip',            // Rate limit by: 'ip', 'email', or 'session'
        ],
    ],
],

use Kreatif\StatamicForms\Actions\SendAdminNotificationAction;

SendAdminNotificationAction::class => [
'enabled' => true,
'to'      => '[email protected], [email protected]', // Comma-separated string
'cc'      => '[email protected]',
'bcc'     => '[email protected]',
'from'    => '[email protected]',
'reply_to'=> '[email protected]', // If not set, defaults to the user's email
'subject' => 'translate:kreatif-forms::forms.new_submission_subject', // Translatable subject
],

use Kreatif\StatamicForms\Actions\SendAutoresponderAction;

SendAutoresponderAction::class => [
'enabled' => true,
'from'    => '[email protected]',
'reply_to'=> '[email protected]', // Optional. If omitted, no Reply-To header is added.
'subject' => 'Thanks for your message!',
// Optionally specify a custom template
'html'    => 'kreatif-forms::html.emails.special-autoresponder',
'text'    => 'kreatif-forms::text.emails.special-autoresponder',
],

use Kreatif\StatamicForms\Actions\AddToIubendaAction;

AddToIubendaAction::class => [
    'enabled' => true,
    'preferences' => [
        'newsletter' => 'newsletter', // Reads the submitted newsletter field
        'marketing' => ['field' => 'marketing_consent', 'default' => false],
    ],
    'legal_notices' => [
        'privacy_policy' => 'privacy', // Reads the submitted privacy checkbox
        'cookie_policy' => true,       // Fixed config values still work
    ],
    'field_mapping' => [
        // Iubenda API Key => Your Form Field Handle
        'first_name' => 'vorname',
        'last_name'  => 'nachname',
        'email'      => 'email_address',
    ],
],

'field_mapping' => [
    'first_name' => 'full_name', // Map the full name field
    'last_name'  => null,      // Set last_name to null
    'email'      => 'email',
],

'handlers' => [
    'contact' => [
        'actions' => [
            SendAdminNotificationAction::class => ['enabled' => true, 'to' => '[email protected]'],
            SendAutoresponderAction::class => ['enabled' => true],
        ],
    ],
],

'handlers' => [
    'inquiries' => [
        'actions' => [
            // This action will run, but its 'to' and 'subject' will be overridden
            SendAdminNotificationAction::class => ['enabled' => true, 'to' => '[email protected]'],
        ],
    ],
],

 'handlers' => [
    'newsletter_signup' => [
        'actions' => [
            // Only the Iubenda action is defined
            AddToIubendaAction::class => ['enabled' => true],
        ],
    ],
],

namespace App\Listeners;

use Kreatif\StatamicForms\Events\ActionFailed;
use Illuminate\Support\Facades\Log;

class LogFailedFormActions
{
    public function handle(ActionFailed $event): void
    {
        Log::critical('Form action failed', [
            'action' => $event->actionClass,
            'form' => $event->submission->form()->handle(),
            'errors' => $event->result->getErrors(),
        ]);

        // Maybe send alert to monitoring service
        // Sentry::captureMessage('Form action failed');
    }
}

protected $listen = [
    \Kreatif\StatamicForms\Events\ActionFailed::class => [
        \App\Listeners\LogFailedFormActions::class,
    ],
];

namespace App\FormActions;

use Kreatif\StatamicForms\Actions\BaseAction;
use Kreatif\StatamicForms\Contracts\ActionResult;
use Statamic\Forms\Submission;

class SendToCustomCrmAction extends BaseAction
{
    protected function handle(Submission $submission, array $config): ActionResult
    {
        $apiKey = $config['api_key'] ?? env('CRM_API_KEY');
        $endpoint = $config['endpoint'] ?? 'https://crm.example.com/api/contacts';

        // Your custom logic here
        $response = Http::post($endpoint, [
            'api_key' => $apiKey,
            'name' => $submission->get('name'),
            'email' => $submission->get('email'),
        ]);

        if ($response->failed()) {
            return ActionResult::failure(
                errors: [$response->body()],
                message: 'Failed to send to CRM'
            );
        }

        return ActionResult::success(
            data: $response->json(),
            message: 'Successfully sent to CRM'
        );
    }

    public function validate(array $config): bool
    {
        return !empty($config['api_key'] ?? env('CRM_API_KEY'));
    }

    public function getPriority(): int
    {
        return 95; // Run after most other actions
    }
}

use App\FormActions\SendToCustomCrmAction;

'handlers' => [
    'contact_form' => [
        'actions' => [
            SendToCustomCrmAction::class => [
                'enabled' => true,
                'api_key' => env('CRM_API_KEY'),
                'endpoint' => 'https://crm.example.com/api/contacts',
            ],
        ],
    ],
],
bash
    # Publish the configuration file (if-forms-config
    
    # Publish email templates (optional)
    php please vendor:publish --tag=kreatif-forms-views
    
    # Publish language files (optional)
    php please vendor:publish --tag=kreatif-forms-lang