You are here

trait PcaAddressFieldWidgetTrait in Loqate 2.x

Same name and namespace in other branches
  1. 8 src/PcaAddressFieldWidgetTrait.php \Drupal\loqate\PcaAddressFieldWidgetTrait

Class PcaAddressFieldWidgetTrait.

@package Drupal\loqate

Hierarchy

2 files declare their use of PcaAddressFieldWidgetTrait
AddressPcaAddressWidget.php in modules/pca_address/src/Plugin/Field/FieldWidget/AddressPcaAddressWidget.php
LoqatePcaAddressDefaultWidget.php in src/Plugin/Field/FieldWidget/LoqatePcaAddressDefaultWidget.php

File

src/PcaAddressFieldWidgetTrait.php, line 10

Namespace

Drupal\loqate
View source
trait PcaAddressFieldWidgetTrait {

  /**
   * Get the default settings for the field widget.
   */
  public static function getFieldWidgetDefaultSettings() {
    return [
      'pca_fields' => [],
      'pca_options' => [],
      'show_address_fields' => FALSE,
      'allow_manual_input' => TRUE,
      'loqate_api_key' => NULL,
    ];
  }

  /**
   * Get widget settings form.
   */
  public function buildFieldWidgetsettingsForm(array $form) {
    $form['show_address_fields'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show address fields'),
      '#default_value' => $this
        ->getSetting('show_address_fields'),
    ];
    $form['allow_manual_input'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Allow manual input'),
      '#default_value' => $this
        ->getSetting('allow_manual_input'),
    ];
    $form['loqate_api_key'] = [
      '#type' => 'key_select',
      '#title' => $this
        ->t('Loqate API key'),
      '#default_value' => $this
        ->getSetting('loqate_api_key'),
    ];
    return $form;
  }

  /**
   * Build the widget settings summary.
   */
  public function buildFieldWidgetSettingsSummary() {
    $summary = [];
    $widget_settings = $this
      ->getSettings();
    $summary[] = $this
      ->t('Show address fields: @bool', [
      '@bool' => (bool) $widget_settings['show_address_fields'] ? 'Yes' : 'No',
    ]);
    $summary[] = $this
      ->t('Allow manual input: @bool', [
      '@bool' => (bool) $widget_settings['allow_manual_input'] ? 'Yes' : 'No',
    ]);
    $summary[] = $this
      ->t('Key ID: @key', [
      '@key' => !empty($widget_settings['loqate_api_key']) ? $widget_settings['loqate_api_key'] : 'None',
    ]);
    return $summary;
  }
  public function buildFieldWidgetFormElement(array $element) {
    $widget_settings = $this
      ->getSettings();

    // Set field mapping settings.
    $element['address']['#pca_fields'] = $widget_settings['pca_fields'];

    // Set options settings.
    $element['address']['#pca_options'] = $widget_settings['pca_options'];

    // Set show address fields bool.
    $element['address']['#show_address_fields'] = (bool) $widget_settings['show_address_fields'];

    // Set allow manual input bool.
    $element['address']['#allow_manual_input'] = (bool) $widget_settings['allow_manual_input'];

    // Set options key if set.
    if ($widget_settings['loqate_api_key']) {
      $element['address']['#pca_options']['key'] = $widget_settings['loqate_api_key'];
    }
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PcaAddressFieldWidgetTrait::buildFieldWidgetFormElement public function
PcaAddressFieldWidgetTrait::buildFieldWidgetsettingsForm public function Get widget settings form.
PcaAddressFieldWidgetTrait::buildFieldWidgetSettingsSummary public function Build the widget settings summary.
PcaAddressFieldWidgetTrait::getFieldWidgetDefaultSettings public static function Get the default settings for the field widget.