You are here

class yamaps_views_plugin_style_default_map in Yandex.Maps 7

Class defines Yandex Maps style plugin handler.

Hierarchy

Expanded class hierarchy of yamaps_views_plugin_style_default_map

1 string reference to 'yamaps_views_plugin_style_default_map'
yamaps_views_views_plugins in modules/yamaps_views/yamaps_views.views.inc
Implements hook_views_plugins().

File

modules/yamaps_views/handlers/yamaps_views_plugin_style_default_map.inc, line 11
Class yamaps_views_plugin_style_default_map.

View source
class yamaps_views_plugin_style_default_map extends views_plugin_style {
  const PLACEMARK_TITLE = 'iconContent';
  const PLACEMARK_BALLON_HEADER = 'balloonContentHeader';
  const PLACEMARK_BALLON_BODY = 'balloonContentBody';
  const PLACEMARK_DEFAULT_FIELD = '<default>';
  const PLACEMARK_NONE_FIELD = '<none>';

  /**
   * Returns default settings for the map.
   *
   * @return array
   *   Default values.
   */
  public function option_definition() {
    $options = parent::option_definition();
    $options['yandex_map_field'] = [
      'default' => '',
    ];
    $options['placemarks'] = [
      'default' => '',
    ];
    $options['lines'] = [
      'default' => '',
    ];
    $options['polygons'] = [
      'default' => '',
    ];
    $options['routes'] = [
      'default' => '',
    ];
    $options['yamaps_center_options'] = [
      'default' => [
        'map_center_type' => 'geolocation',
        'map_center_geolocation' => '',
        'zoom' => 6,
        'map_container' => [
          'coords' => '',
        ],
      ],
    ];
    $options['placemark_title'] = [
      'default' => self::PLACEMARK_DEFAULT_FIELD,
    ];
    $options['baloon_title'] = [
      'default' => self::PLACEMARK_DEFAULT_FIELD,
    ];
    $options['baloon_body'] = [
      'default' => self::PLACEMARK_DEFAULT_FIELD,
    ];
    $options['type'] = [
      'default' => 'yandex#map',
    ];
    $options['map_center'] = [
      'default' => '',
    ];
    $options['map_grouping_cat'] = [
      'default' => 'standart',
    ];
    return $options;
  }

  /**
   * Returns Yandex Maps views settings form.
   *
   * @param array $form
   *   Form structure array.
   * @param array $form_state
   *   Form state array.
   */
  public function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $fields = $this
      ->getFields();
    $yandex_fields = $this
      ->getYandexMapsFields();
    $form['yandex_map_field'] = [
      '#title' => t('Yandex Map Field'),
      '#description' => t('Choose Yandex Maps field. Add if views fields this field for the first.'),
      '#type' => 'select',
      '#options' => $yandex_fields,
      '#required' => TRUE,
      '#default_value' => $this->options['yandex_map_field'],
    ];
    $form['yamaps_center_options'] = [
      '#type' => 'fieldset',
      '#title' => t('Map center'),
      '#states' => [
        'invisible' => [
          ':input[name="style_options[yandex_map_field]"]' => [
            'value' => '',
          ],
        ],
      ],
    ];
    $form['yamaps_center_options']['map_center_type'] = [
      '#type' => 'radios',
      '#title' => t('Choose map center type'),
      '#options' => [
        'geolocation' => t('Geolocation.'),
        'mini_map' => t('Choose on map.'),
      ],
      '#default_value' => $this->options['yamaps_center_options']['map_center_type'],
      '#required' => FALSE,
      '#description' => t('Type of map displaying.'),
    ];
    $form['yamaps_center_options']['map_center_geolocation'] = [
      '#title' => t('Map center geolocation'),
      '#description' => t('Please enter place on whitch map will be centered.'),
      '#type' => 'textfield',
      '#default_value' => $this->options['yamaps_center_options']['map_center_geolocation'],
      '#size' => 40,
      '#states' => [
        'visible' => [
          ':input[name="style_options[yamaps_center_options][map_center_type]"]' => [
            'value' => 'geolocation',
          ],
        ],
      ],
    ];
    $form['yamaps_center_options']['zoom'] = [
      '#title' => t('Zoom'),
      '#type' => 'select',
      '#description' => t('Zoom of map'),
      '#options' => range(1, 15),
      '#states' => [
        'visible' => [
          ':input[name="style_options[yamaps_center_options][map_center_type]"]' => [
            'value' => 'geolocation',
          ],
        ],
      ],
      '#default_value' => $this->options['yamaps_center_options']['zoom'],
    ];
    $this->options['coords'] = $this
      ->getCoordinates();
    $prepared_params = yamaps_format_values_to_js($this->options);

