Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.44 KB | None | 0 0
  1. $entity_map = Drupal::service('entity.repository')->loadEntityByUuid('openlayers_map', $mapid_['#value']);
  2.  
  3. $entity_map = $this->entityRepository->loadEntityByUuid('openlayers_map', $mapid_['#value']);
  4.  
  5. Error: Using $this when not in object context
  6.  
  7. <?php
  8.  
  9. namespace Drupalopenlayers_geofieldPluginFieldFieldWidget;
  10.  
  11. use DrupalCoreFieldFieldItemListInterface;
  12. use DrupalCoreFieldWidgetBase;
  13. use DrupalCoreFormFormStateInterface;
  14. use DrupalCoreAjaxAjaxResponse;
  15. use DrupalCoreAjaxInvokeCommand;
  16. use DrupalComponentUtilityHtml;
  17. use DrupalCoreEntityEntityTypeManagerInterface;
  18. use DrupalCoreEntityEntityRepositoryInterface;
  19. use SymfonyComponentDependencyInjectionContainerInterface;
  20. use DrupalCorePluginContainerFactoryPluginInterface;
  21. use DrupalCoreFieldFieldDefinitionInterface;
  22. use DrupalgeofieldGeoPHPGeoPHPInterface;
  23.  
  24. /**
  25. * Plugin implementation of the 'geofield_OpenLayersInputWidget' widget.
  26. *
  27. * @FieldWidget(
  28. * id = "geofield_OpenLayersInputWidget",
  29. * label = @Translation("OpenLayers"),
  30. * field_types = {
  31. * "geofield"
  32. * }
  33. * )
  34. */
  35. class OpenLayersInputWidget extends WidgetBase implements ContainerFactoryPluginInterface {
  36.  
  37.  
  38. /**
  39. * @var DrupalCoreEntityEntityTypeManager
  40. */
  41. protected $entityTypeManager;
  42.  
  43. /**
  44. * @var DrupalCoreEntityEntityRepositoryInterface
  45. */
  46. protected $entityRepository;
  47.  
  48. /**
  49. * @var DrupalgeofieldGeoPHPGeoPHPInterface
  50. */
  51. protected $geoPHP;
  52.  
  53. /**
  54. * @param DrupalCoreEntityEntityTypeManagerInterface $entity_type_manager
  55. * @param DrupalCoreEntityEntityRepositoryInterface $entity_repository
  56. * @throws DrupalComponentPluginExceptionInvalidPluginDefinitionException
  57. * @throws DrupalComponentPluginExceptionPluginNotFoundException
  58. */
  59.  
  60. public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, GeoPHPInterface $geoPHP) {
  61. parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
  62. $this->entityTypeManager = $entity_type_manager;
  63. $this->entityRepository = $entity_repository;
  64. $this->geoPHP = $geoPHP;
  65. }
  66.  
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  71. return new static(
  72. $plugin_id,
  73. $plugin_definition,
  74. $configuration['field_definition'],
  75. $configuration['settings'],
  76. $configuration['third_party_settings'],
  77. $container->get('entity_type.manager'),
  78. $container->get('entity.repository'),
  79. $container->get('geofield.geophp')
  80. );
  81. }
  82.  
  83. /**
  84. * Gives a visual Option to save Geometries.
  85. */
  86. public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  87.  
  88. $mapid = Html::getUniqueId('openlayers_map');
  89. $map = openlayers_map_get_info($this->getSetting('openlayers_map'));
  90. $element['selectGeom'] = [
  91. '#type' => 'select',
  92. '#title' => $this->t('Type of Geom'),
  93. '#attributes' => ['id' => $mapid.'-selectfield'],
  94. '#options' => [
  95. 'Point' => 'Point',
  96. 'LineString' => 'Line',
  97. 'Polygon' => 'Polygon',
  98. // 'MultiPoint' => 'Multipoint',
  99. // 'MultiLineString' => 'Multiline',
  100. // 'MultiPolygon' => 'Multipolygon',.
  101. ],
  102. ];
  103.  
  104. $element['map'] = openlayers_render_map($mapid, $map, null, $map['settings']['mapheight'].'px', true);
  105.  
  106.  
  107. if($this->getSetting('openlayers_showbox')) {
  108. $type = 'hidden';
  109. } else {
  110. $type = 'textarea';
  111. }
  112.  
  113. $element['value'] = [
  114. '#type' => $type,
  115. '#title' => $this->t('Geom as WKT'),
  116. '#default_value' => $items[$delta]->value ?: NULL,
  117. '#attributes' => ['id' => $mapid.'-wktbox'],
  118. ];
  119. return $element;
  120. }
  121.  
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public static function defaultSettings() {
  126. return [
  127. 'openlayers_map' => 'none selected',
  128. 'mapeid' => 'blabla',
  129. 'mapheight' => 450,
  130. 'zoom' => 9,
  131. 'minzoom' => 1,
  132. 'maxzoom' => 18,
  133. 'openlayers_showbox' => false,
  134. ] + parent::defaultSettings();
  135. }
  136. /**
  137. * Get the view settings of a map.
  138. */
  139. public function getMapSettings(&$form, FormStateInterface $form_state) {
  140.  
  141. $response = new AjaxResponse();
  142. $mapid_ = $form_state->getTriggeringElement();
  143.  
  144. if($mapid_['#value'] === 'lastSave') {
  145.  
  146. $entity_type = 'node';
  147. $bundle = $mapid_['#attributes']['bundle'];
  148. $form_mode = 'default';
  149.  
  150. $form_display = $this->entityTypeManager->getStorage('entity_form_display')->load($entity_type . '.' . $bundle . '.' . $form_mode)->execute();
  151.  
  152. $field = $form_display->getComponent($mapid_['#attributes']['field']);
  153. $response->addCommand(new InvokeCommand('#openlayers-map-zoom', 'val', [$field['settings']['zoom']]));
  154. $response->addCommand(new InvokeCommand('#openlayers-map-minzoom', 'val', [$field['settings']['minzoom']]));
  155. $response->addCommand(new InvokeCommand('#openlayers-map-maxzoom', 'val', [$field['settings']['maxzoom']]));
  156. $response->addCommand(new InvokeCommand('#openlayers-map-mapheight', 'val', [$field['settings']['mapheight']]));
  157. $response->addCommand(new InvokeCommand('#openlayers-map-map', 'val', [$mapid_['#attributes']['saved']]));
  158.  
  159. } else {
  160.  
  161. //$entity_map = Drupal::service('entity.repository')->loadEntityByUuid('openlayers_map', $mapid_['#value']);
  162. $entity_map = $this->entityRepository->loadEntityByUuid('openlayers_map', $mapid_['#value']);
  163. $mapheight = $entity_map->map_height->value;
  164. $zoom = $entity_map->zoom->value;
  165. $minzoom = $entity_map->minzoom->value;
  166. $maxzoom = $entity_map->maxzoom->value;
  167. $response->addCommand(new InvokeCommand('#openlayers-map-zoom', 'val', [$zoom]));
  168. $response->addCommand(new InvokeCommand('#openlayers-map-minzoom', 'val', [$minzoom]));
  169. $response->addCommand(new InvokeCommand('#openlayers-map-maxzoom', 'val', [$maxzoom]));
  170. $response->addCommand(new InvokeCommand('#openlayers-map-mapheight', 'val', [$mapheight]));
  171. $response->addCommand(new InvokeCommand('#openlayers-map-map', 'val', [$mapid_['#value']]));
  172. }
  173. return $response;
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function settingsForm(array $form, FormStateInterface $form_state) {
  179.  
  180. $config = $this->fieldDefinition;
  181.  
  182. $elements = parent::settingsForm($form, $form_state);
  183.  
  184. $openlayers_map_options = [];
  185. foreach (openlayers_map_get_info() as $key => $map) {
  186. $openlayers_map_options[$key] = $this->t($map['label']);
  187. }
  188.  
  189. $openlayers_map_options['lastSave'] = $openlayers_map_options[$this->getSetting('openlayers_map')] . ' (last save)';
  190.  
  191. $elements['openlayers_mapid'] = [
  192. '#title' => $this->t('OpenLayers Map'),
  193. '#type' => 'select',
  194. '#options' => $openlayers_map_options,
  195. '#default_value' => 'lastSave',
  196. '#required' => TRUE,
  197. '#attributes' => ['class' => ['openlayers-map-selector'], 'field' => $config->getName(), 'bundle' => $config->getTargetBundle(), 'saved' => $this->getSetting('openlayers_map')],
  198. '#ajax' => [
  199. 'callback' => 'Drupalopenlayers_geofieldPluginFieldFieldWidgetOpenLayersInputWidget::getMapSettings',
  200. 'event' => 'change',
  201. 'progress' => [
  202. 'type' => 'throbber',
  203. 'message' => NULL,
  204. ],
  205. ],
  206. ];
  207.  
  208. $elements['zoom'] = [
  209. '#title' => $this->t('Zoom'),
  210. '#type' => 'number',
  211. '#attributes' => ['id' => ['openlayers-map-zoom']],
  212. '#default_value' => $this->getSetting('zoom'),
  213. '#required' => TRUE,
  214. ];
  215. $elements['minzoom'] = [
  216. '#title' => $this->t('Min. Zoom'),
  217. '#type' => 'number',
  218. '#attributes' => ['id' => ['openlayers-map-minzoom']],
  219. '#default_value' => $this->getSetting('minzoom'),
  220. '#required' => TRUE,
  221. ];
  222. $elements['maxzoom'] = [
  223. '#title' => $this->t('Max. Zoom'),
  224. '#type' => 'number',
  225. '#attributes' => ['id' => ['openlayers-map-maxzoom']],
  226. '#default_value' => $this->getSetting('maxzoom'),
  227. '#required' => TRUE,
  228. ];
  229. $elements['mapheight'] = [
  230. '#title' => $this->t('Map Height'),
  231. '#type' => 'number',
  232. '#attributes' => ['id' => ['openlayers-map-mapheight']],
  233. '#default_value' => $this->getSetting('mapheight'),
  234. '#field_suffix' => $this->t('px'),
  235. '#required' => TRUE,
  236. ];
  237.  
  238. $elements['openlayers_map'] = [
  239. '#title' => $this->t('Map Entity ID'),
  240. '#type' => 'hidden',
  241. '#size' => 36,
  242. '#disabled' => FALSE,
  243. '#attributes' => ['id' => ['openlayers-map-map']],
  244. '#default_value' => $this->getSetting('openlayers_map'),
  245. ];
  246. $elements['openlayers_showbox'] = [
  247. '#type' => 'checkbox',
  248. '#title' => $this->t('Hide input value box'),
  249. '#attributes' => ['id' => ['openlayers-map-showbox']],
  250. '#default_value' => $this->getSetting('openlayers_showbox'),
  251. ];
  252. return $elements;
  253. }
  254.  
  255.  
  256. /**
  257. * {@inheritdoc}
  258. */
  259. public function settingsSummary() {
  260.  
  261. $summary = [];
  262.  
  263. foreach (openlayers_map_get_info() as $key => $map) {
  264. if( $key === $this->getSetting('openlayers_map')) {
  265. $summary[] = $this->t('OpenLayers MAP: @map', ['@map' => $this->t($map['label'])]);
  266. }
  267. }
  268. $summary[] = $this->t('Map height: @height px', ['@height' => $this->getSetting('mapheight')]);
  269. return $summary;
  270. }
  271.  
  272. /**
  273. * {@inheritdoc}
  274. */
  275. public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
  276. $geophp = $this->geoPHP;
  277. foreach ($values as $delta => $value) {
  278. if ($geom = $geophp->load($value['value'])) {
  279. $values[$delta]['value'] = $geom->out('wkt');
  280. }
  281. }
  282. return $values;
  283. }
  284. /**
  285. * {@inheritdoc}
  286. *
  287. * This function is called from parent::view().
  288. */
  289. public function viewElements(FieldItemListInterface $items, $langcode) {
  290. $settings = $this->getSettings();
  291. $map = openlayers_map_get_info($settings['openlayers_map']);
  292. $map['settings']['zoom'] = isset($settings['zoom']) ? $settings['zoom'] : NULL;
  293. $map['settings']['minzoom'] = isset($settings['minzoom']) ? $settings['minzoom'] : NULL;
  294. $map['settings']['maxzoom'] = isset($settings['maxzoom']) ? $settings['maxzoom'] : NULL;
  295.  
  296. $elements = [];
  297. foreach ($items as $delta => $item) {
  298. $features = openlayers_process_geofield($item->value);
  299. $elements[$delta] = openlayers_render_map($map, $features, $settings['mapheight'] . 'px');
  300. }
  301. return $elements;
  302. }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement