mailjet-8.x-2.7/src/Form/MailjetApiForm.php
src/Form/MailjetApiForm.php
<?php
namespace Drupal\mailjet\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\mailjet\MailjetApi;
use Mailjet\Resources;
/**
* Class for MailjetApiForm.
*
* @package Drupal\mailjet\Form
*/
class MailjetApiForm extends ConfigFormBase
{
private mixed $mailjetHandler;
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames()
{
return [
'mailjet_api.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId()
{
return 'mailjet_api_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$config_mailjet = $this->config('mailjet.settings');
$form = parent::buildForm($form, $form_state);
$form['api'] = [
'#type' => 'fieldset',
];
$form['api']['welcome'] = [
'#markup' => t(
'Welcome to the Mailjet Configuration page.</br>
If you are new to Mailjet, please register by clicking on the button above.</br>
Should you already have a pre-existing Mailjet account, please copy and paste your Mailjet API Key and Secret Key which can be found in <a href="https://app.mailjet.com/account/api_keys">your Mailjet account.</a>'
),
];
$form['api']['mailjet_username'] = [
'#type' => 'textfield',
'#title' => t('API Key'),
'#default_value' => $config_mailjet->get('mailjet_username'),
'#required' => TRUE,
];
$form['api']['mailjet_password'] = [
'#type' => 'textfield',
'#title' => t('Secret Key'),
'#default_value' => $config_mailjet->get('mailjet_password'),
'#required' => TRUE,
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
'#button_type' => 'primary',
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state)
{
}
/**
* {@inheritdoc}
* @throws \Exception
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
$config = \Drupal::service('config.factory')
->getEditable('mailjet.settings');
$config->set('mailjet_username', $form_state->getValue('mailjet_username'));
$config->set('mailjet_password', $form_state->getValue('mailjet_password'));
$config->save();
$mailjetApiClient = MailjetApi::getApiClient(
$form_state->getValue('mailjet_username'),
$form_state->getValue('mailjet_password')
);
$response = $mailjetApiClient->get(Resources::$Myprofile);
if ($response->success()) {
$config->set('mailjet_active', TRUE);
$params = [
'AllowedAccess' => 'campaigns,contacts,stats,pricing,account,reports',
'APIKeyALT' => $form_state->getValue('mailjet_username'),
'TokenType' => 'iframe',
'IsActive' => TRUE,
];
$this->mailjetHandler = \Drupal::service('mailjet.handler');
$responseApiToken = MailjetApi::createApiToken($params);
if (FALSE != $responseApiToken) {
$config->set('APItoken', $responseApiToken[0]['Token']);
$config->save();
mailjet_first_sync($this->mailjetHandler->getMailjetDefaultList());
\Drupal::messenger()->addMessage(t('The configuration options have been saved.'));
drupal_flush_all_caches();
} else {
$form_state->setErrorByName('mailjet_username', t('Token was NOT generated! Please try again.'));
}
} else {
\Drupal::messenger()->addMessage(
t('Please verify that you have entered your API and secret key correctly. Please note this plug-in is compatible for Mailjet v3 accounts only. Click <a href=" https://app.mailjet.com/support/why-do-i-get-an-api-error-when-trying-to-activate-a-mailjet-plug-in,497.htm"> here</a> for more information'),
'error'
);
}
}
}