    // Map initialization parameters.
    $map = [
      'init' => [
        'type' => $this->options['type'] ? $this->options['type'] : 'yandex#map',
        'behaviors' => [
          'scrollZoom',
          'dblClickZoom',
          'drag',
        ],
      ],
      'display_options' => [
        'display_type' => 'map',
      ],
      'controls' => 1,
      'traffic' => 0,
      'clusterer' => 0,
      'auto_zoom' => 0,
      'enable_polygons' => 0,
      'enable_lines' => 0,
      'enable_placemarks' => 0,
      'enable_routes' => 0,
      'placemarks' => $prepared_params['placemarks'],
      'lines' => $prepared_params['lines'],
      'polygons' => $prepared_params['polygons'],
      'routes' => $prepared_params['routes'],
      'edit' => TRUE,
    ];
    $coords = json_decode($this->options['coords']);
    if (isset($coords->center, $coords->zoom)) {
      $map['init']['center'] = $coords->center;
      $map['init']['zoom'] = $coords->zoom;
    }
    $id = drupal_html_id(implode('-', [
      $this->plugin_name,
      $this->view->name,
      $this->view->current_display,
      'style_options_form',
    ]));
    $yandexmap_field_settings = $this->view->display_handler->handlers['field'][$this->options['yandex_map_field']]->options['settings'];

    // Set width and height.
    if (isset($yandexmap_field_settings['width']) && isset($yandexmap_field_settings['height'])) {
      $width = $yandexmap_field_settings['width'];
      $height = $yandexmap_field_settings['height'];
    }
    elseif (isset($yandexmap_field_settings['s_width']) && isset($yandexmap_field_settings['s_height'])) {
      $width = $yandexmap_field_settings['s_width'];
      $height = $yandexmap_field_settings['s_height'];
    }
    else {
      $width = YAMAPS_DEFAULT_ADMIN_UI_MAP_WIDTH;
      $height = YAMAPS_DEFAULT_ADMIN_UI_MAP_HEIGHT;
    }
    $form['yamaps_center_options']['map_container'] = [
      '#type' => 'fieldset',
      '#title' => t('Map center'),
      '#states' => [
        'visible' => [
          ':input[name="style_options[yamaps_center_options][map_center_type]"]' => [
            'value' => 'mini_map',
          ],
        ],
      ],
    ];

    // Map container.
    $form['yamaps_center_options']['map_container']['map'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => '',
      '#description' => t('Map view will be used when "Choose on map." radio is active'),
      '#attributes' => [
        'style' => ' width: ' . $width . '; height:' . $height . ';',
        'id' => $id,
        'class' => [
          'yamaps-map-container',
        ],
      ],
    ];
    $form['yamaps_center_options']['map_container']['coords'] = [
      '#type' => 'textfield',
      '#title' => t('Coordinates'),
      '#default_value' => $this
        ->getCoordinates(),
      '#attributes' => [
        'class' => [
          'field-yamaps-coords-' . $id,
        ],
        'style' => 'width: 100%;',
      ],
      '#states' => [
        'invisible' => [
          ':input[name="style_options[yandex_map_field]"]' => [
            'value' => '',
          ],
        ],
        'visible' => [
          ':input[name="style_options[yamaps_center_options][map_center_type]"]' => [
            'value' => 'mini_map',
          ],
        ],
      ],
      '#description' => t('Search for an object on the map to fill this field or leave it blank (if field is not required).'),
    ];

    // Hidden elements to save map information.
    $form['type'] = [
      '#type' => 'hidden',
      '#title' => t('Type'),
      '#default_value' => $this->options['type'],
      '#attributes' => [
        'class' => [
          'field-yamaps-type-' . $id,
        ],
      ],
    ];

