You are here

class geofield_ymap_plugin_style_geofield_ymap in Geofield Yandex Maps 7

Hierarchy

Expanded class hierarchy of geofield_ymap_plugin_style_geofield_ymap

1 string reference to 'geofield_ymap_plugin_style_geofield_ymap'
geofield_ymap_views_plugins in ./geofield_ymap.views.inc
Implementation of hook_views_plugins().

File

./geofield_ymap_plugin_style_geofield_ymap.inc, line 3

View source
class geofield_ymap_plugin_style_geofield_ymap extends views_plugin_style {

  /**
   * Set default options
   */
  function option_definition() {
    $options = parent::option_definition();
    $options['map_type'] = array(
      'default' => 'yandex#map',
    );
    $options['map_center'] = array(
      'default' => '0,0',
    );
    $options['map_zoom'] = array(
      'default' => 10,
    );
    $options['map_auto_centering'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    $options['map_auto_zooming'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    $options['map_clusterize'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    $options['map_hide_empty'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    $options['map_save_state'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    $options['map_object_preset'] = array(
      'default' => '',
    );
    $options['map_controls'] = array(
      'default' => 'default',
    );
    $options['map_behaviors'] = array(
      'default' => 'default',
    );
    $options['hint_content_field'] = array(
      'default' => '',
    );
    $options['icon_content_field'] = array(
      'default' => '',
    );
    $options['cluster_caption_field'] = array(
      'default' => '',
    );
    $options['preset_field'] = array(
      'default' => '',
    );
    $options['additional_settings']['object_options'] = array(
      'default' => '',
    );
    $options['additional_settings']['object_options_use_tokens'] = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    return $options;
  }

  /**
   * Options form.
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $field_options = array(
      '' => t('< none >'),
    );
    $fields = $this->display->handler
      ->get_handlers('field');
    foreach ($fields as $id => $handler) {
      $field_options[$id] = $handler
        ->ui_name(FALSE);
    }
    $form['map_type'] = array(
      '#type' => 'select',
      '#title' => t('Map type'),
      '#options' => _geofield_ymap_get_map_types(),
      '#default_value' => $this->options['map_type'],
    );
    $form['map_center'] = array(
      '#type' => 'textfield',
      '#title' => t('Map center'),
      '#default_value' => $this->options['map_center'],
      '#size' => 40,
    );
    $form['map_zoom'] = array(
      '#type' => 'textfield',
      '#title' => t('Map zoom'),
      '#default_value' => $this->options['map_zoom'],
      '#size' => 5,
    );
    $form['map_auto_centering'] = array(
      '#type' => 'checkbox',
      '#title' => t('Map auto centering'),
      '#default_value' => $this->options['map_auto_centering'],
    );
    $form['map_auto_zooming'] = array(
      '#type' => 'checkbox',
      '#title' => t('Map auto zooming'),
      '#default_value' => $this->options['map_auto_zooming'],
    );
    $form['map_clusterize'] = array(
      '#type' => 'checkbox',
      '#title' => t('Clusterize placemarks'),
      '#default_value' => $this->options['map_clusterize'],
    );
    $form['map_hide_empty'] = array(
      '#type' => 'checkbox',
      '#title' => t('Hide empty map'),
      '#default_value' => $this->options['map_hide_empty'],
    );
    $form['map_save_state'] = array(
      '#type' => 'checkbox',
      '#title' => t('Save user zoom and center'),
      '#default_value' => $this->options['map_save_state'],
    );
    $form['hint_content_field'] = array(
      '#type' => 'select',
      '#title' => t('Hint content field'),
      '#options' => $field_options,
      '#default_value' => $this->options['hint_content_field'],
    );
    $form['icon_content_field'] = array(
      '#type' => 'select',
      '#title' => t('Icon content field'),
      '#options' => $field_options,
      '#default_value' => $this->options['icon_content_field'],
    );
    $form['cluster_caption_field'] = array(
      '#type' => 'select',
      '#title' => t('Cluster caption field'),
      '#options' => $field_options,
      '#default_value' => $this->options['cluster_caption_field'],
    );
    $form['preset_field'] = array(
      '#type' => 'select',
      '#title' => t('Preset field'),
      '#description' => t('Use next modules for replace field value to preset name: !modules', array(
        '!modules' => '
          <a href="https://drupal.org/sandbox/xandeadx/2205151" target="_blank">Views field replace value</a>,
          <a href="https://drupal.org/project/views_regex_rewrite" target="_blank">Views Regex Rewrite</a>,
          <a href="https://drupal.org/project/views_fieldrewrite" target="_blank">Views Field Rewrite</a>
        ',
      )),
      '#options' => $field_options,
      '#default_value' => $this->options['preset_field'],
    );
    $form['map_object_preset'] = array(
      '#type' => 'textfield',
      '#title' => t('Default objects preset'),
      '#description' => t('Default <a href="@url" target="_blank">preset name</a>. Example: <code>islands#blueCircleDotIcon</code>', array(
        '@url' => 'http://api.yandex.ru/maps/doc/jsapi/2.1/ref/reference/option.presetStorage.xml',
      )),
      '#default_value' => $this->options['map_object_preset'],
      '#size' => 40,
    );
    $form['map_controls'] = array(
      '#type' => 'textfield',
      '#title' => t('Controls'),
      '#description' => t('<a href="@url" target="_blank">Controls</a> through a comma, or controls set name. Use <code>&lt;none&gt;</code> to hide all controls. Example: <code>fullscreenControl,searchControl</code>. Default set name: <code>default</code>', array(
        '@url' => 'http://api.yandex.ru/maps/doc/jsapi/2.1/ref/reference/control.Manager.xml#add',
      )),
      '#default_value' => $this->options['map_controls'],
      '#size' => 40,
    );
    $form['map_behaviors'] = array(
      '#type' => 'textfield',
      '#title' => t('Behaviors'),
      '#description' => t('<a href="@url" target="_blank">Map behaviors</a> through a comma. Use <code>&lt;none&gt;</code> to disable all behaviors. Default value: <code>default</code>', array(
        '@url' => 'http://api.yandex.ru/maps/doc/jsapi/2.1/ref/reference/map.behavior.Manager.xml#param-behaviors',
      )),
      '#default_value' => $this->options['map_behaviors'],
      '#size' => 40,
    );
    $form['additional_settings'] = array(
      '#title' => t('Additional settings'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['additional_settings']['object_options'] = array(
      '#title' => t('Object options'),
      '#description' => t('Additional object options in JSON format. <a href="!url" target="_blank">Options list</a>. Example: !example', array(
        '!url' => 'http://api.yandex.ru/maps/doc/jsapi/2.1/ref/reference/GeoObject.xml#constructor-summary',
        '!example' => '
          <code><br />
            {<br />
            &nbsp;&nbsp;"iconLayout": "default#image",<br />
            &nbsp;&nbsp;"iconImageHref": "http://api.yandex.ru/maps/doc/jsapi/2.x/examples/images/myIcon.gif",<br />
            &nbsp;&nbsp;"iconImageSize": [30, 42],<br />
            &nbsp;&nbsp;"iconImageOffset": [-3, -42]<br />
            }
          </code>
        ',
      )),
      '#type' => 'textarea',
      '#default_value' => $this->options['additional_settings']['object_options'],
    );
    $form['additional_settings']['object_options_use_tokens'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use replacement tokens (reduce performance)'),
      '#default_value' => $this->options['additional_settings']['object_options_use_tokens'],
    );
  }

  /**
   * Render the display in this style.
   */
  function render() {
    $geofield = '';
    foreach ($this->view->field as $field_name => $field_handler) {
      if (isset($field_handler->real_field) && preg_match('#_geom$#', $field_handler->real_field)) {
        $geofield = $field_name;
        break;
      }
    }
    if (!$geofield) {
      return t('Add geofield to fields list.');
    }
    $this->view->field[$geofield]->options['exclude'] = TRUE;
    $this
      ->render_fields($this->view->result);
    if ($this->view->result) {
      geophp_load();
    }
    elseif ($this->options['map_hide_empty']) {
      return NULL;
    }
    $features = array();

    // Additional object options without tokens
    $object_options = array();
    if ($this->options['additional_settings']['object_options'] && !$this->options['additional_settings']['object_options_use_tokens']) {
      $object_options = json_decode($this->options['additional_settings']['object_options'], TRUE);
    }
    foreach ($this->view->result as $row_index => $row) {
      $this->view->row_index = $row_index;
      $geofield_value = $this->view->field[$geofield]
        ->get_value($row);
      if (!$geofield_value) {
        continue;
      }
      $baloon_content = trim($this->row_plugin
        ->render($row));
      $hint_content = $this->options['hint_content_field'] ? trim($this
        ->get_field($row_index, $this->options['hint_content_field'])) : '';
      $icon_content = $this->options['icon_content_field'] ? trim($this
        ->get_field($row_index, $this->options['icon_content_field'])) : '';
      $cluster_caption = $this->options['cluster_caption_field'] ? trim($this
        ->get_field($row_index, $this->options['cluster_caption_field'])) : '';
      $preset = $this->options['preset_field'] ? trim($this
        ->get_field($row_index, $this->options['preset_field'])) : '';

      // Additional object options with tokens
      if ($this->options['additional_settings']['object_options'] && $this->options['additional_settings']['object_options_use_tokens']) {
        $object_options = $this
          ->safe_tokenize_value($this->options['additional_settings']['object_options'], $row_index);
        $object_options = json_decode($object_options, TRUE);
      }
      $geoms = array();

      // For views_handler_field_field
      if (is_array($geofield_value)) {
        foreach ($geofield_value as $value) {
          $geom = geoPHP::load($value['geom']);
          $geoms = array_merge($geoms, geofield_ymap_split_objects($geom));
        }
      }
      else {
        $geom = geoPHP::load($geofield_value, 'wkb');
        $geoms = array_merge($geoms, geofield_ymap_split_objects($geom));
      }
      foreach ($geoms as $geom) {
        $feature = array(
          'type' => 'Feature',
          'geometry' => $geom
            ->out('json', TRUE),
        );
        if ($baloon_content) {
          $feature['properties']['balloonContent'] = $baloon_content;
        }
        if ($hint_content) {
          $feature['properties']['hintContent'] = $hint_content;
        }
        if ($icon_content) {
          $feature['properties']['iconContent'] = $icon_content;
          $feature['options']['preset'] = 'islands#blueStretchyIcon';
        }
        if ($cluster_caption) {
          $feature['properties']['clusterCaption'] = $cluster_caption;
        }
        if ($preset) {
          $feature['options']['preset'] = $preset;
        }
        if ($object_options) {
          $feature_options = isset($feature['options']) ? $feature['options'] : array();
          $feature['options'] = $object_options + $feature_options;
        }
        $features[] = $feature;
      }
    }
    unset($this->view->row_index);
    return array(
      '#theme' => 'geofield_ymap',
      '#map_objects' => $features ? array(
        'type' => 'FeatureCollection',
        'features' => $features,
      ) : array(),
      '#map_type' => $this->options['map_type'],
      '#map_center' => $this->options['map_center'],
      '#map_zoom' => $this->options['map_zoom'],
      '#map_auto_centering' => $this->options['map_auto_centering'],
      '#map_auto_zooming' => $this->options['map_auto_zooming'],
      '#map_clusterize' => $this->options['map_clusterize'],
      '#map_object_preset' => $this->options['map_object_preset'],
      '#map_controls' => $this->options['map_controls'],
      '#map_behaviors' => $this->options['map_behaviors'],
      '#map_save_state' => $this->options['map_save_state'],
      '#id' => drupal_html_id('geofield-ymap-' . $this->view->name . '-' . $this->view->current_display),
    );
  }

  /**
   * Take a value and apply token replacement logic to it.
   */
  function safe_tokenize_value($value, $row_index) {
    if (strpos($value, '[') !== FALSE || strpos($value, '!') !== FALSE || strpos($value, '%') !== FALSE) {
      $tokens = isset($this->row_tokens[$row_index]) ? $this->row_tokens[$row_index] : array();
      if (!empty($this->view->build_info['substitutions'])) {
        $tokens += $this->view->build_info['substitutions'];
      }
      if ($tokens) {
        foreach ($tokens as &$token) {
          $token = drupal_substr(json_encode($token), 1, -1);
        }
        $value = strtr($value, $tokens);
      }
    }
    return $value;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
geofield_ymap_plugin_style_geofield_ymap::options_form function Options form. Overrides views_plugin_style::options_form
geofield_ymap_plugin_style_geofield_ymap::option_definition function Set default options Overrides views_plugin_style::option_definition
geofield_ymap_plugin_style_geofield_ymap::render function Render the display in this style. Overrides views_plugin_style::render
geofield_ymap_plugin_style_geofield_ymap::safe_tokenize_value function Take a value and apply token replacement logic to it.
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::additional_theme_functions public function Provide a list of additional theme functions for the theme info page.
views_plugin::options_submit public function Handle any special handling on the validate form. 9
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::summary_title public function Returns the summary of the settings in the display. 8
views_plugin::theme_functions public function Provide a full list of possible theme templates used by this style.
views_plugin_style::$row_plugin public property The row plugin, if it's initialized and the style itself supports it.
views_plugin_style::$row_tokens public property Store all available tokens row rows.
views_plugin_style::build_sort public function Called by the view builder to see if this style handler wants to interfere with the sorts. If so it should build; if it returns any non-TRUE value, normal sorting will NOT be added to the query. 1
views_plugin_style::build_sort_post public function Called by the view builder to let the style build a second set of sorts that will come after any other sorts in the view. 1
views_plugin_style::destroy public function Destructor. Overrides views_object::destroy
views_plugin_style::even_empty public function Should the output of the style plugin be rendered even if it's empty. 1
views_plugin_style::get_field public function Get a rendered field.
views_plugin_style::get_field_value public function Get the raw field value.
views_plugin_style::get_row_class public function Return the token replaced row class for the specified row.
views_plugin_style::init public function Initialize a style plugin.
views_plugin_style::options_validate public function Validate the options form. Overrides views_plugin::options_validate
views_plugin_style::pre_render public function Allow the style to do stuff before each row is rendered.
views_plugin_style::query public function Add anything to the query that we might need to. Overrides views_plugin::query 2
views_plugin_style::render_fields public function Render all of the fields for a given style and store them on the object.
views_plugin_style::render_grouping public function Group records as needed for rendering.
views_plugin_style::render_grouping_sets public function Render the grouping sets.
views_plugin_style::tokenize_value public function Take a value and apply token replacement logic to it.
views_plugin_style::uses_fields public function Return TRUE if this style also uses fields.
views_plugin_style::uses_row_class public function Return TRUE if this style also uses a row plugin.
views_plugin_style::uses_row_plugin public function Return TRUE if this style also uses a row plugin.
views_plugin_style::uses_tokens public function Return TRUE if this style uses tokens.
views_plugin_style::validate public function Validate that the plugin is correct and can be saved. Overrides views_plugin::validate