You are here

public function GmapFieldSettingsForm::buildForm in Google Map Field 8

Implements \Drupal\Core\Form\FormInterface::buildForm().

Overrides ConfigFormBase::buildForm

File

src/Form/GmapFieldSettingsForm.php, line 68
Contains \Drupal\google_map_field\Form\GmapFieldSettingsForm.

Class

GmapFieldSettingsForm
Administration settings form.

Namespace

Drupal\google_map_field\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('google_map_field.settings');
  $settings = $config
    ->get();
  $api_key = '';
  $client_id = '';
  if (isset($settings['google_map_field_apikey']) && trim($settings['google_map_field_apikey']) != '') {
    $api_key = $settings['google_map_field_apikey'];
  }
  if (isset($settings['google_map_field_map_client_id']) && trim($settings['google_map_field_map_client_id']) != '') {
    $client_id = $settings['google_map_field_map_client_id'];
  }
  $form['google_map_field_auth_method'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Google API Authentication Method'),
    '#default_value' => isset($settings['google_map_field_auth_method']) ? $settings['google_map_field_auth_method'] : GOOGLE_MAP_FIELD_AUTH_KEY,
    '#options' => [
      GOOGLE_MAP_FIELD_AUTH_KEY => $this
        ->t('API Key'),
      GOOGLE_MAP_FIELD_AUTH_WORK => $this
        ->t('Google Maps API for Work'),
    ],
  ];
  $form['google_map_field_apikey'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Google Maps API Key'),
    '#description' => $this
      ->t('Obtain a Google Maps Javascript API key at <a href="@link">@link</a>', [
      '@link' => 'https://developers.google.com/maps/documentation/javascript/get-api-key',
    ]),
    '#default_value' => $api_key,
    '#required' => FALSE,
    '#size' => 80,
    '#states' => [
      'visible' => [
        ':input[name="google_map_field_auth_method"]' => [
          'value' => GOOGLE_MAP_FIELD_AUTH_KEY,
        ],
      ],
    ],
  ];
  $form['google_map_field_map_client_id'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Google Maps API for Work: Client ID'),
    '#description' => $this
      ->t('For more information, visit: <a href="@link">@link</a>', [
      '@link' => 'https://developers.google.com/maps/documentation/javascript/get-api-key#client-id',
    ]),
    '#default_value' => $client_id,
    '#required' => FALSE,
    '#size' => 80,
    '#states' => [
      'visible' => [
        ':input[name="google_map_field_auth_method"]' => [
          'value' => GOOGLE_MAP_FIELD_AUTH_WORK,
        ],
      ],
    ],
  ];
  return parent::buildForm($form, $form_state);
}