LocationToGeolocation.php in Geolocation Field 8.3
File
src/Plugin/migrate/process/LocationToGeolocation.php
View source
<?php
namespace Drupal\geolocation\Plugin\migrate\process;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LocationToGeolocation extends ProcessPluginBase implements ContainerFactoryPluginInterface {
protected $migration;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migration = $migration;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration);
}
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!is_array($value) || empty($value['lid'])) {
return NULL;
}
$source_plugin = $this->migration
->getSourcePlugin();
assert($source_plugin instanceof DrupalSqlBase);
$source_database = $source_plugin
->getDatabase();
try {
$location_result = $source_database
->select('location', 'l')
->fields('l')
->condition('l.lid', $value['lid'])
->execute()
->fetchAllAssoc('lid', \PDO::FETCH_ASSOC);
if (count($location_result) === 1) {
$location_raw = reset($location_result);
[
'latitude' => $lat,
'longitude' => $lng,
] = $location_raw;
return (string) $lat !== '0.000000' && (string) $lng !== '0.000000' ? [
'lat' => $lat,
'lng' => $lng,
] : NULL;
}
} catch (DatabaseExceptionWrapper $e) {
}
return NULL;
}
}