GeofieldMapSettingsForm.php in Geofield Map 8
File
src/Form/GeofieldMapSettingsForm.php
View source
<?php
namespace Drupal\geofield_map\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\LinkGeneratorInterface;
class GeofieldMapSettingsForm extends ConfigFormBase {
protected $link;
public function __construct(ConfigFactoryInterface $config_factory, LinkGeneratorInterface $link_generator) {
parent::__construct($config_factory);
$this->link = $link_generator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('link_generator'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->configFactory
->get('geofield_map.settings');
$form['#tree'] = TRUE;
$form['gmap_api_key'] = [
'#type' => 'textfield',
'#default_value' => $config
->get('gmap_api_key'),
'#title' => $this
->t('Gmap Api Key (@link)', [
'@link' => $this->link
->generate(t('Get a Key/Authentication for Google Maps Javascript Library'), Url::fromUri('https://developers.google.com/maps/documentation/javascript/get-api-key', [
'absolute' => TRUE,
'attributes' => [
'target' => 'blank',
],
])),
]),
'#description' => $this
->t('Geofield Map requires a valid Google API key for his main features based on Google & Google Maps APIs.'),
'#placeholder' => $this
->t('Input a valid Gmap API Key'),
];
return parent::buildForm($form, $form_state);
}
public function getFormId() {
return 'geofield_map_settings';
}
protected function getEditableConfigNames() {
return [
'geofield_map.settings',
];
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->configFactory()
->getEditable('geofield_map.settings');
$config
->set('gmap_api_key', $form_state
->getValue('gmap_api_key'));
$config
->save();
drupal_set_message($this
->t('The Geofield Map configurations have been saved.'));
}
}