DemoFormattersController.php in Geolocation Field 8.2
File
modules/geolocation_demo/src/Controller/DemoFormattersController.php
View source
<?php
namespace Drupal\geolocation_demo\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Field\FormatterPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class DemoFormattersController extends ControllerBase {
protected $pluginManagerFieldFormatter;
protected $entityTypeManager;
public function __construct(FormatterPluginManager $plugin_manager_field_formatter, EntityTypeManager $entity_type_manager) {
$this->pluginManagerFieldFormatter = $plugin_manager_field_formatter;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.field.formatter'), $container
->get('entity_type.manager'));
}
public function formatters(Request $request, RouteMatchInterface $route_match) {
$node = $this->entityTypeManager
->getStorage('node')
->create([
'type' => 'geolocation_default_article',
]);
$items = $node
->get('field_geolocation_demo_single');
$default_language = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
$field_definition = $node
->getFieldDefinition('field_geolocation_demo_single');
$widget_settings = [
'field_definition' => $field_definition,
'configuration' => [
'settings' => [
'tokenized_text' => [
'value' => 'The latitude value of this item is: [geolocation_current_item:lat]',
'format' => filter_default_format(),
],
],
'third_party_settings' => [],
],
'view_mode' => 'default',
];
$form = [];
$formatters = [
'geolocation_latlng',
'geolocation_sexagesimal',
'geolocation_token',
];
$moduleHandler = \Drupal::moduleHandler();
if ($moduleHandler
->moduleExists('geolocation_google_maps')) {
$formatters[] = 'geolocation_map';
}
foreach ($formatters as $formatter_id) {
$formatter = $this->pluginManagerFieldFormatter
->getInstance(array_merge_recursive($widget_settings, [
'configuration' => [
'type' => $formatter_id,
],
]));
$form[$formatter_id] = [
'#type' => 'fieldset',
'#title' => $formatter
->getPluginDefinition()['label'],
'widget' => $formatter
->viewElements($items, $default_language),
];
}
return $form;
}
}