InvoiceForm.php in Commerce Invoice 8.2
File
src/Form/InvoiceForm.php
View source
<?php
namespace Drupal\commerce_invoice\Form;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class InvoiceForm extends ContentEntityForm {
protected $dateFormatter;
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, DateFormatterInterface $date_formatter) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->dateFormatter = $date_formatter;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('date.formatter'));
}
public function form(array $form, FormStateInterface $form_state) {
$invoice = $this->entity;
$form = parent::form($form, $form_state);
$invoice_type = $this->entityTypeManager
->getStorage('commerce_invoice_type')
->load($invoice
->bundle());
$form['#title'] = $this
->t('Add new @invoice_type_label', [
'@invoice_type_label' => $invoice_type
->label(),
]);
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$this->entity
->save();
$this
->messenger()
->addMessage($this
->t('The invoice %label has been successfully saved.', [
'%label' => $this->entity
->label(),
]));
$order = $this->entity
->get('orders')
->get(0)->entity;
$redirect_url = $order
->toUrl('canonical');
if ($this->entity
->bundle() == 'default') {
$redirect_url = $order
->toUrl('invoices');
}
elseif ($this->entity
->bundle() == 'credit_memo') {
$redirect_url = $order
->toUrl('credit-memos');
}
$form_state
->setRedirectUrl($redirect_url);
}
}
Classes
Name |
Description |
InvoiceForm |
Form controller for the commerce_invoice entity edit forms. |