You are here

function yamaps_block_configure in Yandex.Maps 7

Implements hook_block_configure().

See also

yamaps_field_process()

File

inc/yamaps.block.inc, line 34
Yandex Maps blocks configuration.

Code

function yamaps_block_configure($delta = YAMAPS_DEFAULT_BLOCK_DELTA) {

  // Add elements from default field edit form.
  $settings = [
    '#delta' => 'block-' . $delta,
    '#value' => [
      'coords' => variable_get($delta . '_block_coords', NULL),
      'type' => variable_get($delta . '_block_type', 'yandex#map'),
      'placemarks' => variable_get($delta . '_block_placemarks', NULL),
      'lines' => variable_get($delta . '_block_lines', NULL),
      'polygons' => variable_get($delta . '_block_polygons', NULL),
      'routes' => variable_get($delta . '_block_routes', NULL),
    ],
  ];

  // Map information.
  $coords = isset($settings['#value']['coords']) ? $settings['#value']['coords'] : NULL;
  $coords_array = drupal_json_decode($coords);
  $type = isset($settings['#value']['type']) ? $settings['#value']['type'] : 'yandex#map';
  $placemarks = isset($settings['#value']['placemarks']) ? $settings['#value']['placemarks'] : NULL;
  $placemarks_array = drupal_json_decode($placemarks);
  $lines = isset($settings['#value']['lines']) ? $settings['#value']['lines'] : NULL;
  $lines_array = drupal_json_decode($lines);
  $polygons = isset($settings['#value']['polygons']) ? $settings['#value']['polygons'] : NULL;
  $polygons_array = drupal_json_decode($polygons);
  $routes = isset($settings['#value']['routes']) ? $settings['#value']['routes'] : NULL;
  $routes_array = drupal_json_decode($routes);

  // Unique map id.
  $id = drupal_html_id(implode('-', [
    'ymap',
    $settings['#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',
  ]));
  $block_form_display_options = variable_get('yamaps_block_edit_display_options', []);
  $behaviors = [
    'scrollZoom',
    'dblClickZoom',
    'drag',
  ];
  $form['global_conf'] = [
    '#markup' => l(t('Global configuration for "Yandex Maps" blocks'), 'admin/config/services/yamaps'),
  ];
  $form[$delta . '_block_display_options'] = [
    '#type' => 'fieldset',
    '#title' => t('Display options'),
    '#tree' => TRUE,
  ];
  $block_display_options = variable_get($delta . '_block_display_options', []);
  $form[$delta . '_block_display_options']['map_type'] = [
    '#type' => 'radios',
    '#title' => t('Map type for end user'),
    '#options' => [
      'dynamic' => t('Dynamic Map'),
      'static' => t('Static Map'),
    ],
    '#default_value' => isset($block_display_options['map_type']) ? $block_display_options['map_type'] : 'dynamic',
    '#description' => t('Configure "Yandex Map" type for end user.'),
  ];
  $form[$delta . '_block_display_options']['display_type'] = [
    '#type' => 'radios',
    '#title' => t('Map display style for end user'),
    '#options' => [
      'map' => t('Map'),
      'map_button' => t('Map opened by button click'),
    ],
    '#default_value' => isset($block_display_options['display_type']) ? $block_display_options['display_type'] : 'map',
    '#required' => FALSE,
    '#description' => t('Configure how to display "Yandex Map" block for end user.'),
  ];
  $form[$delta . '_block_display_options']['open_button_text'] = [
    '#type' => 'textfield',
    '#title' => t('"Open" button text'),
    '#default_value' => isset($block_display_options['open_button_text']) ? t($block_display_options['open_button_text']) : t(YAMAPS_DEFAULT_OPEN_MAP_TEXT),
    '#required' => FALSE,
    '#description' => t('Text of button that opens map for end user.'),
    '#states' => [
      'visible' => [
        ':input[name="' . $delta . '_block_display_options[display_type]"]' => [
          'value' => 'map_button',
        ],
      ],
    ],
  ];
  $form[$delta . '_block_display_options']['close_button_text'] = [
    '#type' => 'textfield',
    '#title' => t('"Close" button text'),
    '#default_value' => isset($block_display_options['close_button_text']) ? t($block_display_options['close_button_text']) : t(YAMAPS_DEFAULT_CLOSE_MAP_TEXT),
    '#required' => FALSE,
    '#description' => t('Text of button that closes map for end user.'),
    '#states' => [
      'visible' => [
        ':input[name="' . $delta . '_block_display_options[display_type]"]' => [
          'value' => 'map_button',
        ],
      ],
    ],
  ];
  $form[$delta . '_block_controls'] = [
    '#title' => t('Show controls'),
    '#type' => 'checkbox',
    '#default_value' => variable_get($delta . '_block_controls', TRUE),
    '#states' => [
      'visible' => [
        ':input[name="' . $delta . '_block_display_options[map_type]"]' => [
          'value' => 'dynamic',
        ],
      ],
    ],
  ];
  $form[$delta . '_block_traffic'] = [
    '#title' => t('Show traffic'),
    '#type' => 'checkbox',
    '#default_value' => variable_get($delta . '_block_traffic', FALSE),
  ];
  $form[$delta . '_block_clusterer'] = [
    '#title' => t('Use clusterer'),
    '#type' => 'checkbox',
    '#default_value' => variable_get($delta . '_block_clusterer', FALSE),
  ];
  $form[$delta . '_block_auto_zoom'] = [
    '#title' => t('Auto zoom'),
    '#type' => 'checkbox',
    '#default_value' => variable_get($delta . '_block_auto_zoom', FALSE),
  ];
  $form[$delta . '_block_enable_lines'] = [
    '#title' => t('Enable lines'),
    '#type' => 'checkbox',
    '#default_value' => variable_get($delta . '_block_enable_lines', FALSE),
  ];
  $form[$delta . '_block_enable_placemarks'] = [
    '#title' => t('Enable placemarks'),
    '#type' => 'checkbox',
    '#default_value' => variable_get($delta . '_block_enable_placemarks', FALSE),
  ];
  $form[$delta . '_block_enable_polygons'] = [
    '#title' => t('Enable polygons'),
    '#type' => 'checkbox',
    '#default_value' => variable_get($delta . '_block_enable_polygons', FALSE),
  ];
  $form[$delta . '_block_enable_routes'] = [
    '#title' => t('Enable routes'),
    '#type' => 'checkbox',
    '#default_value' => variable_get($delta . '_block_enable_routes', FALSE),
  ];
  $form[$delta . '_block_behaviors'] = [
    '#title' => t('Available mouse events'),
    '#type' => 'checkboxes',
    '#options' => yamaps_get_behaviors_list(),
    '#default_value' => variable_get($delta . '_block_behaviors', []),
    '#states' => [
      'visible' => [
        ':input[name="' . $delta . '_block_display_options[map_type]"]' => [
          'value' => 'dynamic',
        ],
      ],
    ],
  ];
  $form[$delta . '_block_width'] = [
    '#title' => t('Map width'),
    '#field_suffix' => ' ' . t('in pixels (px) or percentage (%) for dynamic map, in pixels (px) for static map.'),
    '#type' => 'textfield',
    '#default_value' => variable_get($delta . '_block_width', YAMAPS_DEFAULT_BLOCK_MAP_WIDTH),
    '#size' => 5,
    '#element_validate' => [
      'yamaps_field_validate_pixels_percentage',
    ],
    '#required' => TRUE,
  ];
  $form[$delta . '_block_height'] = [
    '#title' => t('Map height'),
    '#field_suffix' => ' ' . t('in pixels (px) or percentage (%) for dynamic map, in pixels (px) for static map.'),
    '#type' => 'textfield',
    '#default_value' => variable_get($delta . '_block_height', YAMAPS_DEFAULT_BLOCK_MAP_HEIGHT),
    '#size' => 5,
    '#element_validate' => [
      'yamaps_field_validate_pixels_percentage',
    ],
    '#required' => TRUE,
  ];
  $open_map_button = FALSE;
  $close_map_button = FALSE;
  if (isset($block_form_display_options['display_type']) && $block_form_display_options['display_type'] == 'map_button') {
    $form['open_map_button'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => t($block_form_display_options['open_button_text']),
      '#attributes' => [
        'id' => $open_button_id,
        'class' => [
          'open_yamap_button',
        ],
        'mapId' => $id,
      ],
    ];
    $form['close_map_button'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => t($block_form_display_options['close_button_text']),
      '#attributes' => [
        'id' => $close_button_id,
        'class' => [
          'close_yamap_button',
          'element-invisible',
        ],
        'mapId' => $id,
      ],
    ];
    $open_map_button = TRUE;
    $close_map_button = TRUE;
  }
  $container_width = isset($block_form_display_options['width']) ? $block_form_display_options['width'] : YAMAPS_DEFAULT_ADMIN_UI_MAP_WIDTH;
  $container_height = isset($block_form_display_options['height']) ? $block_form_display_options['height'] : YAMAPS_DEFAULT_ADMIN_UI_MAP_HEIGHT;

  // Map container.
  $form['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 . ';',
    ],
  ];
  $form['coords'] = [
    '#type' => 'textfield',
    '#title' => t('Coordinates'),
    '#default_value' => $coords,
    '#attributes' => [
      'class' => [
        'field-yamaps-coords-' . $id,
      ],
      'style' => 'width: 100%;',
    ],
    '#description' => t('Search for an object on the map to fill this field.'),
    '#required' => TRUE,
  ];

  // Hidden elements to save map information.
  $form['type'] = [
    '#type' => 'hidden',
    '#title' => t('Type'),
    '#default_value' => $type,
    '#attributes' => [
      'class' => [
        'field-yamaps-type-' . $id,
      ],
    ],
  ];
  $form['placemarks'] = [
    '#type' => 'hidden',
    '#title' => t('Placemarks'),
    '#default_value' => $placemarks,
    '#attributes' => [
      'class' => [
        'field-yamaps-placemarks-' . $id,
      ],
    ],
  ];
  $form['lines'] = [
    '#type' => 'hidden',
    '#title' => t('Lines'),
    '#default_value' => $lines,
    '#attributes' => [
      'class' => [
        'field-yamaps-lines-' . $id,
      ],
    ],
  ];
  $form['polygons'] = [
    '#type' => 'hidden',
    '#title' => t('Polygons'),
    '#default_value' => $polygons,
    '#attributes' => [
      'class' => [
        'field-yamaps-polygons-' . $id,
      ],
    ],
  ];
  $form['routes'] = [
    '#type' => 'hidden',
    '#title' => t('Routes'),
    '#default_value' => $routes,
    '#attributes' => [
      'class' => [
        'field-yamaps-routes-' . $id,
      ],
    ],
  ];

  // Map description.
  $form['#description'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#value' => l(t('Terms of service «API Yandex.Maps»'), YAMAPS_LEGAL_AGREEMENT_URL, [
      'attributes' => [
        'target' => '_blank',
      ],
    ]),
    '#attributes' => [
      'class' => [
        'yamaps-terms',
      ],
    ],
  ];

  // Map initialization parameters.
  $map = [
    'init' => [
      'center' => $coords_array['center'],
      'zoom' => $coords_array['zoom'],
      'type' => $type,
      'behaviors' => $behaviors,
    ],
    'display_options' => [
      'display_type' => isset($block_form_display_options['display_type']) ? $block_form_display_options['display_type'] : 'map',
    ],
    'controls' => (int) variable_get($delta . '_block_controls', NULL),
    'traffic' => (int) variable_get($delta . '_block_traffic', NULL),
    'clusterer' => (int) variable_get($delta . '_block_clusterer', NULL),
    'auto_zoom' => (int) variable_get($delta . '_block_auto_zoom', NULL),
    'placemarks' => $placemarks_array,
    'lines' => $lines_array,
    'polygons' => $polygons_array,
    'routes' => $routes_array,
    'enable_placemarks' => (int) variable_get($delta . '_block_enable_placemarks', NULL),
    'enable_lines' => (int) variable_get($delta . '_block_enable_lines', NULL),
    'enable_polygons' => (int) variable_get($delta . '_block_enable_polygons', NULL),
    'enable_routes' => (int) variable_get($delta . '_block_enable_routes', NULL),
    'edit' => TRUE,
  ];

  // Add information about this map to js.
  $form['#attached']['js'][] = [
    'data' => [
      'yamaps' => [
        $id => $map,
      ],
    ],
    'type' => 'setting',
  ];

  // Load library.
  $form['#attached']['library'][] = [
    'yamaps',
    'yamaps.full',
  ];
  return $form;
}