You are here

GeofieldMapSettingsForm.php in Geofield Map 8

Same filename and directory in other branches
  1. 8.2 src/Form/GeofieldMapSettingsForm.php

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;

/**
 * Implements the GeofieldMapSettingsForm controller.
 *
 * @see \Drupal\Core\Form\FormBase
 */
class GeofieldMapSettingsForm extends ConfigFormBase {

  /**
   * The Link generator Service.
   *
   * @var \Drupal\Core\Utility\LinkGeneratorInterface
   */
  protected $link;

  /**
   * GeofieldMapSettingsForm constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
   *   The Link Generator service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, LinkGeneratorInterface $link_generator) {
    parent::__construct($config_factory);
    $this->link = $link_generator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('link_generator'));
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'geofield_map_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'geofield_map.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  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();

    // Confirmation on form submission.
    drupal_set_message($this
      ->t('The Geofield Map configurations have been saved.'));
  }

}

Classes

Namesort descending Description
GeofieldMapSettingsForm Implements the GeofieldMapSettingsForm controller.