You are here

public function yamaps_views_plugin_style_default_map::options_form in Yandex.Maps 7

Returns Yandex Maps views settings form.

Parameters

array $form: Form structure array.

array $form_state: Form state array.

Overrides views_plugin_style::options_form

File

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

Class

yamaps_views_plugin_style_default_map
Class defines Yandex Maps style plugin handler.

Code

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' => '',
        ],
      ],
    ],
  ];
}