You are here

function geolocation_address_geolocation_field_map_widget_alter in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 modules/geolocation_address/geolocation_address.module \geolocation_address_geolocation_field_map_widget_alter()

Implements hook_geolocation_field_map_widget_alter().

File

modules/geolocation_address/geolocation_address.module, line 19
Provide address integration where due.

Code

function geolocation_address_geolocation_field_map_widget_alter(&$element, $context) {

  /** @var \Drupal\geolocation\Plugin\Field\FieldWidget\GeolocationMapWidgetBase $widget */
  $widget = $context['widget'];

  /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
  $field_definition = $context['field_definition'];
  $settings = $widget
    ->getThirdPartySettings('geolocation_address');
  if (empty($settings['enable'])) {
    return;
  }
  $address_label = t('Address');

  /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $all_field_definitions */
  $all_field_definitions = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions($field_definition
    ->getTargetEntityTypeId(), $field_definition
    ->getTargetBundle());
  foreach ($all_field_definitions as $single_field_definition) {
    if ($single_field_definition
      ->getName() == $settings['address_field']) {
      $address_label = $single_field_definition
        ->getLabel();
    }
  }
  $element['#attached'] = BubbleableMetadata::mergeAttachments(empty($element['#attached']) ? [] : $element['#attached'], [
    'library' => [
      'geolocation_address/geolocation_address.widget',
    ],
    'drupalSettings' => [
      'geolocation' => [
        'addressIntegration' => [
          $element['widget']['#field_name'] => [
            'geocoder' => $settings['geocoder'],
            'geocoder_settings' => $settings['geocoder_settings'],
            'address_field' => $settings['address_field'],
            'sync_mode' => $settings['sync_mode'],
            'direction' => $settings['direction'],
          ],
        ],
      ],
    ],
  ]);
  $element['#attributes'] = BubbleableMetadata::mergeAttachments(empty($element['#attributes']) ? [] : $element['#attributes'], [
    'data-address-integration' => $element['widget']['#field_name'],
  ]);
  if ($settings['sync_mode'] == 'manual') {
    if (empty($element['map'])) {
      return;
    }
    $element['map']['#controls']['geolocation_address'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'geolocation-map-control',
          'geolocation-address',
        ],
      ],
    ];
    if (!empty($element['#address_settings']['button_position'])) {
      $element['map']['#controls']['geolocation_address']['#attributes']['data-control-position'] = $element['#address_settings']['button_position'];
    }
    $element['map']['#controls']['geolocation_address']['address_pull'] = [
      '#type' => 'html_tag',
      '#tag' => 'button',
      '#attributes' => [
        'class' => [
          'address-button',
          'address-button-pull',
        ],
        'title' => t('Pull all address from %address onto map', [
          '%address' => $address_label,
        ]),
      ],
      '#value' => t('Pull from %address', [
        '%address' => $address_label,
      ]),
    ];
    if ($settings['direction'] == 'duplex') {
      $element['map']['#controls']['geolocation_address']['address_push'] = [
        '#type' => 'html_tag',
        '#tag' => 'button',
        '#attributes' => [
          'class' => [
            'address-button',
            'address-button-push',
          ],
          'title' => t('Push current location data to %address', [
            '%address' => $address_label,
          ]),
        ],
        '#value' => t('Push to %address', [
          '%address' => $address_label,
        ]),
      ];
    }
  }
}