HubspotSettingsForm.php in Hubspot forms 8
File
src/Form/HubspotSettingsForm.php
View source
<?php
namespace Drupal\hubspot_forms\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class HubspotSettingsForm extends ConfigFormBase {
public function __construct(ConfigFactoryInterface $config_factory) {
parent::__construct($config_factory);
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'));
}
public function getFormId() {
return 'hubspot_forms_settings';
}
protected function getEditableConfigNames() {
return [
'hubspot_forms.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = \Drupal::config('hubspot_forms.settings');
$form['hubspot_api_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('Hubspot API Key'),
'#default_value' => $config
->get('hubspot_api_key'),
'#description' => $this
->t('Please use <strong>demo</strong> to load example forms.
Generate a <a href="https://app.hubspot.com/keys/get" target="_blank">new key</a>.
Make sure to clear Drupal cache after you change API Key.'),
'#required' => TRUE,
];
return parent::buildForm($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('hubspot_forms.settings');
$config
->set('hubspot_api_key', $form_state
->getValue('hubspot_api_key'))
->save();
\Drupal::cache()
->delete('hubspot_forms');
parent::submitForm($form, $form_state);
}
}