You are here

public function YaMaps::buildOptionsForm in Yandex.Maps 8

Provide a form to edit options for this plugin.

Overrides StylePluginBase::buildOptionsForm

File

src/Plugin/views/style/YaMaps.php, line 324

Class

YaMaps
Allow to display several field items on a yandex map.

Namespace

Drupal\yamaps\Plugin\views\style

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $fields = $this
    ->getFields();
  $yandex_fields = $this
    ->getYandexMapsFields();
  $form['yandex_map_field'] = [
    '#title' => $this
      ->t('Yandex Map Field'),
    '#description' => $this
      ->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' => $this
      ->t('Map center'),
    '#states' => [
      'invisible' => [
        ':input[name="style_options[yandex_map_field]"]' => [
          'value' => '',
        ],
      ],
    ],
  ];
  $form['yamaps_center_options']['map_center_type'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Choose map center type'),
    '#options' => [
      'geolocation' => $this
        ->t('Geolocation.'),
      'mini_map' => $this
        ->t('Choose on map.'),
    ],
    '#default_value' => $this->options['yamaps_center_options']['map_center_type'],
    '#required' => FALSE,
    '#description' => $this
      ->t('Type of map displaying.'),
  ];
  $form['yamaps_center_options']['map_center_geolocation'] = [
    '#title' => $this
      ->t('Map center geolocation'),
    '#description' => $this
      ->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' => $this
      ->t('Zoom'),
    '#type' => 'select',
    '#description' => $this
      ->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();
  $decoded_params = $this->geocoding
    ->decodeParams($this->options);

  // Map initialization parameters.
  $map = [
    'init' => [
      'center' => $decoded_params['coords']['center'] ?? NULL,
      'zoom' => $decoded_params['coords']['zoom'] ?? NULL,
      'type' => 'yandex#map',
      'behaviors' => [
        'scrollZoom',
        'dblClickZoom',
        'drag',
      ],
    ],
    'display_options' => [
      'display_type' => 'map',
      'width' => static::YAMAPS_DEFAULT_ADMIN_UI_MAP_WIDTH,
      'height' => static::YAMAPS_DEFAULT_ADMIN_UI_MAP_HEIGHT,
    ],
    'controls' => 1,
    'placemarks' => $decoded_params['placemarks'],
    'edit' => FALSE,
  ];
  $id = Html::getUniqueId(implode('-', [
    $this
      ->getPluginId(),
    $this->view
      ->getDisplay()
      ->getType(),
    $this->view->current_display,
    'style_options_form',
  ]));
  if ($this->options['yandex_map_field']) {
    $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'];
  }
  else {
    $width = static::YAMAPS_DEFAULT_ADMIN_UI_MAP_WIDTH;
    $height = static::YAMAPS_DEFAULT_ADMIN_UI_MAP_HEIGHT;
  }
  $form['yamaps_center_options']['map_container'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->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' => $this
      ->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' => $this
      ->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' => $this
      ->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' => $this
      ->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,
      ],
    ],
  ];

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