Patch Geofield to add support for centroid WKT in LatLon formatter (Issue #3207078), and remove custom geofield_centroid formatter.

This commit is contained in:
Michael Stenta 2021-04-03 15:16:00 -04:00
parent 19f8851543
commit 577f04fa0c
3 changed files with 3 additions and 86 deletions

View File

@ -57,6 +57,9 @@
"drupal/entity_browser": {
"Issue #3160482: Error with UntrustedCallbackException": "https://www.drupal.org/files/issues/2021-03-02/error-with-untrustedcallbackexception-3160482-5.patch"
},
"drupal/geofield": {
"Issue #3207078: Add support for centroid WKT in LatLon formatter": "https://www.drupal.org/files/issues/2021-04-03/3207078-4.patch"
},
"drupal/state_machine": {
"Issue #3144828: Declare dependency on core options module": "https://www.drupal.org/files/issues/2021-03-01/3144828-11-DO_NOT_TEST_OR_COMMIT.patch"
},

View File

@ -54,10 +54,3 @@ block.settings.map_block:
map_type:
type: string
label: 'Map type'
field.formatter.settings.geofield_centroid:
type: mapping
label: 'Geofield centroid display format settings'
mapping:
output_format:
type: string
label: 'Output format'

View File

@ -1,79 +0,0 @@
<?php
namespace Drupal\farm_map\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\geofield\Plugin\Field\FieldFormatter\LatLonFormatter;
/**
* Plugin implementation of the 'geofield_centroid' formatter.
*
* @FieldFormatter(
* id = "geofield_centroid",
* label = @Translation("Centroid"),
* field_types = {
* "geofield"
* }
* )
*/
class CentroidFormatter extends LatLonFormatter {
/**
* Helper function to get the formatter settings options.
*
* @return array
* The formatter settings options.
*/
protected function formatOptions() {
// Get parent options.
$options = parent::formatOptions();
// Define a WKT Point format.
$options['wkt'] = $this->t('WKT Point');
return $options;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$output = ['#markup' => ''];
$geom = $this->geoPhpWrapper->load($item->value);
if (empty($geom)) {
continue;
}
$centroid = $geom->centroid();
if (!empty($centroid)) {
/** @var \Point $centroid */
if ($this->getOutputFormat() == 'decimal') {
$output = [
'#theme' => 'geofield_latlon',
'#lat' => $centroid->y(),
'#lon' => $centroid->x(),
];
}
elseif ($this->getOutputFormat() == 'wkt') {
$output = [
'#markup' => "POINT({$centroid->x()} {$centroid->y()})",
];
}
else {
$components = $this->getDmsComponents($centroid);
$output = [
'#theme' => 'geofield_dms',
'#components' => $components,
];
}
}
$elements[$delta] = $output;
}
return $elements;
}
}