DemoWidget.php in Geolocation Field 8.2
File
modules/geolocation_demo/src/Form/DemoWidget.php
View source
<?php
namespace Drupal\geolocation_demo\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Field\WidgetPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class DemoWidget extends FormBase {
protected $pluginManagerFieldWidget;
protected $entityTypeManager;
public function __construct(WidgetPluginManager $plugin_manager_field_widget, EntityTypeManager $entity_type_manager) {
$this->pluginManagerFieldWidget = $plugin_manager_field_widget;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.field.widget'), $container
->get('entity_type.manager'));
}
public function getWidgetForm($widget_id, array $form, FormStateInterface $form_state) {
$node = $this->entityTypeManager
->getStorage('node')
->create([
'type' => 'geolocation_default_article',
]);
$field_name = 'field_geolocation_demo_multiple';
$field_definition = $node
->getFieldDefinition($field_name);
$widget_settings = [
'field_definition' => $field_definition,
'form_mode' => 'default',
'prepare' => TRUE,
'configuration' => [
'settings' => [],
'third_party_settings' => [],
],
];
$widget = $this->pluginManagerFieldWidget
->getInstance(array_merge_recursive($widget_settings, [
'configuration' => [
'type' => $widget_id,
],
]));
$items = $node
->get($field_name);
$form['#parents'] = [];
return $widget
->form($items, $form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
Classes
Name |
Description |
DemoWidget |
Returns responses for geolocation_demo module routes. |