View source
<?php
namespace Drupal\geofield\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\geofield\GeoPHP\GeoPHPInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class GeofieldDefaultFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $geoPhpWrapper;
protected $options;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, GeoPHPInterface $geophp_wrapper) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->geoPhpWrapper = $geophp_wrapper;
$this->options = $this->geoPhpWrapper
->getAdapterMap();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('geofield.geophp'));
}
public static function defaultSettings() {
return [
'output_format' => 'wkt',
'output_escape' => TRUE,
];
}
protected function getOutputFormat() {
return in_array($this
->getSetting('output_format'), array_keys($this->options)) ? $this
->getSetting('output_format') : self::defaultSettings()['output_format'];
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
unset($this->options['google_geocode']);
$elements['output_format'] = [
'#title' => $this
->t('Output Format'),
'#type' => 'select',
'#default_value' => $this
->getOutputFormat(),
'#options' => $this->options,
'#required' => TRUE,
];
$elements['output_escape'] = [
'#title' => $this
->t('Escape output (recommended)'),
'#description' => $this
->t('The text is escaped by converting special characters to HTML entities.<br>In some circumstances (i.e. part of Json output) this might not be the wanted/preferred behaviour.'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('output_escape'),
];
return $elements;
}
public function settingsSummary() {
$summary = [];
$summary[] = $this
->t('Geospatial output format: @format', [
'@format' => $this
->getOutputFormat(),
]);
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$geom = $this->geoPhpWrapper
->load($item->value);
$output = $geom ? $geom
->out($this
->getOutputFormat()) : '';
if ($this
->getSetting('output_escape')) {
$output = Html::escape($output);
}
$elements[$delta] = [
'#markup' => $output,
];
}
return $elements;
}
}