View source
<?php
namespace Drupal\yamaps\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;
class YamapsFieldType extends FieldItemBase {
public static function defaultFieldSettings() {
return [
'enable_placemarks' => TRUE,
'enable_lines' => TRUE,
'enable_polygons' => TRUE,
] + parent::defaultFieldSettings();
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$element['enable_placemarks'] = [
'#type' => 'checkbox',
'#title' => t('Enable "Placemark" tool'),
'#default_value' => $this
->getSetting('enable_placemarks'),
];
$element['enable_lines'] = [
'#type' => 'checkbox',
'#title' => t('Enable "Line" tool'),
'#default_value' => $this
->getSetting('enable_lines'),
];
$element['enable_polygons'] = [
'#type' => 'checkbox',
'#title' => t('Enable "Polygon" tool'),
'#default_value' => $this
->getSetting('enable_lines'),
];
return $element;
}
public static function schema(FieldStorageDefinitionInterface $field) {
return [
'columns' => [
'coords' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Coordinates for "Yandex Maps" object.',
],
'type' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => 'yandex#map',
'description' => 'Type of "Yandex Maps" object.',
],
'placemarks' => [
'type' => 'text',
'not null' => TRUE,
'description' => 'Settings and data for "Yandex Maps" placemark.',
],
'lines' => [
'type' => 'text',
'not null' => TRUE,
'description' => 'Settings and data for "Yandex Maps" lines.',
],
'polygons' => [
'type' => 'text',
'not null' => TRUE,
'description' => 'Settings and data for "Yandex Maps" polygons.',
],
'hide' => [
'type' => 'int',
'default' => 0,
'description' => 'Flag defining whether "Yandex Maps" field should be hidden for end user or not.',
],
],
];
}
public function isEmpty() {
$value = $this
->get('coords')
->getValue();
return $value === NULL || $value === '';
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['coords'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Coordinates'));
$properties['type'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Type of "Yandex Maps" object'));
$properties['placemarks'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Placemarks'));
$properties['lines'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Lines'));
$properties['polygons'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Polygons'));
$properties['hide'] = DataDefinition::create('integer')
->setLabel(new TranslatableMarkup('Placemarks'));
return $properties;
}
}