You are here

public function GeofieldMapWidget::settingsForm in Geofield Map 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldWidget/GeofieldMapWidget.php \Drupal\geofield_map\Plugin\Field\FieldWidget\GeofieldMapWidget::settingsForm()

Returns a form to configure settings for the widget.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the widget. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

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

Return value

array The form definition for the widget settings.

Overrides GeofieldLatLonWidget::settingsForm

File

src/Plugin/Field/FieldWidget/GeofieldMapWidget.php, line 293

Class

GeofieldMapWidget
Plugin implementation of the 'geofield_map' widget.

Namespace

Drupal\geofield_map\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $default_settings = self::defaultSettings();
  $elements = [];
  $gmap_settings_page_link = Url::fromRoute('geofield_map.settings', [], [
    'query' => [
      'destination' => Url::fromRoute('<current>')
        ->toString(),
    ],
  ]);

  // Attach Geofield Map Library.
  $elements['#attached']['library'] = [
    'geofield_map/geofield_map_general',
    'geofield_map/geofield_map_widget',
  ];
  $elements['#tree'] = TRUE;
  $elements['default_value'] = [
    'lat' => [
      '#type' => 'value',
      '#value' => $this
        ->getSetting('default_value')['lat'],
    ],
    'lon' => [
      '#type' => 'value',
      '#value' => $this
        ->getSetting('default_value')['lon'],
    ],
  ];
  $geocoder_module_link = $this->link
    ->generate('Geocoder Module', Url::fromUri('https://www.drupal.org/project/geocoder', [
    'attributes' => [
      'target' => 'blank',
    ],
  ]));

  // Set Map Geocoder Control Element, if the Geocoder Module exists,
  // otherwise output a tip on Geocoder Module Integration.
  $this
    ->setGeocoderMapControl($elements, $this
    ->getSettings());

  // Alter and tune the original 'map_geocoder' sub-settings.
  unset($elements['map_geocoder']['access_warning']);
  unset($elements['map_geocoder']['settings']['position']);
  unset($elements['map_geocoder']['settings']['input_size']);
  unset($elements['map_geocoder']['settings']['infowindow']);
  unset($elements['map_geocoder']['settings']['zoom']);
  $elements['map_geocoder']['control']['#description'] = $this
    ->t('This enables Geocoding capabilities throughout the @geocoder_module_link providers (and overtakes the Geocode & ReverseGeocode functionalities provided by the Google Maps Geocoder).', [
    '@geocoder_module_link' => $geocoder_module_link,
  ]);

  // Set Google Api Key Element.
  $elements['map_google_api_key'] = $this
    ->setMapGoogleApiKeyElement();
  $elements['map_google_places'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Google Places'),
    '#description' => $this
      ->t('Note: this works only with Gmap Api Key set in the @geofield_map_settings_link page. Cannot work with "@search_address_geocoder_option_title" enabled', [
      '@geofield_map_settings_link' => $this->link
        ->generate('Geofield Map Settings', $gmap_settings_page_link),
      '@search_address_geocoder_option_title' => $this
        ->getMapGeocoderTitle(),
    ]),
    '#states' => [
      'invisible' => [
        ':input[name="fields[field_geofield][settings_edit_form][settings][map_geocoder][control]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $elements['map_google_places']['places_control'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable Address Geocoding via the @google_places_link.', [
      '@google_places_link' => $this->link
        ->generate($this
        ->t('Google Maps Places Autocomplete Service'), Url::fromUri('https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete', [
        'absolute' => TRUE,
        'attributes' => [
          'target' => 'blank',
        ],
      ])),
    ]),
    '#default_value' => $this
      ->getSetting('map_google_places')['places_control'],
    '#return_value' => 1,
  ];
  $elements['map_google_places']['places_additional_options'] = [
    '#type' => 'textarea',
    '#rows' => 2,
    '#title' => $this
      ->t('Google Maps Places Autocomplete Service Additional Options'),
    '#description' => $this
      ->t('An object literal of additional options, that comply with the @autocomplete_class.<br><b>The placeholder values are the default ones used by the widget.</b><br>The syntax should respect the javascript object notation (json) format.<br>As suggested in the field placeholder, always use double quotes (") both for the indexes and the string values.', [
      "@autocomplete_class" => $this->link
        ->generate($this
        ->t('google.maps.places.Autocomplete class'), Url::fromUri('https://developers.google.com/maps/documentation/javascript/reference/3/#Autocomplete', [
        'absolute' => TRUE,
        'attributes' => [
          'target' => 'blank',
        ],
      ])),
    ]),
    '#default_value' => $this
      ->getSetting('map_google_places')['places_additional_options'],
    '#placeholder' => '{"fields": ["place_id"], "strictBounds": "false"}',
    '#element_validate' => [
      [
        get_class($this),
        'jsonValidate',
      ],
    ],
    '#states' => [
      'visible' => [
        ':input[name="fields[field_geofield][settings_edit_form][settings][map_google_places][places_control]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $elements['map_library'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Map Library'),
    '#default_value' => $this
      ->getSetting('map_library'),
    '#options' => [
      'gmap' => $this
        ->t('Google Maps'),
      'leaflet' => $this
        ->t('Leaflet js'),
    ],
  ];

  // Alter settings elements in case of Google Maps API Key missing.
  if (empty($this
    ->getGmapApiKey())) {
    $elements['map_google_places']['#description'] = $this
      ->t("<span class='geofield-map-warning'>Gmap Api Key missing - Google Places library not available.</span>");
    $elements['map_google_places']['places_control']['#disabled'] = TRUE;
    $elements['map_google_places']['places_control']['#default_value'] = FALSE;
    $elements['map_library']['#default_value'] = 'leaflet';
    $elements['map_library']['#description'] = $this
      ->t("<span class='geofield-map-warning'>Gmap Api Key missing - Google Maps Library not available.</span>");
    $elements['map_library']['#options'] = [
      'leaflet' => $this
        ->t('Leaflet js'),
    ];
  }
  $elements['map_type_google'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Map type'),
    '#default_value' => $this
      ->getSetting('map_type_google'),
    '#options' => $this->gMapTypesOptions,
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $this->fieldDefinition
          ->getName() . '][settings_edit_form][settings][map_library]"]' => [
          'value' => 'gmap',
        ],
      ],
    ],
  ];
  $elements['map_type_leaflet'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Map type'),
    '#default_value' => $this
      ->getSetting('map_type_leaflet'),
    '#options' => $this->leafletTileManager
      ->getLeafletTilesLayersOptions(),
    '#description' => $this->currentUser
      ->hasPermission('configure geofield_map') ? $this
      ->t('Choose one among all the Leaflet Tiles Plugins defined for the Geofield Map module (@see LeafletTileLayerPlugin).<br>You can add your one into your custom module as a new LeafletTileLayer Plugin. (Free Leaflet Tile Layers definitions are available from @free_leaflet_tiles_link).', [
      '@free_leaflet_tiles_link' => $this->link
        ->generate($this
        ->t('leaflet-extras/leaflet-providers'), Url::fromUri('http://leaflet-extras.github.io/leaflet-providers/preview/index.html', [
        'absolute' => TRUE,
        'attributes' => [
          'target' => 'blank',
        ],
      ])),
    ]) : '',
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $this->fieldDefinition
          ->getName() . '][settings_edit_form][settings][map_library]"]' => [
          'value' => 'leaflet',
        ],
      ],
    ],
  ];
  $elements['map_type_selector'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Provide a Map type Selector on the Map'),
    '#description' => $this
      ->t('If checked, the user will be able to change Map Type throughout the selector.'),
    '#default_value' => $this
      ->getSetting('map_type_selector'),
  ];
  $elements['map_dimensions'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Map Dimensions'),
  ];
  $elements['map_dimensions']['width'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Map width'),
    '#default_value' => $this
      ->getSetting('map_dimensions')['width'],
    '#size' => 25,
    '#maxlength' => 25,
    '#description' => $this
      ->t('The default width of a Google map, as a CSS length or percentage. Examples: <em>50px</em>, <em>5em</em>, <em>2.5in</em>, <em>95%</em>'),
    '#required' => TRUE,
  ];
  $elements['map_dimensions']['height'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Map height'),
    '#default_value' => $this
      ->getSetting('map_dimensions')['height'],
    '#size' => 25,
    '#maxlength' => 25,
    '#description' => $this
      ->t('The default height of a Google map, as a CSS length or percentage. Examples: <em>50px</em>, <em>5em</em>, <em>2.5in</em>, <em>95%</em>'),
    '#required' => TRUE,
  ];
  $elements['zoom'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Zoom Settings'),
  ];
  $elements['zoom']['start'] = [
    '#type' => 'number',
    '#min' => $this
      ->getSetting('zoom')['min'],
    '#max' => $this
      ->getSetting('zoom')['max'],
    '#title' => $this
      ->t('Start Zoom level'),
    '#description' => $this
      ->t('The initial Zoom level for an empty Geofield.'),
    '#default_value' => $this
      ->getSetting('zoom')['start'],
    '#element_validate' => [
      [
        get_class($this),
        'zoomLevelValidate',
      ],
    ],
  ];
  $elements['zoom']['focus'] = [
    '#type' => 'number',
    '#min' => $this
      ->getSetting('zoom')['min'],
    '#max' => $this
      ->getSetting('zoom')['max'],
    '#title' => $this
      ->t('Focus Zoom level'),
    '#description' => $this
      ->t('The Zoom level for an assigned Geofield or for Geocoding operations results.'),
    '#default_value' => $this
      ->getSetting('zoom')['focus'],
    '#element_validate' => [
      [
        get_class($this),
        'zoomLevelValidate',
      ],
    ],
  ];
  $elements['zoom']['min'] = [
    '#type' => 'number',
    '#min' => $default_settings['zoom']['min'],
    '#max' => $default_settings['zoom']['max'],
    '#title' => $this
      ->t('Min Zoom level'),
    '#description' => $this
      ->t('The Minimum Zoom level for the Map.'),
    '#default_value' => $this
      ->getSetting('zoom')['min'],
  ];
  $elements['zoom']['max'] = [
    '#type' => 'number',
    '#min' => $default_settings['zoom']['min'],
    '#max' => $default_settings['zoom']['max'],
    '#title' => $this
      ->t('Max Zoom level'),
    '#description' => $this
      ->t('The Maximum Zoom level for the Map.'),
    '#default_value' => $this
      ->getSetting('zoom')['max'],
    '#element_validate' => [
      [
        get_class($this),
        'maxZoomLevelValidate',
      ],
    ],
  ];
  $elements['click_to_find_marker'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Click to Find marker'),
    '#description' => $this
      ->t('Provides a button to recenter the map on the marker location.'),
    '#default_value' => $this
      ->getSetting('click_to_find_marker'),
  ];
  $elements['click_to_place_marker'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Click to place marker'),
    '#description' => $this
      ->t('Provides a button to place the marker in the center location.'),
    '#default_value' => $this
      ->getSetting('click_to_place_marker'),
  ];
  $elements['hide_coordinates'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Hide Lat/Lon Coordinates Input'),
    '#description' => $this
      ->t('Option to visually hide the coordinates input elements from the widget form.'),
    '#default_value' => $this
      ->getSetting('hide_coordinates'),
  ];
  $fields_list = array_merge_recursive($this->entityFieldManager
    ->getFieldMapByFieldType('string_long'), $this->entityFieldManager
    ->getFieldMapByFieldType('string'));
  $string_fields_options = [
    '0' => $this
      ->t('- Any -'),
  ];

  // Filter out the not acceptable values from the options.
  foreach ($fields_list[$form['#entity_type']] as $k => $field) {
    if (in_array($form['#bundle'], $field['bundles']) && !in_array($k, [
      'revision_log',
      'behavior_settings',
      'parent_id',
      'parent_type',
      'parent_field_name',
    ])) {
      $string_fields_options[$k] = $k;
    }
  }
  $elements['geoaddress_field'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Geoaddress-ed Field'),
    '#description' => $this
      ->t('If a not null Google Maps API Key is set, it is possible to choose the Entity Title, or a "string" type field (among the content type ones), to sync and populate with the Search / Reverse Geocoded Address.'),
    '#states' => [
      'invisible' => [
        ':input[name="fields[' . $this->fieldDefinition
          ->getName() . '][settings_edit_form][settings][map_google_api_key]"]' => [
          'value' => '',
        ],
      ],
    ],
  ];
  $elements['geoaddress_field']['field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Choose an existing field where to store the Searched / Reverse Geocoded Address'),
    '#description' => $this
      ->t('Choose among the title and the simple string fields (un-formatted text fields) of this bundle/entity type, if available.'),
    '#options' => $string_fields_options,
    '#default_value' => $this
      ->getSetting('geoaddress_field')['field'],
  ];
  $elements['geoaddress_field']['hidden'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('<strong>Hide</strong> this field in the Content Edit Form'),
    '#description' => $this
      ->t('If checked, the selected Geoaddress Field will be Hidden to the user in the edit form, </br>and totally managed by the Geofield Reverse Geocode'),
    '#default_value' => $this
      ->getSetting('geoaddress_field')['hidden'],
    '#states' => [
      'invisible' => [
        [
          ':input[name="fields[' . $this->fieldDefinition
            ->getName() . '][settings_edit_form][settings][geoaddress_field][field]"]' => [
            'value' => 'title',
          ],
        ],
        'or',
        [
          ':input[name="fields[' . $this->fieldDefinition
            ->getName() . '][settings_edit_form][settings][geoaddress_field][field]"]' => [
            'value' => '0',
          ],
        ],
      ],
    ],
  ];
  $elements['geoaddress_field']['disabled'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('<strong>Disable</strong> this field in the Content Edit Form'),
    '#description' => $this
      ->t('If checked, the selected Geoaddress Field will be Disabled to the user in the edit form, </br>and totally managed by the Geofield Reverse Geocode'),
    '#default_value' => $this
      ->getSetting('geoaddress_field')['disabled'],
    '#states' => [
      'invisible' => [
        [
          ':input[name="fields[' . $this->fieldDefinition
            ->getName() . '][settings_edit_form][settings][geoaddress_field][hidden]"]' => [
            'checked' => TRUE,
          ],
        ],
        'or',
        [
          ':input[name="fields[' . $this->fieldDefinition
            ->getName() . '][settings_edit_form][settings][geoaddress_field][field]"]' => [
            'value' => '0',
          ],
        ],
      ],
    ],
  ];
  return $elements + parent::settingsForm($form, $form_state);
}