You are here

public function WebformLocationPlaces::form in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformLocationPlaces.php \Drupal\webform\Plugin\WebformElement\WebformLocationPlaces::form()

Gets the actual configuration webform array to be built.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array An associative array contain the element's configuration webform without any default values.

Overrides WebformLocationBase::form

File

src/Plugin/WebformElement/WebformLocationPlaces.php, line 49

Class

WebformLocationPlaces
Provides an 'location' element using Algolia Places.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $form['composite']['app_id'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Algolia application id'),
    '#description' => $this
      ->t('Algolia requires users to use a valid application id and API key for more than 1,000 requests per day. By <a href="https://www.algolia.com/users/sign_up/places">signing up</a>, you can create a free Places app and access your API keys.'),
  ];
  $default_app_id = $this->configFactory
    ->get('webform.settings')
    ->get('element.default_algolia_places_app_id');
  if ($default_app_id) {
    $form['composite']['app_id']['#description'] .= '<br /><br />' . $this
      ->t('Defaults to: %value', [
      '%value' => $default_app_id,
    ]);
  }
  else {
    $form['composite']['app_id']['#required'] = TRUE;
    if ($this->currentUser
      ->hasPermission('administer webform')) {
      $t_args = [
        ':href' => UrlGenerator::fromRoute('webform.config.elements')
          ->toString(),
      ];
      $form['composite']['app_id']['#description'] .= '<br /><br />' . $this
        ->t('You can either enter an element specific application id and API key here or set the <a href=":href">default site-wide application id and API key</a>.', $t_args);
    }
  }
  $form['composite']['api_key'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Algolia API key'),
  ];
  $default_api_key = $this->configFactory
    ->get('webform.settings')
    ->get('element.default_algolia_places_api_key');
  if ($default_api_key) {
    $form['composite']['api_key']['#description'] = $this
      ->t('Defaults to: %value', [
      '%value' => $default_api_key,
    ]);
  }
  return $form;
}