You are here

public function YaMapsBlock::blockForm in Yandex.Maps 8

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/YaMapsBlock.php, line 98

Class

YaMapsBlock
Provides a yandex map block.

Namespace

Drupal\yamaps\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {
  $form = parent::blockForm($form, $form_state);
  $config = $this
    ->getConfiguration();

  // Unique map id.
  $mapId = Html::getUniqueId(implode('-', [
    'ymap',
    'yamaps',
    'edit',
  ]));

  // Add elements from default field edit form.
  $settings = [
    '#value' => [
      'coords' => !empty($config['yamaps_block_coords']) ? $config['yamaps_block_coords'] : NULL,
      'type' => !empty($config['yamaps_block_type']) ? $config['yamaps_block_type'] : 'yandex#map',
      'placemarks' => !empty($config['yamaps_block_placemarks']) ? $config['yamaps_block_placemarks'] : NULL,
      'lines' => !empty($config['yamaps_block_lines']) ? $config['yamaps_block_lines'] : NULL,
      'polygons' => !empty($config['yamaps_block_polygons']) ? $config['yamaps_block_polygons'] : NULL,
      'routes' => !empty($config['yamaps_block_routes']) ? $config['yamaps_block_routes'] : NULL,
    ],
  ];

  // Map information.
  $coords = $settings['#value']['coords'];
  $coords_array = Json::decode($settings['#value']['coords']);
  $type = $settings['#value']['type'];
  $placemarks = $settings['#value']['placemarks'];
  $placemarks_array = Json::decode($placemarks);
  $lines = $settings['#value']['lines'];
  $lines_array = Json::decode($lines);
  $polygons = $settings['#value']['polygons'];
  $polygons_array = Json::decode($polygons);
  $routes = $settings['#value']['routes'];
  $routes_array = Json::decode($routes);
  $form['yamaps_block_controls'] = [
    '#title' => $this
      ->t('Show controls'),
    '#type' => 'checkbox',
    '#default_value' => !empty($config['yamaps_block_controls']) ? $config['yamaps_block_controls'] : TRUE,
  ];
  $form['yamaps_block_traffic'] = [
    '#title' => $this
      ->t('Show traffic'),
    '#type' => 'checkbox',
    '#default_value' => !empty($config['yamaps_block_traffic']) ? $config['yamaps_block_traffic'] : FALSE,
  ];
  $form['yamaps_block_clusterer'] = [
    '#title' => $this
      ->t('Use clusterer'),
    '#type' => 'checkbox',
    '#default_value' => !empty($config['yamaps_block_clusterer']) ? $config['yamaps_block_clusterer'] : FALSE,
  ];
  $form['yamaps_block_auto_zoom'] = [
    '#title' => $this
      ->t('Auto zoom'),
    '#type' => 'checkbox',
    '#default_value' => !empty($config['yamaps_block_auto_zoom']) ? $config['yamaps_block_auto_zoom'] : FALSE,
  ];
  $form['yamaps_block_behaviors'] = [
    '#title' => $this
      ->t('Available mouse events'),
    '#type' => 'checkboxes',
    '#options' => $this
      ->getBehaviorsList(),
    '#default_value' => !empty($config['yamaps_block_behaviors']) ? $config['yamaps_block_behaviors'] : [],
  ];
  $form['yamaps_block_width'] = [
    '#title' => $this
      ->t('Map width'),
    '#field_suffix' => ' ' . $this
      ->t('in pixels (px) or percentage (%) for dynamic map, in pixels (px) for static map.'),
    '#type' => 'textfield',
    '#default_value' => !empty($config['yamaps_block_width']) ? $config['yamaps_block_width'] : self::YAMAPS_DEFAULT_BLOCK_MAP_WIDTH,
    '#size' => 5,
    '#element_validate' => [
      [
        $this,
        'yamapsFieldValidatePixelsPercentage',
      ],
    ],
    '#required' => TRUE,
  ];
  $form['yamaps_block_height'] = [
    '#title' => $this
      ->t('Map height'),
    '#field_suffix' => ' ' . $this
      ->t('in pixels (px) or percentage (%) for dynamic map, in pixels (px) for static map.'),
    '#type' => 'textfield',
    '#default_value' => !empty($config['yamaps_block_height']) ? $config['yamaps_block_height'] : self::YAMAPS_DEFAULT_BLOCK_MAP_HEIGHT,
    '#size' => 5,
    '#element_validate' => [
      [
        $this,
        'yamapsFieldValidatePixelsPercentage',
      ],
    ],
    '#required' => TRUE,
  ];
  $container_width = self::YAMAPS_DEFAULT_ADMIN_UI_MAP_WIDTH;
  $container_height = self::YAMAPS_DEFAULT_ADMIN_UI_MAP_HEIGHT;

  // Map container.
  $form['map'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#value' => '',
    '#attributes' => [
      'id' => $mapId,
      'class' => [
        'yamaps-map-container',
      ],
      'style' => 'width: ' . $container_width . '; height: ' . $container_height . ';',
    ],
  ];
  $form['yamaps_block_coords'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Coordinates'),
    '#default_value' => $coords,
    '#attributes' => [
      'class' => [
        'field-yamaps-coords-' . $mapId,
      ],
      'style' => 'width: 100%;',
    ],
    '#description' => $this
      ->t('Search for an object on the map to fill this field.'),
    '#required' => TRUE,
  ];

  // Hidden elements to save map information.
  $form['yamaps_block_type'] = [
    '#type' => 'hidden',
    '#title' => $this
      ->t('Type'),
    '#default_value' => $type,
    '#attributes' => [
      'class' => [
        'field-yamaps-type-' . $mapId,
      ],
    ],
  ];
  $form['yamaps_block_placemarks'] = [
    '#type' => 'hidden',
    '#title' => $this
      ->t('Placemarks'),
    '#default_value' => $placemarks,
    '#attributes' => [
      'class' => [
        'field-yamaps-placemarks-' . $mapId,
      ],
    ],
  ];
  $form['yamaps_block_lines'] = [
    '#type' => 'hidden',
    '#title' => $this
      ->t('Lines'),
    '#default_value' => $lines,
    '#attributes' => [
      'class' => [
        'field-yamaps-lines-' . $mapId,
      ],
    ],
  ];
  $form['yamaps_block_polygons'] = [
    '#type' => 'hidden',
    '#title' => $this
      ->t('Polygons'),
    '#default_value' => $polygons,
    '#attributes' => [
      'class' => [
        'field-yamaps-polygons-' . $mapId,
      ],
    ],
  ];
  $form['yamaps_block_routes'] = [
    '#type' => 'hidden',
    '#title' => $this
      ->t('Routes'),
    '#default_value' => $routes,
    '#attributes' => [
      'class' => [
        'field-yamaps-routes-' . $mapId,
      ],
    ],
  ];
  $yamapsTermsUrl = Url::fromUri(self::YAMAPS_LEGAL_AGREEMENT_URL);

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

  // Map initialization parameters.
  $map = [
    'id' => $mapId,
    'init' => [
      'center' => $coords_array['center'] ?? NULL,
      'zoom' => $coords_array['zoom'] ?? NULL,
      'type' => $type,
      'behaviors' => [
        'scrollZoom',
        'dblClickZoom',
        'drag',
      ],
    ],
    'display_options' => [
      'display_type' => 'map',
      'width' => self::YAMAPS_DEFAULT_ADMIN_UI_MAP_WIDTH,
      'height' => self::YAMAPS_DEFAULT_ADMIN_UI_MAP_HEIGHT,
    ],
    'edit' => TRUE,
    'controls' => 1,
    'traffic' => 0,
    'clusterer' => 0,
    'auto_zoom' => 0,
    'placemarks' => $placemarks_array,
    'lines' => $lines_array,
    'polygons' => $polygons_array,
    'routes' => $routes_array,
  ];
  $form['#attached']['library'][] = 'yamaps/yamaps-placemark';
  $form['#attached']['library'][] = 'yamaps/yamaps-map';
  $form['#attached']['drupalSettings']['yamaps'] = [
    $mapId => $map,
  ];
  return $form;
}