View source
<?php
namespace Drupal\geofield\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Render\FormattableMarkup;
class GeofieldItem extends FieldItemBase {
public static function defaultStorageSettings() {
return [
'backend' => 'geofield_backend_default',
] + parent::defaultStorageSettings();
}
public static function defaultFieldSettings() {
return [] + parent::defaultFieldSettings();
}
public static function schema(FieldStorageDefinitionInterface $field) {
$backend_manager = \Drupal::service('plugin.manager.geofield_backend');
try {
if (!empty($field
->getSetting('backend')) && $backend_manager
->getDefinition($field
->getSetting('backend')) != NULL) {
$backend_plugin = $backend_manager
->createInstance($field
->getSetting('backend'));
}
} catch (PluginException $e) {
watchdog_exception("geofield_backend_manager", $e);
}
return [
'columns' => [
'value' => isset($backend_plugin) ? $backend_plugin
->schema() : [],
'geo_type' => [
'type' => 'varchar',
'default' => '',
'length' => 64,
],
'lat' => [
'type' => 'numeric',
'precision' => 18,
'scale' => 12,
'not null' => FALSE,
],
'lon' => [
'type' => 'numeric',
'precision' => 18,
'scale' => 12,
'not null' => FALSE,
],
'left' => [
'type' => 'numeric',
'precision' => 18,
'scale' => 12,
'not null' => FALSE,
],
'top' => [
'type' => 'numeric',
'precision' => 18,
'scale' => 12,
'not null' => FALSE,
],
'right' => [
'type' => 'numeric',
'precision' => 18,
'scale' => 12,
'not null' => FALSE,
],
'bottom' => [
'type' => 'numeric',
'precision' => 18,
'scale' => 12,
'not null' => FALSE,
],
'geohash' => [
'type' => 'varchar',
'length' => GEOFIELD_GEOHASH_LENGTH,
'not null' => FALSE,
],
],
'indexes' => [
'lat' => [
'lat',
],
'lon' => [
'lon',
],
'top' => [
'top',
],
'bottom' => [
'bottom',
],
'left' => [
'left',
],
'right' => [
'right',
],
'geohash' => [
'geohash',
],
'centroid' => [
'lat',
'lon',
],
'bbox' => [
'top',
'bottom',
'left',
'right',
],
],
];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Geometry'))
->addConstraint('GeoType', []);
$properties['geo_type'] = DataDefinition::create('string')
->setLabel(t('Geometry Type'));
$properties['lat'] = DataDefinition::create('float')
->setLabel(t('Centroid Latitude'));
$properties['lon'] = DataDefinition::create('float')
->setLabel(t('Centroid Longitude'));
$properties['left'] = DataDefinition::create('float')
->setLabel(t('Left Bounding'));
$properties['top'] = DataDefinition::create('float')
->setLabel(t('Top Bounding'));
$properties['right'] = DataDefinition::create('float')
->setLabel(t('Right Bounding'));
$properties['bottom'] = DataDefinition::create('float')
->setLabel(t('Bottom Bounding'));
$properties['geohash'] = DataDefinition::create('string')
->setLabel(t('Geohash'));
$properties['latlon'] = DataDefinition::create('string')
->setLabel(t('LatLong Pair'));
return $properties;
}
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
$settings = $this
->getSettings();
$backend_manager = \Drupal::service('plugin.manager.geofield_backend');
$backends = $backend_manager
->getDefinitions();
$backend_options = [];
$backend_descriptions_list = '<ul>';
foreach ($backends as $id => $backend) {
$backend_options[$id] = $backend['admin_label'];
$backend_descriptions_list .= '<li>' . $backend['admin_label'] . ': ' . $backend['description'] . '</li>';
}
$element['backend'] = [
'#type' => 'select',
'#title' => $this
->t('Storage backend'),
'#default_value' => $settings['backend'],
'#options' => $backend_options,
'#description' => [
'#markup' => $this
->t('Select the Backend for storing Geofield data. The following are available: @backend_descriptions_list', [
'@backend_descriptions_list' => new FormattableMarkup($backend_descriptions_list, []),
]),
],
'#disabled' => $has_data,
];
return $element;
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
if (!empty($value)) {
$geo_php_wrapper = \Drupal::service('geofield.geophp');
$geometry = $geo_php_wrapper
->load($value);
return $geometry instanceof \Geometry ? $geometry
->isEmpty() : FALSE;
}
return TRUE;
}
public function setValue($values, $notify = TRUE) {
parent::setValue($values);
$this
->populateComputedValues();
}
protected function populateComputedValues() {
$geom = \Drupal::service('geofield.geophp')
->load($this->value);
if (!empty($geom) && !$geom
->isEmpty()) {
$centroid = $geom
->getCentroid();
$bounding = $geom
->getBBox();
$this->geo_type = $geom
->geometryType();
$this->lon = $centroid
->getX();
$this->lat = $centroid
->getY();
$this->left = $bounding['minx'];
$this->top = $bounding['maxy'];
$this->right = $bounding['maxx'];
$this->bottom = $bounding['miny'];
$this->geohash = substr($geom
->out('geohash'), 0, GEOFIELD_GEOHASH_LENGTH);
$this->latlon = $centroid
->getY() . ',' . $centroid
->getX();
}
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
return [
'value' => \Drupal::service('geofield.wkt_generator')
->WktGenerateGeometry(),
];
}
}