GeofieldBaseWidget.php in Geofield 8
File
src/Plugin/Field/FieldWidget/GeofieldBaseWidget.php
View source
<?php
namespace Drupal\geofield\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\geofield\Plugin\GeofieldBackendManager;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\geofield\GeoPHP\GeoPHPInterface;
use Drupal\geofield\WktGeneratorInterface;
abstract class GeofieldBaseWidget extends WidgetBase implements ContainerFactoryPluginInterface {
protected $geofieldBackend = NULL;
protected $geoPhpWrapper;
protected $wktGenerator;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, GeoPHPInterface $geophp_wrapper, WktGeneratorInterface $wkt_generator, GeofieldBackendManager $geofield_backend_manager = NULL) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
try {
if ($geofield_backend_manager instanceof GeofieldBackendManager) {
$this->geofieldBackend = $geofield_backend_manager
->createInstance($field_definition
->getSetting("backend"));
}
} catch (PluginException $e) {
watchdog_exception("geofield base widget", $e);
}
$this->geoPhpWrapper = $geophp_wrapper;
$this->wktGenerator = $wkt_generator;
}
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['third_party_settings'], $container
->get('geofield.geophp'), $container
->get('geofield.wkt_generator'), $container
->get('plugin.manager.geofield_backend'));
}
protected function geofieldBackendValue($value) {
$output = NULL;
if ($this->geofieldBackend && ($geom = $this->geoPhpWrapper
->load($value))) {
$output = $this->geofieldBackend
->save($geom);
}
elseif ($geom = $this->geoPhpWrapper
->load($value)) {
$output = $geom
->out('wkt');
}
return $output;
}
}