Problem/Motivation

Hi,

I was working this afternoon with Openlayers Styles and I wondered if images styles from Drupal core (Imagecache in D6) could be applied to Openlayers styles (Icons for markers).
Also in Views, it should be possible to use a image field as a marker for Openlayers, so I could leverage the power of image styles directly.

Proposed resolution

Add a new 'Openlayers' formatter and provide options.

Remaining tasks

Implementing Tokens in Openlayers formatter's form (in Views).

User interface changes

  • New 'Openlayers' formatter with image field in Views.
  • New Openlayers style form when you select the 'Openlayers' formatter.
  • New select box in the Style form with image styles as options.

API changes

No API changes at first sight.

Original report by Pol

Comments

pol’s picture

Here's a first working version of the patch, implementing the second solution, it adds a select box in the style form.

pol’s picture

StatusFileSize
new3.19 KB

New patch working flawlessly.

It also fix a problem in the styles add/edit form.

The form function openlayers_ui_styles_form is using a preview function to generate the preview style on the right.
This form element calls:
'#markup' => openlayers_ui_style_preview($preview_style, TRUE),

The function openlayers_ui_style_preview calls:

  // Render style data
  $style->data = openlayers_render_style($style->data);

The function openlayers_render_style modify the icon path and return the style array.

As $preview_style is passed by reference, the $style in the form function openlayers_ui_styles_form gets modified and if you used a relative path for the icon former, you'll be getting an absolute path after submit, which is not good.

I made a trick to copy the $preview_file into a new variable, even if you change it, it will not alter the original $style.

  // Calling this trick to avoid transformation of the path
  $preview_style = unserialize(serialize($preview_style));

Maybe there is a better method to do it, just let me know, I'm waiting for feedback.

pol’s picture

New version of the patch, allowing us to see the styles applied on the icon in the styles list.

zzolo’s picture

Component: OL Layers » OL Styles

Hey @Pol. Thanks for your work on this. I have some questions and reservations, though.

  1. Do image styles apply for images that are not managed by Drupal?
  2. What's the use case exactly? I guess I don't see the issue with getting the URL directly and putting that in the field. There are no dynamic aspects of this.
  3. This needs to be solely a UI level feature, if it does happen. The API for styles is already too hackish. A URL is all we want in the API.

On a similar note, it would be really cool to get the URL of an image field in Views and use that as the value (obviously supporting Styles). The issue with this is that the Views integration with Image field does not provide a URL.

Let me know what you think.

pol’s picture

Hi @zzolo,

  1. Do image styles apply for images that are not managed by Drupal?
    • What do you mean ? You can apply styles to any images, as long as you provide a valid path, it's ok.
  2. What's the use case exactly? I guess I don't see the issue with getting the URL directly and putting that in the field. There are no dynamic aspects of this.
    • The idea came into my mind like that: I was using Openlayers Styles Icons to add some nice icons to my maps and they were not the same size, so I had the idea to apply a style on it to get them all in the same size, without having the hassle to do it manually.
      There is no dynamic but it's a cool feature, knowing that if you want to have your style in different style, now you can do it.
  3. This needs to be solely a UI level feature, if it does happen. The API for styles is already too hackish. A URL is all we want in the API.
    • Too hackish ? It's been 1 month that I use D7 for production sites and I never had the chance to work with the image API and I have to say that it's easy and clear. You can do what you want with 3 functions :-).
      You can get url with image_style_url or a path with image_style_path.

On a similar note, it would be really cool to get the URL of an image field in Views and use that as the value (obviously supporting Styles). The issue with this is that the Views integration with Image field does not provide a URL.
That is also something to do and probably my next thing to do 'in the train' :-)
Do you already have some ideas, paths to follow to do it ? Anything is welcome.

On the other hand, have you checked the problem described in comment #2 ? That's something to fix for next release...

Thanks !

nod_’s picture

To avoid serialize/unserialize, you could use clone depending on what the variable is.
and zzolo was talking about openlayers' style API, not Drupal's image one.

pol’s picture

Hi @nod_,

I tried with clone and it doesn't works, that's why I used unserialize/serialize.

pol’s picture

Hello,

I talked with @zzolo and he gave me a couple of advice for this patch:

  1. Do not let Openlayers Core function to handle the modification of the path.
  2. Keep the modification of the path of the file in the UI side.

And that's what I did.
The comments #2, #6 and #7 are now obsolete.

pol’s picture

Status: Needs review » Active
StatusFileSize
new8.02 KB

Hello,

I made a new patch fixing the problem that @zzolo talked in comment #4:

On a similar note, it would be really cool to get the URL of an image field in Views and use that as the value (obviously supporting Styles). The issue with this is that the Views integration with Image field does not provide a URL.

This is a first working patch for a Views Integration for styles.

Test it and let me know what to improve.
I will add support for token soon.

How to use it ?

So, in a few words, now when you add a Openlayers Data Overlay, you are able to map a Style field.
The Style field must be an image (obviously), it means that a image field should exists in the fields list.
You also have to choose the 'Openlayer' formatter for images.
There you will be able to configure the style as it was added manually in Openlayers.
Dont forget, If you map a Style field, the style settings in the edit map form will be overriden and useless.

dasjo’s picture

Status: Active » Needs review
pol’s picture

Status: Active » Needs review
StatusFileSize
new6.51 KB

Updated patch for 7.x-2.x.

pol’s picture

It looks like I forgot an important patch in the last patch, I fixed it with this.

This patch has been tested and showed in a presentation, so, I know it's working :-)

pol’s picture

I added a new feature to the patch.

By default, if no 'graphicWidth' and 'graphicHeight' are set, it will use 12 (pixels) for each.

It would be cool to set automatically those to the width and height from the generated image.

This is done in that part of the patch:

  /**
   * Find and compute styling attributes.
   */
  function get_style($feature, $handlers, $datasource) {

    $style = array();

    if (isset($datasource['style_field']) && !empty($datasource['style_field'])) {
      $style_field = $handlers[$datasource['style_field']];
      $style = $style_field->options['settings'];

      // Compute the size of the picture.
      // If no size found, Openlayers will use 12x12 by default.
      // This is a newer way to get the raw value of a field, only in view-dev, let's not use it now.
      // But keep it for later.
      // $raw_value = $this->view->style_plugin->get_field_value($this->view->row_index, $datasource['style_field']);
      $raw_value = $this->view->field[$datasource['style_field']]->get_value($this->view->result[$this->view->row_index]);
      if (isset($raw_value[0]['uri'])) {
        $preview_file = image_style_path($style['imageStyle'], $raw_value[0]['uri']);
        $image_info = image_get_info($preview_file);
        $style['graphicWidth'] = $image_info['width'];
        $style['graphicHeight'] = $image_info['height'];
      }

      unset($style['imageStyle']);
      $style['externalGraphic'] = $style_field->original_value;

      foreach ($style as $prop => $value) {
        if (empty($value)) {
          unset($style[$prop]);
        }
      }
    }

    return $style;
  }
pol’s picture

Is there someone available to review this patch ?

zzolo’s picture

Version: 7.x-2.x-dev » 7.x-3.x-dev

Anyone else tested this yet? Also, moving to 3.x; seems like too big of a feature to add now to 2.x

pol’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Update issue.