You are here

class GeofieldMap in Geofield Map 8

Same name and namespace in other branches
  1. 8.2 src/Element/GeofieldMap.php \Drupal\geofield_map\Element\GeofieldMap

Provides a Geofield Map form element.

Plugin annotation

@FormElement("geofield_map");

Hierarchy

Expanded class hierarchy of GeofieldMap

1 #type use of GeofieldMap
GeofieldMapWidget::formElement in src/Plugin/Field/FieldWidget/GeofieldMapWidget.php
Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().

File

src/Element/GeofieldMap.php, line 15

Namespace

Drupal\geofield_map\Element
View source
class GeofieldMap extends GeofieldElementBase {

  /**
   * {@inheritdoc}
   */
  public static $components = [
    'lat' => [
      'title' => 'Latitude',
      'range' => 90,
    ],
    'lon' => [
      'title' => 'Longitude',
      'range' => 180,
    ],
  ];

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#process' => [
        [
          $class,
          'latLonProcess',
        ],
      ],
      '#element_validate' => [
        [
          $class,
          'elementValidate',
        ],
      ],
      '#theme_wrappers' => [
        'fieldset',
      ],
    ];
  }

  /**
   * Generates the Geofield Map form element.
   *
   * @param array $element
   *   An associative array containing the properties and children of the
   *   element. Note that $element must be taken by reference here, so processed
   *   child elements are taken over into $form_state.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param array $complete_form
   *   The complete form structure.
   *
   * @return array
   *   The processed element.
   */
  public static function latLonProcess(array &$element, FormStateInterface $form_state, array &$complete_form) {

    // Conditionally use the Leaflet library from the D8 Module, if enabled.
    if ($element['#map_library'] == 'leaflet') {
      $element['#attached']['library'][] = \Drupal::moduleHandler()
        ->moduleExists('leaflet') ? 'leaflet/leaflet' : 'geofield_map/leaflet';
    }
    $mapid = 'map-' . $element['#id'];
    $element['map'] = [
      '#type' => 'fieldset',
      '#weight' => 0,
    ];
    if (strlen($element['#gmap_api_key']) > 0) {
      $element['map']['geocode'] = [
        '#prefix' => '<label>' . t("Geocode address") . '</label>',
        '#type' => 'textfield',
        '#description' => t("Use this to geocode your search location."),
        '#size' => 60,
        '#maxlength' => 128,
        '#attributes' => [
          'id' => 'search-' . $element['#id'],
          'class' => [
            'form-text',
            'form-autocomplete',
            'geofield-map-search',
          ],
        ],
      ];
      if (\Drupal::currentUser()
        ->hasPermission('configure geofield_map')) {
        $element['map']['geocode']['#description'] .= '<div class="geofield-map-message">' . t('@google_places_autocomplete_message<br>@message_recipient', [
          '@google_places_autocomplete_message' => !$element['#gmap_places'] ? 'Google Places Autocomplete Service disabled. Might be enabled in the Geofield Widget configuration.' : 'Google Places Autocomplete Service enabled.',
          '@message_recipient' => t('(This message is only shown to the Geofield Map module administrator).'),
        ]) . '</div>';
      }
    }
    elseif (\Drupal::currentUser()
      ->hasPermission('configure geofield_map')) {
      $element['map']['geocode_missing'] = [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#value' => t("Gmap Api Key missing | The Geocode & ReverseGeocode functionalities are not available. <br>@settings_page_link", [
          '@settings_page_link' => \Drupal::linkGenerator()
            ->generate(t('Set it in the Geofield Map Configuration Page'), Url::fromRoute('geofield_map.settings', [], [
            'query' => [
              'destination' => Url::fromRoute('<current>')
                ->toString(),
            ],
          ])),
        ]),
        '#attributes' => [
          'class' => [
            'geofield-map-message',
          ],
        ],
      ];
    }
    $element['map']['geofield_map'] = [
      '#theme' => 'geofield_google_map',
      '#mapid' => $mapid,
      '#width' => isset($element['#map_dimensions']['width']) ? $element['#map_dimensions']['width'] : '100%',
      '#height' => isset($element['#map_dimensions']['height']) ? $element['#map_dimensions']['height'] : '450px',
    ];
    $element['map']['actions'] = [
      '#type' => 'actions',
    ];
    if (!empty($element['#click_to_find_marker']) && $element['#click_to_find_marker'] == TRUE) {
      $element['map']['actions']['click_to_find_marker'] = [
        '#type' => 'button',
        '#value' => t('Find marker'),
        '#name' => 'geofield-map-center',
        '#attributes' => [
          'id' => $element['#id'] . '-click-to-find-marker',
        ],
      ];
      $element['#attributes']['class'] = [
        'geofield-map-center',
      ];
    }
    if (!empty($element['#click_to_place_marker']) && $element['#click_to_place_marker'] == TRUE) {
      $element['map']['actions']['click_to_place_marker'] = [
        '#type' => 'button',
        '#value' => t('Place marker here'),
        '#name' => 'geofield-map-marker',
        '#attributes' => [
          'id' => $element['#id'] . '-click-to-place-marker',
        ],
      ];
      $element['#attributes']['class'] = [
        'geofield-map-marker',
      ];
    }
    if (!empty($element['#geolocation']) && $element['#geolocation'] == TRUE) {
      $element['#attached']['library'][] = 'geofield_map/geolocation';
      $element['map']['actions']['geolocation'] = [
        '#type' => 'button',
        '#value' => t('Find my location'),
        '#name' => 'geofield-html5-geocode-button',
        '#attributes' => [
          'mapid' => $mapid,
        ],
      ];
      $element['#attributes']['class'] = [
        'auto-geocode',
      ];
    }
    static::elementProcess($element, $form_state, $complete_form);
    $element['lat']['#attributes']['id'] = 'lat-' . $element['#id'];
    $element['lon']['#attributes']['id'] = 'lon-' . $element['#id'];
    $address_field_exists = FALSE;

    // Geoaddress Field Settings (now limited to the first delta value)
    if ($element['#delta'] == 0) {
      if (!empty($element['#geoaddress_field']['field'])) {
        $address_field_name = $element['#geoaddress_field']['field'];
        $parents = array_slice($element['#array_parents'], 0, -4);
        $parents[] = $address_field_name;
        $address_field = NestedArray::getValue($complete_form, $parents, $address_field_exists);
        if ($address_field_exists) {
          $address_field['widget'][$element['#delta']]['value']['#description'] = (string) t('This value will be synchronized with the Geofield Map Reverse-Geocoded value.');
          if ($element['#geoaddress_field']['hidden']) {
            $address_field['#attributes']['class'][] = 'geofield_map_geoaddress_field_hidden';
          }
          if ($element['#geoaddress_field']['disabled']) {
            $address_field['widget'][$element['#delta']]['value']['#attributes']['readonly'] = 'readonly';
            $address_field['widget'][$element['#delta']]['value']['#description'] = (string) t('This field is readonly. It will be synchronized with the Geofield Map Reverse-Geocoded value.');
          }

          // Ensure the geoaddress_field has got an #id, otherwise generate it.
          if (!isset($address_field['widget'][$element['#delta']]['value']['#id'])) {
            $address_field['widget'][$element['#delta']]['value']['#id'] = $element['#geoaddress_field']['field'] . '-0';
          }
          NestedArray::setValue($complete_form, $parents, $address_field);
        }
      }
    }

    // Attach Geofield Map Libraries.
    $element['#attached']['library'][] = 'geofield_map/geofield_map_general';
    $element['#attached']['library'][] = 'geofield_map/geofield_map_widget';

    // The Entity Form.

    /* @var \Drupal\Core\Entity\ContentEntityFormInterface $entityForm */
    $entityForm = $form_state
      ->getBuildInfo()['callback_object'];
    $entity_operation = method_exists($entityForm, 'getOperation') ? $entityForm
      ->getOperation() : 'any';

    // Geofield Map Element specific mapid settings.
    $settings[$mapid] = [
      'entity_operation' => $entity_operation,
      'id' => $element['#id'],
      'gmap_api_key' => $element['#gmap_api_key'] && strlen($element['#gmap_api_key']) > 0 ? $element['#gmap_api_key'] : NULL,
      'name' => $element['#name'],
      'lat' => floatval($element['lat']['#default_value']),
      'lng' => floatval($element['lon']['#default_value']),
      'zoom_start' => intval($element['#zoom']['start']),
      'zoom_focus' => intval($element['#zoom']['focus']),
      'zoom_min' => intval($element['#zoom']['min']),
      'zoom_max' => intval($element['#zoom']['max']),
      'latid' => $element['lat']['#attributes']['id'],
      'lngid' => $element['lon']['#attributes']['id'],
      'searchid' => isset($element['map']['geocode']) ? $element['map']['geocode']['#attributes']['id'] : NULL,
      'geoaddress_field' => $address_field_exists ? $element['#geoaddress_field']['field'] : NULL,
      'geoaddress_field_id' => $address_field_exists ? $address_field['widget'][0]['value']['#id'] : NULL,
      'mapid' => $mapid,
      'widget' => TRUE,
      'gmap_places' => $element['#gmap_places'],
      'gmap_places_options' => $element['#gmap_places_options'],
      'map_library' => $element['#map_library'],
      'map_type' => $element['#map_type'],
      'map_type_selector' => $element['#map_type_selector'] ? TRUE : FALSE,
      'map_types_google' => $element['#map_types_google'],
      'map_types_leaflet' => $element['#map_types_leaflet'],
      'click_to_find_marker_id' => $element['#click_to_find_marker'] ? $element['map']['actions']['click_to_find_marker']['#attributes']['id'] : NULL,
      'click_to_find_marker' => $element['#click_to_find_marker'] ? TRUE : FALSE,
      'click_to_place_marker_id' => $element['#click_to_place_marker'] ? $element['map']['actions']['click_to_place_marker']['#attributes']['id'] : NULL,
      'click_to_place_marker' => $element['#click_to_place_marker'] ? TRUE : FALSE,
    ];

    // Geofield Map Element global settings.
    // If the gmap_api_key is defined, set it.
    if ($element['#gmap_api_key'] && strlen($element['#gmap_api_key']) > 0) {
      $settings['gmap_api_key'] = $element['#gmap_api_key'];
    }
    $element['#attached']['drupalSettings'] = [
      'geofield_map' => $settings,
    ];
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
FormElement::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElementInterface::valueCallback 15
GeofieldElementBase::elementProcess public static function Generates a Geofield generic component based form element.
GeofieldElementBase::elementValidate public static function Validates a Geofield generic component based form element.
GeofieldElementBase::getComponents public static function Components Getter. 2
GeofieldMap::$components public static property
GeofieldMap::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
GeofieldMap::latLonProcess public static function Generates the Geofield Map form element.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.