    // Hidden elements to saving map information.
    $form['placemarks'] = [
      '#type' => 'hidden',
      '#default_value' => $this->options['placemarks'],
      '#attributes' => [
        'class' => [
          'field-yamaps-placemarks-' . $id,
        ],
      ],
    ];
    $form['lines'] = [
      '#type' => 'hidden',
      '#default_value' => $this->options['lines'],
      '#attributes' => [
        'class' => [
          'field-yamaps-lines-' . $id,
        ],
      ],
    ];
    $form['polygons'] = [
      '#type' => 'hidden',
      '#default_value' => $this->options['polygons'],
      '#attributes' => [
        'class' => [
          'field-yamaps-polygons-' . $id,
        ],
      ],
    ];
    $form['routes'] = [
      '#type' => 'hidden',
      '#default_value' => $this->options['routes'],
      '#attributes' => [
        'class' => [
          'field-yamaps-routes-' . $id,
        ],
      ],
    ];
    $form['#attached']['js'][] = [
      'data' => [
        'yamaps' => [
          $id => $map,
        ],
      ],
      'type' => 'setting',
    ];

    // Load library.
    $form['#attached']['library'][] = [
      'yamaps',
      'yamaps.full',
    ];
    $form['placemark_title'] = [
      '#title' => t('Placemark title'),
      '#type' => 'select',
      '#options' => $fields,
      '#default_value' => $this->options['placemark_title'],
      '#states' => [
        'invisible' => [
          ':input[name="style_options[yandex_map_field]"]' => [
            'value' => '',
          ],
        ],
      ],
    ];
    $form['baloon_title'] = [
      '#title' => t('Baloon title'),
      '#type' => 'select',
      '#options' => $fields,
      '#default_value' => $this->options['baloon_title'],
      '#states' => [
        'invisible' => [
          ':input[name="style_options[yandex_map_field]"]' => [
            'value' => '',
          ],
        ],
      ],
    ];
    $form['baloon_body'] = [
      '#title' => t('Balloon body Field'),
      '#type' => 'select',
      '#multiple' => TRUE,
      '#options' => $fields,
      '#default_value' => $this->options['baloon_body'],
      '#states' => [
        'invisible' => [
          ':input[name="style_options[yandex_map_field]"]' => [
            'value' => '',
          ],
        ],
      ],
    ];
  }

  /**
   * Prepares map body.
   *
   * @param array $body_array
   *   Body array.
   *
   * @return string
   *   Prepared body.
   */
  public function prepareBody(array $body_array) {
    $output = '<div class="balloon-inner">';
    foreach ($body_array as $key => $val) {
      $output .= '<span class="' . $key . '">' . $val . '</span>';
    }
    $output .= '</div>';
    return $output;
  }

  /**
   * Checks if the style uses fields.
   *
   * @return bool
   *   Boolean value.
   */
  public function uses_fields() {
    $fields = parent::uses_fields();
    $yandexmap_field = '';
    if (isset($this->view->field)) {
      foreach ($this->view->field as $field_name => $field_handler) {
        if (isset($field_handler->field_info) && $field_handler->field_info['type'] == 'field_yamaps') {
          $yandexmap_field = $field_name;
          break;
        }
      }
    }

    // Excluded field.
    if (isset($this->view->field[$yandexmap_field])) {
      $this->view->field[$yandexmap_field]->options['exclude'] = TRUE;
    }
    return $fields;
  }

  /**
   * Validates views style form.
   *
   * @return array
   *   Errors list.
   */
  public function validate() {
    $errors = parent::validate();

    // Checking of existence of pager settings.
    if ($this->display->handler
      ->use_pager()) {
      $showed =& drupal_static(__FUNCTION__, FALSE);
      if (!$showed) {
        $errors[] = t('The Yandex Maps style cannot be used with a pager. Disable the "Use pager" option for this display.');
        $showed = TRUE;
      }
    }
    return $errors;
  }

  /**
   * Renders the map.
   *
   * @return array
   *   Rendered string.
   */
  public function render() {
    $placemarks = [];
    $prepared_lines = [];
    $prepared_polygons = [];
    $prepared_route = [];
    $this->options['coords'] = $this
      ->getCoordinates();
    $prepared_params = yamaps_format_values_to_js($this->options);
    if (empty($this->options['yandex_map_field'])) {
      return t('Please add "Yandex Maps" field and chose it in views settings form.');
    }
    $yandexmap_field_type = $this->view->field[$this->options['yandex_map_field']]->options['type'];
    if ($yandexmap_field_type == YAMAPS_STATIC_FORMATTER && (!isset($prepared_params['coords']['center']) || isset($prepared_params['coords']['center']) && !is_array($prepared_params['coords']['center']))) {
      return t('The Static Yandex Maps style cannot be used without coordinates of center.');
    }
    $this
      ->render_fields($this->view->result);
    $yandexmap_field_name = 'field_' . $this->options['yandex_map_field'];
    $yandexmap_field_settings = $this->view->field[$this->options['yandex_map_field']]->options['settings'];
    foreach ($this->view->result as $row_index => $row) {

      // Fix yandex cart.
      foreach ($row->{$yandexmap_field_name} as $yandexmap_field_coords) {
        if (isset($yandexmap_field_coords['raw']['placemarks'])) {

          // Preparing placemarks.
          $decoded_placemarks = drupal_json_decode($yandexmap_field_coords['raw']['placemarks']);
          if (is_array($decoded_placemarks)) {
            foreach ($decoded_placemarks as $placemark) {

              // Override placemark title.
              $this
                ->overridePlacemarkTitle($placemark, $row_index, $row);

              // Prepare Baloon title.
              if ($this->options['baloon_title'] && $this->options['baloon_title'] !== self::PLACEMARK_DEFAULT_FIELD) {
                $placemark['params'][self::PLACEMARK_BALLON_HEADER] = $this->rendered_fields[$row_index][$this->options['baloon_title']];
              }

              // Prepare Baloon body.
              if (isset($this->options['baloon_body']) && is_array($this->options['baloon_body'])) {
                $ballon_body = [];
                foreach ($this->options['baloon_body'] as $bval) {
                  $ballon_body[] = $this
                    ->get_field($row_index, $bval);
                }
                $placemark['params'][self::PLACEMARK_BALLON_BODY] = $this
                  ->prepareBody($ballon_body);
              }
              $this->view->row_index = $row_index;
              unset($ballon_body);
              $placemarks[] = $placemark;
            }
          }
        }

        // Preparing lines.
        if (isset($yandexmap_field_coords['raw']['lines'])) {
          $decoded_lines = drupal_json_decode($yandexmap_field_coords['raw']['lines']);
          if (is_array($decoded_lines)) {
            foreach ($decoded_lines as $lines) {
              $prepared_lines[] = $lines;
            }
          }
        }

        // Preparing polygons.
        if (isset($yandexmap_field_coords['raw']['polygons'])) {
          $decoded_polygons = drupal_json_decode($yandexmap_field_coords['raw']['polygons']);
          if (is_array($decoded_polygons)) {
            foreach ($decoded_polygons as $polygons) {
              $prepared_polygons[] = $polygons;
            }
          }
        }

        // Preparing routes.
        if (isset($yandexmap_field_coords['raw']['routes'])) {
          $decoded_routes = drupal_json_decode($yandexmap_field_coords['raw']['routes']);
          if (is_array($decoded_routes)) {
            foreach ($decoded_routes as $route) {
              $prepared_route[] = $route;
            }
          }
        }
      }
    }
    unset($this->view->row_index);
    $array_of_unique_params = [
      $this->plugin_name,
      $this->view->name,
      $this->view->current_display,
    ];
    if (isset($this->view->dom_id)) {
      $array_of_unique_params[] = $this->view->dom_id;
    }

    // Unique map id.
    $id = drupal_html_id(implode('-', $array_of_unique_params));
    $open_button_id = drupal_html_id(implode('-', [
      $id,
      'open_button',
    ]));
    $close_button_id = drupal_html_id(implode('-', [
      $id,
      'close_button',
    ]));
    switch ($this->options['yamaps_center_options']['map_center_type']) {
      case 'geolocation':
        $prepared_params['coords']['center'] = NULL;
        $parameters = yamaps_geocoding($this->options['yamaps_center_options']['map_center_geolocation']);
        if (isset($parameters) && $parameters !== FALSE) {
          $prepared_params['coords']['center'] = $parameters['map_center'];
        }
        $prepared_params['coords']['zoom'] = ++$this->options['yamaps_center_options']['zoom'];
        $prepared_params['type'] = 'yandex#map';
        break;
      case 'mini_map':

        // Merging placemark.
        if (is_array($prepared_params['placemarks'])) {
          foreach ($prepared_params['placemarks'] as $p) {
            $placemarks[] = $p;
          }
        }

        // Merging lines.
        if (is_array($prepared_params['lines'])) {
          foreach ($prepared_params['lines'] as $lines) {
            $prepared_lines[] = $lines;
          }
        }

        // Merging polygons.
        if (is_array($prepared_params['polygons'])) {
          foreach ($prepared_params['polygons'] as $polygon) {
            $prepared_polygons[] = $polygon;
          }
        }

        // Merging routes.
        if (is_array($prepared_params['routes'])) {
          foreach ($prepared_params['routes'] as $route) {
            $prepared_route[] = $route;
          }
        }
        break;
    }
    $views_output = [];
    switch ($yandexmap_field_type) {
      case YAMAPS_DYNAMIC_FORMATTER:

        // Map initialization parameters.
        $map = [
          'id' => $id,
          'display_options' => [
            'display_type' => $yandexmap_field_settings['yamaps_display_options']['display_type'],
          ],
          'controls' => $yandexmap_field_settings['controls'],
          'traffic' => $yandexmap_field_settings['traffic'],
          'clusterer' => isset($yandexmap_field_settings['clusterer']) ? $yandexmap_field_settings['clusterer'] : 0,
          'auto_zoom' => isset($yandexmap_field_settings['auto_zoom']) ? $yandexmap_field_settings['auto_zoom'] : 0,
          'placemarks' => empty($placemarks) ? NULL : $placemarks,
          'lines' => empty($prepared_lines) ? NULL : $prepared_lines,
          'polygons' => empty($prepared_polygons) ? NULL : $prepared_polygons,
          'routes' => empty($prepared_route) ? NULL : $prepared_route,
          'enable_placemarks' => (int) (!empty($yandexmap_field_settings['enable_placemarks'])),
          'enable_lines' => (int) (!empty($yandexmap_field_settings['enable_lines'])),
          'enable_polygons' => (int) (!empty($yandexmap_field_settings['enable_polygons'])),
          'enable_routes' => (int) (!empty($yandexmap_field_settings['enable_routes'])),
          'edit' => FALSE,
          'init' => [
            'type' => $prepared_params['type'],
            'center' => empty($prepared_params['coords']['center']) ? NULL : $prepared_params['coords']['center'],
            'zoom' => empty($prepared_params['coords']['zoom']) ? NULL : $prepared_params['coords']['zoom'],
            'behaviors' => array_values(array_filter($yandexmap_field_settings['behaviors'])),
          ],
        ];

        // Adding map to js.
        $views_output['#attached']['js'][] = [
          'data' => [
            'yamaps' => [
              $id => $map,
            ],
          ],
          'type' => 'setting',
        ];

        // Load library.
        $views_output['#attached']['library'][] = [
          'yamaps',
          'yamaps.full',
        ];
        $hide_map = isset($yandexmap_field_settings['yamaps_display_options']['display_type']) && $yandexmap_field_settings['yamaps_display_options']['display_type'] == 'map_button';
        $map_class = [
          'yamaps-map-container',
        ];
        $map_class[] = $hide_map ? 'element-invisible' : '';
        if (isset($yandexmap_field_settings['yamaps_display_options']['display_type']) && $yandexmap_field_settings['yamaps_display_options']['display_type'] == 'map_button') {
          $views_output['open_map_button'] = [
            '#type' => 'html_tag',
            '#tag' => 'div',
            '#value' => isset($yandexmap_field_settings['yamaps_display_options']['open_button_text']) ? t($yandexmap_field_settings['yamaps_display_options']['open_button_text']) : t(YAMAPS_DEFAULT_OPEN_MAP_TEXT),
            '#attributes' => [
              'id' => $open_button_id,
              'class' => [
                'open_yamap_button',
              ],
              'mapId' => $id,
            ],
          ];
          $views_output['close_map_button'] = [
            '#type' => 'html_tag',
            '#tag' => 'div',
            '#value' => isset($yandexmap_field_settings['yamaps_display_options']['close_button_text']) ? t($yandexmap_field_settings['yamaps_display_options']['close_button_text']) : t(YAMAPS_DEFAULT_CLOSE_MAP_TEXT),
            '#attributes' => [
              'id' => $close_button_id,
              'class' => [
                'close_yamap_button',
                'element-invisible',
              ],
              'mapId' => $id,
            ],
          ];
        }
        $views_output[] = [
          '#type' => 'html_tag',
          '#tag' => 'div',
          '#attributes' => [
            'style' => ' width: ' . $yandexmap_field_settings['width'] . '; height:' . $yandexmap_field_settings['height'] . ';',
            'id' => $id,
            'class' => $map_class,
          ],
          '#value' => '',
        ];
        break;
      case YAMAPS_STATIC_FORMATTER:
        $params = [
          'll' => end($prepared_params['coords']['center']) . ',' . reset($prepared_params['coords']['center']),
          'z' => $prepared_params['coords']['zoom'],
          'size' => $yandexmap_field_settings['s_width'] . ',' . $yandexmap_field_settings['s_height'],
        ];

        // 2.x to 1.x types.
        $map_types = [
          'yandex#map' => 'map',
          'yandex#satellite' => 'sat',
          'yandex#hybrid' => 'sat,skl',
          'yandex#publicMap' => 'pmap',
          'yandex#publicMapHybrid' => 'sat,pskl',
        ];
        $params['l'] = $map_types[$prepared_params['type']];
        if ($yandexmap_field_settings['s_traffic']) {
          $params['l'] .= ',trf';
        }

        // 2.x to 1.x colors.
        $colors21 = [
          'blue' => 'bl',
          'lightblue' => 'lb',
          'night' => 'nt',
          'darkblue' => 'db',
          'green' => 'gn',
          'white' => 'wt',
          'red' => 'rd',
          'orange' => 'or',
          'darkorange' => 'do',
          'yellow' => 'yw',
          'violet' => 'vv',
          'pink' => 'pn',
        ];

        // 2.x to hex colors.
        $colors = [
          'blue' => '006cff',
          'lightblue' => '66c7ff',
          'night' => '004056',
          'darkblue' => '00339a',
          'green' => '33cc00',
          'white' => 'ffffff',
          'red' => 'ff0000',
          'orange' => 'ffb400',
          'darkorange' => 'ff6600',
          'yellow' => 'ffea00',
          'violet' => 'b832fd',
          'pink' => 'fd32fb',
        ];

        // Placemarks.
        if (!empty($placemarks)) {
          $pt = [];
          foreach ($placemarks as $placemark) {
            $pm = end($placemark['coords']) . ',' . reset($placemark['coords']) . ',';
            $pm .= 'pm2';
            $pm .= $colors21[$placemark['params']['color']];
            $pm .= 'm';
            $pt[] = $pm;
          }
          $params['pt'] = implode('~', $pt);
        }

        // Lines and polygons.
        $pl = [];
        if ($prepared_lines) {
          foreach ($prepared_lines as $line) {
            $opts = $line['params'];
            $pm = 'c:' . $colors[$opts['strokeColor']] . dechex(255 * $opts['opacity']) . ',';
            $pm .= 'w:' . $opts['strokeWidth'] . ',';
            $c = [];
            foreach ($line['coords'] as $coords) {
              $c[] = end($coords);
              $c[] = reset($coords);
            }
            $pm .= implode(',', $c);
            $pl[] = $pm;
          }
        }
        if ($prepared_polygons) {
          foreach ($prepared_polygons as $polygon) {
            $opts = $polygon['params'];
            $opa = dechex(255 * $opts['opacity']);
            $pm = 'c:' . $colors[$opts['strokeColor']] . $opa . ',';
            $pm .= 'f:' . $colors[$opts['fillColor']] . $opa . ',';
            $pm .= 'w:' . $opts['strokeWidth'] . ',';
            $c = [];
            foreach ($polygon['coords'] as $coords_array) {
              foreach ($coords_array as $coords) {
                $c[] = end($coords);
                $c[] = reset($coords);
              }
            }
            $pm .= implode(',', $c);
            $pl[] = $pm;
          }
        }
        if (!empty($pl)) {
          $params['pl'] = implode('~', $pl);
        }
        $open_button_id = drupal_html_id(implode('-', [
          $id,
          'open_button',
        ]));
        $hide_map = isset($yandexmap_field_settings['yamaps_display_options_static']['display_type_static']) && $yandexmap_field_settings['yamaps_display_options_static']['display_type_static'] == 'map_button';
        $map_display_style = $hide_map ? '; display: none' : '';

        // Returns map container div with image.
        $views_output['map_container'] = [
          '#type' => 'html_tag',
          '#tag' => 'div',
          '#attributes' => [
            'id' => $id,
            'style' => 'width:' . $yandexmap_field_settings['s_width'] . 'px; height:' . $yandexmap_field_settings['s_height'] . 'px' . $map_display_style . ';',
          ],
          '#value' => theme('image', [
            'path' => url(YAMAPS_STATIC_API_URL, [
              'query' => $params,
              'external' => TRUE,
            ]),
            'width' => $yandexmap_field_settings['s_width'],
            'height' => $yandexmap_field_settings['s_height'],
            'title' => t('Yandex Map'),
          ]),
        ];
        if ($hide_map) {
          $views_output['open_button_text'] = [
            '#type' => 'html_tag',
            '#tag' => 'div',
            '#value' => isset($yandexmap_field_settings['yamaps_display_options_static']['open_button_text_static']) ? t($yandexmap_field_settings['yamaps_display_options_static']['open_button_text_static']) : t(YAMAPS_DEFAULT_OPEN_MAP_TEXT),
            '#attributes' => [
              'id' => $open_button_id,
              'class' => [
                'open_yamap_button_static',
              ],
              'mapId' => $id,
            ],
          ];
        }

        // Map initialization parameters.
        $maps[$id] = [
          'display_options' => [
            'display_type' => $yandexmap_field_settings['yamaps_display_options_static']['display_type_static'],
          ],
        ];

        // Adding CSS for button.
        $views_output['#attached']['css'][] = drupal_get_path('module', 'yamaps') . '/misc/yamaps.css';

        // Adding map to js and load library.
        $views_output['#attached']['js'][] = [
          'data' => [
            'yamapsStatic' => $maps,
          ],
          'type' => 'setting',
        ];
        $views_output['#attached']['library'][] = [
          'yamaps',
          'yamaps.full',
        ];
        break;
    }
    return $views_output;
  }

  /**
   * Returns field names.
   *
   * @return array
   *   Fields list.
   */
  public function getFields() {
    $field_names = [
      '' => t(self::PLACEMARK_NONE_FIELD),
      self::PLACEMARK_DEFAULT_FIELD => t('Default baloon value'),
    ];
    $fields = $this->display->handler
      ->get_handlers('field');
    foreach ($fields as $id => $handler) {
      if (isset($handler->human_name)) {
        $field_names[$id] = $handler->human_name;
      }
      else {
        $field_names[$id] = $handler->definition['title'];
      }
    }
    return $field_names;
  }

  /**
   * Override Placemark title.
   *
   * @param int $placemark
   *   Placemark.
   * @param int $row_index
   *   Row index.
   * @param array $row
   *   Row.
   */
  private function overridePlacemarkTitle(&$placemark, $row_index, array $row) {
    if (isset($this->options['placemark_title'], $this->view->field[$this->options['placemark_title']]) && $this->options['placemark_title'] !== self::PLACEMARK_DEFAULT_FIELD) {

      // Prepare placemark title.
      $marker_title = $this
        ->preparePlacemarkTitle($row_index);
      $field_title_settings = $this->view->field[$this->options['placemark_title']];
      if (isset($field_title_settings->field_info['type'])) {
        switch ($field_title_settings->field_info['type']) {
          case 'image':
            if (isset($row->{'field_' . $this->options['placemark_title']}[0]['rendered']['#image_style'], $row->{'field_' . $this->options['placemark_title']}[0]['raw']['uri'])) {

              // Special logic for image fields.
              // Placemark type.
              $placemark['options']['iconLayout'] = 'default#image';

              // Image href.
              $placemark['options']['iconImageHref'] = image_style_url($row->{'field_' . $this->options['placemark_title']}[0]['rendered']['#image_style'], $row->{'field_' . $this->options['placemark_title']}[0]['raw']['uri']);
              $image_dimensions = getimagesize($placemark['options']['iconImageHref']);

              // Placemark image size.
              $placemark['options']['iconImageSize'] = [
                $image_dimensions[0],
                $image_dimensions[1],
              ];

              // Icon image offset of upper left angle.
              $placemark['options']['iconImageOffset'] = [
                -($image_dimensions[0] / 2),
                $image_dimensions[1] * 0.1 - $image_dimensions[1],
              ];
            }
            else {
              $this
                ->prepareDefaultPlacemarkTitle($placemark, $marker_title);
            }
            break;
          default:
            $this
              ->prepareDefaultPlacemarkTitle($placemark, $marker_title);
            break;
        }
      }
      else {
        $this
          ->prepareDefaultPlacemarkTitle($placemark, $marker_title);
      }
    }
  }

  /**
   * Prepare placemark for the map.
   *
   * @param int $row_index
   *   Row index value.
   *
   * @return string
   *   Prepared string.
   */
  private function preparePlacemarkTitle($row_index) {
    $title = $this
      ->get_field($row_index, $this->options['placemark_title']);
    return check_plain(strip_tags($title));
  }

  /**
   * Prepare default placemark.
   *
   * @param array $placemark
   *   Placemark.
   * @param $marker_title
   *   Marker title.
   */
  private function prepareDefaultPlacemarkTitle(array &$placemark, $marker_title) {
    $placemark['params'][self::PLACEMARK_TITLE] = $marker_title;
  }

  /**
   * Returns yandex maps specific field names.
   *
   * @return array
   *   List of yandex maps fields.
   */
  public function getYandexMapsFields() {
    $field_names = [
      '' => t(self::PLACEMARK_NONE_FIELD),
    ];
    $fields = $this->display->handler
      ->get_handlers('field');
    foreach ($fields as $id => $handler) {
      if (isset($handler->field_info) && $handler->field_info['module'] == 'yamaps') {
        $field_names[$id] = $handler->definition['title'];
      }
    }
    return $field_names;
  }

  /**
   * Returns map coordinates.
   *
   * Get coordinates with support of old coordinates place.
   *
   * @return string
   *   List options 'coords'.
   */
  public function getCoordinates() {
    if (isset($this->options['coords'])) {
      return $this->options['coords'];
    }
    elseif (isset($this->options['yamaps_center_options']['map_container']['coords'])) {
      return $this->options['yamaps_center_options']['map_container']['coords'];
    }
    else {
      return '';
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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_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.
yamaps_views_plugin_style_default_map::getCoordinates public function Returns map coordinates.
yamaps_views_plugin_style_default_map::getFields public function Returns field names.
yamaps_views_plugin_style_default_map::getYandexMapsFields public function Returns yandex maps specific field names.
yamaps_views_plugin_style_default_map::options_form public function Returns Yandex Maps views settings form. Overrides views_plugin_style::options_form
yamaps_views_plugin_style_default_map::option_definition public function Returns default settings for the map. Overrides views_plugin_style::option_definition
yamaps_views_plugin_style_default_map::overridePlacemarkTitle private function Override Placemark title.
yamaps_views_plugin_style_default_map::PLACEMARK_BALLON_BODY constant
yamaps_views_plugin_style_default_map::PLACEMARK_BALLON_HEADER constant
yamaps_views_plugin_style_default_map::PLACEMARK_DEFAULT_FIELD constant
yamaps_views_plugin_style_default_map::PLACEMARK_NONE_FIELD constant
yamaps_views_plugin_style_default_map::PLACEMARK_TITLE constant
yamaps_views_plugin_style_default_map::prepareBody public function Prepares map body.
yamaps_views_plugin_style_default_map::prepareDefaultPlacemarkTitle private function Prepare default placemark.
yamaps_views_plugin_style_default_map::preparePlacemarkTitle private function Prepare placemark for the map.
yamaps_views_plugin_style_default_map::render public function Renders the map. Overrides views_plugin_style::render
yamaps_views_plugin_style_default_map::uses_fields public function Checks if the style uses fields. Overrides views_plugin_style::uses_fields
yamaps_views_plugin_style_default_map::validate public function Validates views style form. Overrides views_plugin_style::validate