diff --git a/leaflet.formatters.inc b/leaflet.formatters.inc index 821250f..588b7c1 100644 --- a/leaflet.formatters.inc +++ b/leaflet.formatters.inc @@ -389,35 +389,42 @@ function leaflet_apply_map_settings(&$map, &$features, $map_settings, $entity_ty $feat['popup'] = $settings['popup']['text']; } - // Apply Icon settings: - if ($icon == 'html') { - $icon_config = array( - 'label' => 'hidden', - 'settings' => array( - 'image_style' => isset($settings['icon']['iconImageStyle']) ? $settings['icon']['iconImageStyle'] : '', - ), + // Applying icon settings: html marker from image field + if ($icon == 'html' && isset($feat['icon_object'])) { + + // Obtaining basic information to render image html. + $icon_variables = array( + 'path' => $feat['icon_object']['uri'], + 'style_name' => isset($settings['icon']['iconImageStyle']) ? $settings['icon']['iconImageStyle'] : '', ); - $icon_field = field_view_field($entity_type, $feat['entity'], $settings['icon']['html'], $icon_config); + + // Put height and width from settings page into the array to get rendered as + // html attributes in the img tag 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']; - 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']); - } + + $icon_variables['attributes'] = array(); + if (!empty($settings['icon']['iconSize']['x'])) { + $icon_variables['attributes']['width'] = $settings['icon']['iconSize']['x']; + } + if (!empty($settings['icon']['iconSize']['y'])) { + $icon_variables['attributes']['height'] = $settings['icon']['iconSize']['y']; } } - $feat['html'] = render($icon_field); + + // Render the image field. + $feat['html'] = theme('image_style', $icon_variables); $feat['html_class'] = $settings['icon']['htmlClass']; $feat['icon']['iconAnchor'] = $settings['icon']['iconAnchor']; + + // Clean up temporary information + unset($feat['icon_object']); } + + // Applying icon settings: for markers. if ($icon == 'marker') { $feat['icon'] = $settings['icon']; unset($feat['icon']['html']); @@ -631,6 +638,7 @@ function leaflet_form_elements($group, $settings, $options = NULL) { '#description' => "It is usually preferable to leave this blank and use the image style system to size the icons. Sizes are in pixels.", '#type' => 'fieldset', '#collapsible' => TRUE, + '#collapsed' => TRUE, ); $form_element['iconSize']['x'] = array( '#title' => t('Width'), @@ -652,6 +660,7 @@ function leaflet_form_elements($group, $settings, $options = NULL) { '#title' => t('Icon Anchor'), '#type' => 'fieldset', '#collapsible' => TRUE, + '#collapsed' => TRUE, '#description' => t("The coordinates of the 'tip' of the icon (relative to its top left corner). The icon will be aligned so that this point is at the marker's geographical location."), ); $form_element['iconAnchor']['x'] = array( diff --git a/leaflet_views/leaflet_views_plugin_style.inc b/leaflet_views/leaflet_views_plugin_style.inc index 2678ad1..d0ef91c 100644 --- a/leaflet_views/leaflet_views_plugin_style.inc +++ b/leaflet_views/leaflet_views_plugin_style.inc @@ -93,14 +93,23 @@ class leaflet_views_plugin_style extends views_plugin_style { // Get list of fields in this view & flag available geodata fields: $handlers = $this->display->handler->get_handlers('field'); + // Initialize and prepare data about available fields in this view. $fields = array(); $fields_data = array(); + $fields_image = array(); foreach ($handlers as $field_id => $handler) { + + // Adding fields to special array. $fields[$field_id] = $handler->ui_name(); + // Adding geo data fields to dedicated array. if (!empty($handler->field_info['type']) && $handler->field_info['type'] == 'geofield') { $fields_data[$field_id] = $handler->ui_name(); } + // Adding image fields to dedicated array. + if (!empty($handler->field_info['type']) && $handler->field_info['type'] == 'image') { + $fields_image[$field_id] = $handler->ui_name(); + } } // Check whether we have a geofield we can work with: @@ -207,7 +216,8 @@ class leaflet_views_plugin_style extends views_plugin_style { ); $form['zoom'] = leaflet_form_elements('zoom', $this->options); - $form['icon'] = leaflet_form_elements('icon', $this->options, array('path' => 'style_options', 'fields' => $fields)); + $form['icon'] = leaflet_form_elements('icon', $this->options, array('path' => 'style_options', 'fields' => $fields_image)); + $form['markers'] = leaflet_form_elements('markers', $this->options, array('path' => 'style_options', 'fields' => $fields)); $form['vector_display'] = leaflet_form_elements('vector_display', $this->options, array('path' => 'style_options')); $form['tokens'] = leaflet_form_elements('tokens', $this->options, array('weight' => 998, 'entity_type' => $this->entity_type)); } @@ -225,37 +235,59 @@ class leaflet_views_plugin_style extends views_plugin_style { * Renders view. */ function render() { + if (!empty($this->view->live_preview)) { return t('No preview available'); } $data = array(); $map = leaflet_map_get_info($this->options['map']); - // Is there a geofield selected? + + // Only proceed if a geofield has been selected. if ($this->options['data_source']) { + + // Render fields $this->render_fields($this->view->result); + foreach ($this->view->result as $id => $result) { - $geofield = $this->get_field_value($id, $this->options['data_source']); + // Obtain geo information + $geofield = $this->get_field_value($id, $this->options['data_source']); if (!empty($geofield)) { $entity = FALSE; + // Render the entity with the selected view mode: if ($this->options['description_field'] === '#rendered_entity' && is_object($result)) { $entity = entity_load_single($this->entity_type, $result->{$this->entity_info['entity keys']['id']}); $build = entity_view($this->entity_type, array($entity), $this->options['view_mode']); $description = drupal_render($build); } + // Normal rendering via fields: elseif ($this->options['description_field']) { $description = $this->rendered_fields[$id][$this->options['description_field']]; } + // Obtain coordinates as needed by leaflet. $points = leaflet_process_geofield($geofield); + // Attach pop-ups if we have rendered into $description: if (isset($description)) { foreach ($points as &$point) { $point['popup'] = $description; } } + + // Adding icon information to the points array when the point icon is set to field (html DivIcon). + if ($this->options['icon']['iconType'] == 'html') { + $icon_full_name = 'field_' . $this->options['icon']['html']; + $icon_object = $this->view->result[$id]->{$icon_full_name}; + if (!empty($icon_object)) { + foreach ($points as &$point) { + $point['icon_object'] = $icon_object[0]['raw']; + } + } + } + // Attach also titles & entities, they might be used later on. if ($this->options['name_field']) { foreach ($points as &$point) { @@ -265,6 +297,7 @@ class leaflet_views_plugin_style extends views_plugin_style { } } } + // Let modules modify the points data. drupal_alter('leaflet_views_alter_points_data', $result, $points); // Merge these points into the $data array for map rendering: