You are here

yamaps.widget.inc in Yandex.Maps 7

Yandex Maps field widget.

File

inc/yamaps.widget.inc
View source
<?php

/**
 * @file
 * Yandex Maps field widget.
 */

/**
 * Implements hook_field_widget_info().
 */
function yamaps_field_widget_info() {
  return [
    'yamaps_field' => [
      'label' => t('Yandex map'),
      'field types' => [
        'field_yamaps',
      ],
    ],
  ];
}

/**
 * Implements hook_field_widget_form().
 */
function yamaps_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  if ($field['type'] == 'field_yamaps') {

    // Unique map id.
    $id = drupal_html_id(implode('-', [
      'ymap',
      $element['#field_name'],
      $element['#delta'],
      'edit',
    ]));

    // Unique map button id.
    $open_button_id = drupal_html_id(implode('-', [
      $id,
      'open_button',
    ]));
    $close_button_id = drupal_html_id(implode('-', [
      $id,
      'close_button',
    ]));
    $value = isset($items[$delta]) ? $items[$delta] : [];
    $element += [
      '#type' => $instance['widget']['type'],
      '#default_value' => $value,
    ];

    // Set value after "Add more" button clicked.
    if (isset($form_state['values'][$field['field_name']][$langcode][$delta])) {
      $value = $form_state['values'][$field['field_name']][$langcode][$delta];
    }

    // Map information.
    yamaps_cleanup_values($value);
    $default_js = yamaps_format_values_to_js($value);

    // Issue #1712292 fix.
    // Display default position on edit form.
    if (!$element['#default_value']) {
      if (isset($element['#entity']) && ($entity = $element['#entity'])) {
        $default_value = field_get_default_value($element['#entity_type'], $entity, $field, $instance);
        $default_value = reset($default_value);
        if (empty($default_value) || !is_array($default_value)) {
          $default_value = [];
        }
        yamaps_cleanup_values($default_value);
        $default_js = yamaps_format_values_to_js($default_value);
      }
    }
    $container_width = isset($field['settings']['display_options']['width']) ? $field['settings']['display_options']['width'] : YAMAPS_DEFAULT_ADMIN_UI_MAP_WIDTH;
    $container_height = isset($field['settings']['display_options']['height']) ? $field['settings']['display_options']['height'] : YAMAPS_DEFAULT_ADMIN_UI_MAP_HEIGHT;
    $open_map_button = FALSE;
    $close_map_button = FALSE;
    if ($field['settings']['display_options']['display_type'] == 'map_button') {
      $element['open_map_button'] = [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#value' => t($field['settings']['display_options']['open_button_text']),
        '#attributes' => [
          'id' => $open_button_id,
          'class' => [
            'open_yamap_button',
          ],
          'mapId' => $id,
        ],
      ];
      $element['close_map_button'] = [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#value' => isset($field['settings']['display_options']['close_button_text']) ? t($field['settings']['display_options']['close_button_text']) : t(YAMAPS_DEFAULT_CLOSE_MAP_TEXT),
        '#attributes' => [
          'id' => $close_button_id,
          'class' => [
            'close_yamap_button',
            'element-invisible',
          ],
          'mapId' => $id,
        ],
      ];
      $open_map_button = TRUE;
      $close_map_button = TRUE;
    }

    // Map container.
    $element['map_wrapper']['map'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => '',
      '#attributes' => [
        'id' => $id,
        'class' => $open_map_button && $close_map_button ? [
          'yamaps-map-container',
          'element-invisible',
        ] : [
          'yamaps-map-container',
        ],
        'style' => 'width: ' . $container_width . '; height: ' . $container_height . ';',
      ],
    ];
    $coords_description = [
      t('Search for an object on the map to fill this field automatically or leave it blank (if field is not required).'),
      '<strong>' . t('Leave this field blank to remove field item.') . '</strong>',
      l(t('Terms of service «API Yandex.Maps»'), YAMAPS_LEGAL_AGREEMENT_URL, [
        'attributes' => [
          'target' => '_blank',
        ],
      ]),
    ];
    $element['coords'] = [
      '#type' => 'textfield',
      '#title' => t('Coordinates'),
      '#default_value' => $value['coords'],
      '#attributes' => [
        'class' => [
          'field-yamaps-coords-' . $id,
        ],
        'style' => 'width: 100%;',
      ],
      '#description' => implode(' ', $coords_description),
    ];
    if ($field['settings']['display_options']['display_type'] == 'map_without_fields') {
      $element['coords']['#attributes']['class'][] = 'element-hidden';
      $element['coords']['#title_display'] = 'none';
      $element['coords']['#description'] = '';
    }
    if ($field['settings']['display_options']['display_type'] != 'map_without_fields') {
      $element['hide'] = [
        '#type' => 'checkbox',
        '#title' => t('Hide field'),
        '#default_value' => $value['hide'],
        '#description' => t("Hide field containing map from the end user.\n    Can be useful when administrator uses field as optional and doesn't intend to display map."),
      ];
    }

    // Hidden elements to saving map information.
    $element['type'] = [
      '#type' => 'hidden',
      '#default_value' => $value['type'],
      '#attributes' => [
        'class' => [
          'field-yamaps-type-' . $id,
        ],
      ],
    ];
    $element['placemarks'] = [
      '#type' => 'hidden',
      '#default_value' => $value['placemarks'],
      '#attributes' => [
        'class' => [
          'field-yamaps-placemarks-' . $id,
        ],
      ],
    ];
    $element['lines'] = [
      '#type' => 'hidden',
      '#default_value' => $value['lines'],
      '#attributes' => [
        'class' => [
          'field-yamaps-lines-' . $id,
        ],
      ],
    ];
    $element['polygons'] = [
      '#type' => 'hidden',
      '#default_value' => $value['polygons'],
      '#attributes' => [
        'class' => [
          'field-yamaps-polygons-' . $id,
        ],
      ],
    ];
    $element['routes'] = [
      '#type' => 'hidden',
      '#default_value' => $value['routes'],
      '#attributes' => [
        'class' => [
          'field-yamaps-routes-' . $id,
        ],
      ],
    ];

    // Map initialization parameters.
    $map = [
      'init' => [
        'center' => isset($default_js['coords']['center']) ? $default_js['coords']['center'] : NULL,
        'zoom' => isset($default_js['coords']['zoom']) ? $default_js['coords']['zoom'] : NULL,
        'type' => $default_js['type'],
        'behaviors' => [
          'scrollZoom',
          'dblClickZoom',
          'drag',
        ],
      ],
      'display_options' => [
        'display_type' => $field['settings']['display_options']['display_type'],
      ],
      'controls' => (int) (!empty($field['settings']['display_options']['controls'])),
      'traffic' => (int) (!empty($field['settings']['display_options']['traffic'])),
      'clusterer' => (int) (!empty($field['settings']['display_options']['clusterer'])),
      'auto_zoom' => (int) (!empty($field['settings']['display_options']['auto_zoom'])),
      'placemarks' => $default_js['placemarks'],
      'lines' => $default_js['lines'],
      'polygons' => $default_js['polygons'],
      'routes' => $default_js['routes'],
      'enable_placemarks' => (int) (!empty($field['settings']['display_options']['enable_placemarks'])),
      'enable_lines' => (int) (!empty($field['settings']['display_options']['enable_lines'])),
      'enable_polygons' => (int) (!empty($field['settings']['display_options']['enable_polygons'])),
      'enable_routes' => (int) (!empty($field['settings']['display_options']['enable_routes'])),
      'edit' => TRUE,
    ];

    // Add information about this map to js.
    // Settings should be attached to each element, because element changes
    // its ID when reloaded via Ajax in case of multi value field.
    $element['#attached']['js'][] = [
      'data' => [
        'yamaps' => [
          $id => $map,
        ],
      ],
      'type' => 'setting',
    ];
    $form['#attached']['library'][] = [
      'yamaps',
      'yamaps.full',
    ];
  }
  return $element;
}