invoicemgmt-1.0.0/src/Form/InvoiceMgmtSettingsForm.php
src/Form/InvoiceMgmtSettingsForm.php
<?php
declare(strict_types=1);
namespace Drupal\invoicemgmt\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
/**
* Configuration form for Invoice Management settings.
*/
final class InvoiceMgmtSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'invoicemgmt_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames(): array {
return ['invoicemgmt.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$config = $this->config('invoicemgmt.settings');
$form['logo'] = [
'#type' => 'managed_file',
'#title' => $this->t('Logo'),
'#description' => $this->t('Upload a logo for invoices.'),
'#upload_location' => 'public://invoicemgmt/logos/',
'#upload_validators' => [
'FileExtension' => [
'extensions' => 'png jpg jpeg gif',
],
'FileSizeLimit' => [
'fileLimit' => 2097152,
],
],
'#default_value' => $config->get('logo') ? [$config->get('logo')] : [],
];
$form['terms_conditions'] = [
'#type' => 'textarea',
'#title' => $this->t('Terms and Conditions'),
'#description' => $this->t('Enter the terms and conditions text.'),
'#default_value' => $config->get('terms_conditions'),
'#rows' => 8,
];
$form['bank_details'] = [
'#type' => 'textarea',
'#title' => $this->t('Bank Details'),
'#description' => $this->t('Enter bank account details.'),
'#default_value' => $config->get('bank_details'),
'#rows' => 6,
];
$form['currency'] = [
'#type' => 'select',
'#title' => $this->t('Currency'),
'#description' => $this->t('Select the default currency for invoices.'),
'#options' => [
'GBP' => $this->t('GBP (£) - British Pound'),
'USD' => $this->t('USD ($) - US Dollar'),
'AED' => $this->t('AED (د.إ) - UAE Dirham'),
'INR' => $this->t('INR (₹) - Indian Rupee'),
'EUR' => $this->t('EUR (€) - Euro'),
],
'#default_value' => $config->get('currency') ?: 'GBP',
'#required' => TRUE,
];
$form['seller_name'] = [
'#type' => 'textfield',
'#title' => $this->t('Seller Name'),
'#description' => $this->t('Enter the seller/company name.'),
'#default_value' => $config->get('seller_name'),
'#maxlength' => 255,
'#required' => TRUE,
];
$form['seller_address'] = [
'#type' => 'textarea',
'#title' => $this->t('Seller Address'),
'#description' => $this->t('Enter the seller/company address.'),
'#default_value' => $config->get('seller_address'),
'#rows' => 4,
'#required' => TRUE,
];
$form['invoice_number_prefix'] = [
'#type' => 'textfield',
'#title' => $this->t('Invoice Number Prefix'),
'#description' => $this->t('Enter the prefix for invoice numbers (e.g., INV). Format will be: PREFIX-YYYYMM-UNIQUECODE'),
'#default_value' => $config->get('invoice_number_prefix') ?: 'INV',
'#maxlength' => 10,
'#required' => TRUE,
];
$form['footer_text'] = [
'#type' => 'textarea',
'#title' => $this->t('Footer Text'),
'#description' => $this->t('Enter custom footer text for invoices (e.g., Thank you for your business!).'),
'#default_value' => $config->get('footer_text') ?: 'Thank you for your business!',
'#rows' => 3,
];
$form['primary_color'] = [
'#type' => 'color',
'#title' => $this->t('Primary Color'),
'#description' => $this->t('Choose the primary color for invoice header and elements.'),
'#default_value' => $config->get('primary_color') ?: '#667eea',
];
$form['secondary_color'] = [
'#type' => 'color',
'#title' => $this->t('Secondary Color'),
'#description' => $this->t('Choose the secondary color for invoice header gradient.'),
'#default_value' => $config->get('secondary_color') ?: '#764ba2',
];
$form['accent_color'] = [
'#type' => 'color',
'#title' => $this->t('Accent Color'),
'#description' => $this->t('Choose the accent color for highlights and borders.'),
'#default_value' => $config->get('accent_color') ?: '#4299e1',
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$config = $this->config('invoicemgmt.settings');
// Handle logo file.
$logo_fid = NULL;
$logo_upload = $form_state->getValue('logo');
if (!empty($logo_upload[0])) {
$file = File::load($logo_upload[0]);
if ($file) {
$file->setPermanent();
$file->save();
$logo_fid = $file->id();
}
}
$config
->set('logo', $logo_fid)
->set('terms_conditions', $form_state->getValue('terms_conditions'))
->set('bank_details', $form_state->getValue('bank_details'))
->set('currency', $form_state->getValue('currency'))
->set('seller_name', $form_state->getValue('seller_name'))
->set('seller_address', $form_state->getValue('seller_address'))
->set('invoice_number_prefix', $form_state->getValue('invoice_number_prefix'))
->set('footer_text', $form_state->getValue('footer_text'))
->set('primary_color', $form_state->getValue('primary_color'))
->set('secondary_color', $form_state->getValue('secondary_color'))
->set('accent_color', $form_state->getValue('accent_color'))
->save();
parent::submitForm($form, $form_state);
}
}
