InviteResendForm.php in Invite 8
File
src/Form/InviteResendForm.php
View source
<?php
namespace Drupal\invite\Form;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\invite\InviteConstants;
use Drupal\invite_by_email\Plugin\Invite\InviteByEmail;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\invite\InviteInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Url;
class InviteResendForm extends FormBase {
protected $inviteStorage;
protected $entityTypeManager;
protected $messenger;
public function __construct(EntityStorageInterface $invite_storage, EntityTypeManager $entityTypeManager, MessengerInterface $messenger) {
$this->entityTypeManager = $entityTypeManager;
$this->inviteStorage = $invite_storage;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($entity_type_manager
->getStorage('invite'), $container
->get('entity_type.manager'), $container
->get('messenger'));
}
public function getFormId() {
return 'invite_resend_form';
}
public function buildForm(array $form, FormStateInterface $form_state, InviteInterface $invite = NULL) {
$form['#title'] = $this
->t('Resend invite to @field_invite_email_address', [
'@field_invite_email_address' => $invite->field_invite_email_address->value,
]);
$this->inviteStorage = $invite;
$form['actions']['#type'] = 'actions';
$form['resend_invite'] = [
'#type' => 'submit',
'#title' => $this
->t('Resend Invite'),
'#description' => $this
->t('Resend current invite.'),
'#button_type' => 'primary',
'#value' => $this
->t('Resend Invite'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$invite = $this->inviteStorage;
$invite_by_email = new InviteByEmail([], 0, 0, $this->messenger);
$invite_by_email
->send($invite);
$invite
->set('status', InviteConstants::INVITE_VALID);
$invite
->save();
$url = Url::fromRoute('invite.invite_list');
$form_state
->setRedirectUrl($url);
}
}