You are here

public function GeolocationMapWidgetBase::form in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldWidget/GeolocationMapWidgetBase.php \Drupal\geolocation\Plugin\Field\FieldWidget\GeolocationMapWidgetBase::form()

Creates a form element for a field.

If the entity associated with the form is new (i.e., $entity->isNew() is TRUE), the 'default value', if any, is pre-populated. Also allows other modules to alter the form element by implementing their own hooks.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: An array of the field values. When creating a new entity this may be NULL or an empty array to use default values.

array $form: An array representing the form that the editing element will be attached to.

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

int $get_delta: Used to get only a specific delta value of a multiple value field.

Return value

array The form element array created for this field.

Overrides WidgetBase::form

2 calls to GeolocationMapWidgetBase::form()
GeolocationGoogleWidget::form in modules/geolocation_google_maps/src/Plugin/Field/FieldWidget/GeolocationGoogleWidget.php
Creates a form element for a field.
GeolocationLeafletWidget::form in modules/geolocation_leaflet/src/Plugin/Field/FieldWidget/GeolocationLeafletWidget.php
Creates a form element for a field.
2 methods override GeolocationMapWidgetBase::form()
GeolocationGoogleWidget::form in modules/geolocation_google_maps/src/Plugin/Field/FieldWidget/GeolocationGoogleWidget.php
Creates a form element for a field.
GeolocationLeafletWidget::form in modules/geolocation_leaflet/src/Plugin/Field/FieldWidget/GeolocationLeafletWidget.php
Creates a form element for a field.

File

src/Plugin/Field/FieldWidget/GeolocationMapWidgetBase.php, line 284

Class

GeolocationMapWidgetBase
Map widget base.

Namespace

Drupal\geolocation\Plugin\Field\FieldWidget

Code

public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL) {
  $element = parent::form($items, $form, $form_state, $get_delta);
  $settings = $this
    ->getSettings();
  $id = Html::getUniqueId('edit_' . $this->fieldDefinition
    ->getName() . '_wrapper');
  if (empty($element['#attributes'])) {
    $element['#attributes'] = [];
  }
  $element['#attributes'] = array_merge_recursive($element['#attributes'], [
    'data-widget-type' => $this
      ->getPluginId(),
    'id' => $id,
    'class' => [
      'geolocation-map-widget',
    ],
  ]);
  if (empty($element['#attached'])) {
    $element['#attached'] = [];
  }
  $element['#attached'] = BubbleableMetadata::mergeAttachments($element['#attached'], [
    'library' => [
      'geolocation/geolocation.widget.map',
    ],
    'drupalSettings' => [
      'geolocation' => [
        'widgetSettings' => [
          $id => [
            'autoClientLocationMarker' => $settings['auto_client_location_marker'] ? TRUE : FALSE,
            'cardinality' => $this->fieldDefinition
              ->getFieldStorageDefinition()
              ->getCardinality(),
            'fieldName' => $this->fieldDefinition
              ->getName(),
          ],
        ],
      ],
    ],
  ]);
  $element['map'] = [
    '#type' => 'geolocation_map',
    '#weight' => -10,
    '#settings' => $settings[static::$mapProviderSettingsFormId],
    '#id' => $id . '-map',
    '#maptype' => static::$mapProviderId,
    '#context' => [
      'widget' => $this,
    ],
  ];
  $element['map'] = $this->mapCenterManager
    ->alterMap($element['map'], $settings['centre']);
  if ($this
    ->getSetting('allow_override_map_settings') && !empty($items
    ->get(0)
    ->getValue()['data']['map_provider_settings'])) {
    $element['map']['#settings'] = $items
      ->get(0)
      ->getValue()['data']['map_provider_settings'];
  }
  $context = [
    'widget' => $this,
    'form_state' => $form_state,
    'field_definition' => $this->fieldDefinition,
  ];
  if (!$this
    ->isDefaultValueWidget($form_state)) {
    \Drupal::moduleHandler()
      ->alter('geolocation_field_map_widget', $element, $context);
  }
  return $element;
}