You are here

function leaflet_apply_map_settings in Leaflet 7

Universal method for applying map settings from leaflet_form_elements items.

@array $map Leaflet map definition. @array $features Associative array of map features. @array $map_settings Map settings returned by a settings form. @string $entity_type Entity type this map is being displayed for.

2 calls to leaflet_apply_map_settings()
leaflet_field_formatter_view in ./leaflet.formatters.inc
Implements hook_field_formatter_view().
leaflet_views_plugin_style::render in leaflet_views/leaflet_views_plugin_style.inc
Renders view.

File

./leaflet.formatters.inc, line 353
Leaflet field formatter functions.

Code

function leaflet_apply_map_settings(&$map, &$features, $map_settings, $entity_type) {

  // These are the settings key that will be tokenized by entity:
  $token_by_entity = array(
    'popup',
    'icon',
    'vector_display',
  );

  // Apply zoom settings (will override hook_leaflet_map_info() values):
  if (isset($map_settings['zoom']['initialZoom']) && $map_settings['zoom']['initialZoom'] >= 0) {
    $map['settings']['zoom'] = intval($map_settings['zoom']['initialZoom']);
  }
  if (isset($map_settings['zoom']['minZoom']) && $map_settings['zoom']['minZoom'] >= 0) {
    $map['settings']['minZoom'] = intval($map_settings['zoom']['minZoom']);
  }
  if (isset($map_settings['zoom']['maxZoom']) && $map_settings['zoom']['maxZoom'] >= 0) {
    $map['settings']['maxZoom'] = intval($map_settings['zoom']['maxZoom']);
  }
  if (isset($map_settings['zoom']['scrollWheelZoom'])) {
    $map['settings']['scrollWheelZoom'] = $map_settings['zoom']['scrollWheelZoom'];
  }
  $icon = 'none';
  if (isset($map_settings['icon']['iconType'])) {
    $icon = $map_settings['icon']['iconType'];
    $classes = NULL;
    if ($icon == 'html' && isset($map_settings['icon']['htmlClass'])) {

      // Clean the user-entered classes.
      $classes = explode(' ', $map_settings['icon']['htmlClass']);
      array_walk($classes, 'drupal_html_class');
      $map_settings['icon']['htmlClass'] = implode(' ', $classes);
    }
    elseif (!$map_settings['icon']['iconUrl']) {
      $icon = 'none';
    }
  }
  $vector_settings = FALSE;
  if (!empty($map_settings['vector_display']) && $map_settings['vector_display']['stroke_override']) {
    $vector_settings = TRUE;
  }
  foreach ($features as &$feat) {

    // Apply tokens where relevant:
    $settings = $map_settings;
    $entity = !empty($feat['entity']) ? $feat['entity'] : NULL;
    $token_args = array(
      'data' => array(
        $entity_type => $entity,
      ),
      'options' => array(
        'clear' => TRUE,
      ),
    );
    foreach ($token_by_entity as $key) {
      if (isset($settings[$key]) && is_array($settings[$key])) {
        array_walk_recursive($settings[$key], 'leaflet_token_replace', $token_args);
      }
    }
    if (isset($settings['popup']['show']) && $settings['popup']['show']) {
      $feat['popup'] = trim($settings['popup']['text']);
    }

    // Apply Icon settings:
    if ($icon == 'html') {

      // Maps handled by Views will have rendered HTML already.
      if (isset($feat['rendered_html'])) {
        $feat['html'] = $feat['rendered_html'];
      }
      else {
        $icon_config = array(
          'label' => 'hidden',
          'settings' => array(
            'image_style' => isset($settings['icon']['iconImageStyle']) ? $settings['icon']['iconImageStyle'] : '',
          ),
        );
        $icon_field = field_view_field($entity_type, $feat['entity'], $settings['icon']['html'], $icon_config);
        $feat['html'] = render($icon_field);
      }
      if (!empty($settings['icon']['iconSize']['x']) || !empty($settings['icon']['iconSize']['y'])) {

        // We hand the size off to the API, but the rendered HTML will dictate
        // its own size, so we override that as well.
        // @todo: actually allow the API settings to control this.
        $feat['icon']['iconSize'] = $settings['icon']['iconSize'];
        if (!empty($icon_field)) {
          foreach (element_children($icon_field) as $i) {
            if (!empty($settings['icon']['iconSize']['x'])) {
              $icon_field[$i]['#item']['width'] = $settings['icon']['iconSize']['x'];
              unset($icon_field[$i]['#item']['width']);
            }
            if (!empty($settings['icon']['iconSize']['y'])) {
              $icon_field[$i]['#item']['height'] = $settings['icon']['iconSize']['y'];
              unset($icon_field[$i]['#item']['width']);
            }
          }
        }
      }
      $feat['html_class'] = $settings['icon']['htmlClass'];
      $feat['icon']['iconAnchor'] = $settings['icon']['iconAnchor'];
    }
    if ($icon == 'marker') {
      $feat['icon'] = $settings['icon'];
      unset($feat['icon']['html']);
      unset($feat['icon']['html_class']);
    }

    // Apply vector display settings:
    if ($vector_settings) {
      if ($feat['type'] != 'point') {

        // To avoid overwrite the options with empty values removes all
        // NULL, FALSE and empty Strings and leaves zero values.
        $options = array_filter($settings['vector_display'], 'strlen');
        unset($options['stroke_override']);
        $feat['options'] = $options;
      }
    }

    // Unset the entity here so it does not get returned to the client in JS.
    unset($feat['entity']);
  }
}