class InvoiceTypeForm in Commerce Invoice 8.2
Provides an invoice type form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\Core\Entity\BundleEntityFormBase
- class \Drupal\commerce\Form\CommerceBundleEntityFormBase
- class \Drupal\commerce_invoice\Form\InvoiceTypeForm uses EntityDuplicateFormTrait
- class \Drupal\commerce\Form\CommerceBundleEntityFormBase
- class \Drupal\Core\Entity\BundleEntityFormBase
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of InvoiceTypeForm
File
- src/
Form/ InvoiceTypeForm.php, line 18
Namespace
Drupal\commerce_invoice\FormView source
class InvoiceTypeForm extends CommerceBundleEntityFormBase {
use EntityDuplicateFormTrait;
/**
* The workflow manager.
*
* @var \Drupal\state_machine\WorkflowManagerInterface
*/
protected $workflowManager;
/**
* Constructs a new InvoiceTypeForm object.
*
* @param \Drupal\commerce\EntityTraitManagerInterface $trait_manager
* The entity trait manager.
* @param \Drupal\state_machine\WorkflowManagerInterface $workflow_manager
* The workflow manager.
*/
public function __construct(EntityTraitManagerInterface $trait_manager, WorkflowManagerInterface $workflow_manager) {
parent::__construct($trait_manager);
$this->workflowManager = $workflow_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.commerce_entity_trait'), $container
->get('plugin.manager.workflow'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\commerce_invoice\Entity\InvoiceTypeInterface $invoice_type */
$invoice_type = $this->entity;
$workflows = $this->workflowManager
->getGroupedLabels('commerce_invoice');
$number_pattern_storage = $this->entityTypeManager
->getStorage('commerce_number_pattern');
$number_patterns = $number_pattern_storage
->loadByProperties([
'targetEntityType' => 'commerce_invoice',
]);
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $invoice_type
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $invoice_type
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\commerce_invoice\\Entity\\InvoiceType::load',
'source' => [
'label',
],
],
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'#disabled' => !$invoice_type
->isNew(),
];
$form['workflow'] = [
'#type' => 'select',
'#title' => $this
->t('Workflow'),
'#options' => $workflows,
'#default_value' => $invoice_type
->getWorkflowId(),
'#description' => $this
->t('Used by all invoices of this type.'),
];
$form['numberPattern'] = [
'#type' => 'select',
'#title' => $this
->t('Number pattern'),
'#options' => EntityHelper::extractLabels($number_patterns),
'#default_value' => $invoice_type
->getNumberPatternId(),
];
$form['logo_file'] = [
'#title' => $this
->t('Logo'),
'#type' => 'managed_file',
'#upload_location' => 'public://commerce_invoice/',
'#upload_validators' => [
'file_validate_extensions' => [
'png jpg svg',
],
],
];
if ($file = $invoice_type
->getLogoFile()) {
$form['logo_file']['#default_value'] = [
'target_id' => $file
->id(),
];
}
$form['emails'] = [
'#type' => 'details',
'#title' => $this
->t('Emails'),
'#weight' => 5,
'#open' => TRUE,
'#collapsible' => TRUE,
'#tree' => FALSE,
];
$form['emails']['notice'] = [
'#markup' => '<p>' . $this
->t('Emails are sent in the HTML format. You will need a module such as <a href="https://www.drupal.org/project/swiftmailer">Swiftmailer</a> to send HTML emails.') . '</p>',
];
$form['emails']['sendConfirmation'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Email the customer a confirmation when an invoice is "confirmed" or paid'),
'#default_value' => $invoice_type
->isNew() ? TRUE : $invoice_type
->shouldSendConfirmation(),
];
$form['emails']['confirmationBcc'] = [
'#type' => 'textfield',
'#title' => $this
->t('Send a copy of the confirmation to this email:'),
'#default_value' => $invoice_type
->isNew() ? '' : $invoice_type
->getConfirmationBcc(),
'#states' => [
'visible' => [
':input[name="sendConfirmation"]' => [
'checked' => TRUE,
],
],
],
];
$token_types = [
'commerce_invoice',
];
$form['payment-terms'] = [
'#type' => 'details',
'#title' => $this
->t('Payment terms'),
'#tree' => FALSE,
'#open' => TRUE,
];
$form['payment-terms']['dueDays'] = [
'#type' => 'number',
'#size' => 3,
'#description' => $this
->t("used to determine the invoice's due date."),
'#field_suffix' => $this
->t('days'),
'#title' => $this
->t('Due date'),
'#default_value' => $invoice_type
->getDueDays(),
];
$form['payment-terms']['paymentTerms'] = [
'#type' => 'textarea',
'#title' => $this
->t('Payment terms'),
'#default_value' => $invoice_type
->getPaymentTerms(),
'#description' => $this
->t('The payment terms.'),
'#element_validate' => [
'token_element_validate',
],
'#token_types' => $token_types,
];
$form['payment-terms']['payment_terms_token_help'] = [
'#theme' => 'token_tree_link',
'#token_types' => $token_types,
];
$form['footerText'] = [
'#type' => 'textarea',
'#title' => $this
->t('Footer text'),
'#default_value' => $invoice_type
->getFooterText(),
'#description' => $this
->t('Text to display in the footer of the invoice.'),
'#element_validate' => [
'token_element_validate',
],
'#token_types' => $token_types,
];
$form['footer_text_token_help'] = [
'#theme' => 'token_tree_link',
'#token_types' => $token_types,
];
$form = $this
->buildTraitForm($form, $form_state);
if ($this->moduleHandler
->moduleExists('language')) {
$form['language'] = [
'#type' => 'details',
'#title' => $this
->t('Language settings'),
];
$form['language']['language_configuration'] = [
'#type' => 'commerce_invoice_language_configuration',
'#entity_information' => [
'entity_type' => 'commerce_invoice',
'bundle' => $invoice_type
->id(),
],
'#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('commerce_invoice', $invoice_type
->id()),
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$this
->validateTraitForm($form, $form_state);
/** @var \Drupal\commerce_invoice\Entity\InvoiceTypeInterface $invoice_type */
$invoice_type = $this->entity;
$logo_file = $form_state
->getValue([
'logo_file',
'0',
]);
/** @var \Drupal\file\Entity\File $file */
if (!empty($logo_file) && ($file = $this->entityTypeManager
->getStorage('file')
->load($logo_file))) {
$file
->setPermanent();
$file
->save();
$invoice_type
->setLogo($file
->uuid());
}
else {
$invoice_type
->setLogo(NULL);
}
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$this->entity
->save();
$this
->postSave($this->entity, $this->operation);
$this
->submitTraitForm($form, $form_state);
$this
->messenger()
->addMessage($this
->t('Saved the %label invoice type.', [
'%label' => $this->entity
->label(),
]));
$form_state
->setRedirect('entity.commerce_invoice_type.collection');
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BundleEntityFormBase:: |
protected | function | Protects the bundle entity's ID property's form element against changes. | |
CommerceBundleEntityFormBase:: |
protected | property | The entity trait manager. | |
CommerceBundleEntityFormBase:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityForm:: |
|
CommerceBundleEntityFormBase:: |
protected | function | Builds the trait form. | |
CommerceBundleEntityFormBase:: |
protected | function | Submits the trait form. | |
CommerceBundleEntityFormBase:: |
protected | function | Validates the trait form. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityDuplicateFormTrait:: |
protected | property | The source entity. | |
EntityDuplicateFormTrait:: |
public | function | ||
EntityDuplicateFormTrait:: |
protected | function | Invokes entity duplicate hooks after the entity has been duplicated. | |
EntityDuplicateFormTrait:: |
public | function | ||
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
InvoiceTypeForm:: |
protected | property | The workflow manager. | |
InvoiceTypeForm:: |
public static | function |
Instantiates a new instance of this class. Overrides CommerceBundleEntityFormBase:: |
|
InvoiceTypeForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
InvoiceTypeForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
InvoiceTypeForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
InvoiceTypeForm:: |
public | function |
Constructs a new InvoiceTypeForm object. Overrides CommerceBundleEntityFormBase:: |
|
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |