GoogleMapFieldType.php 3.96 KB
Newer Older
scot.hubbard's avatar
scot.hubbard committed
1 2 3 4 5 6 7
<?php

namespace Drupal\google_map_field\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
8
use Drupal\Core\StringTranslation\TranslatableMarkup;
scot.hubbard's avatar
scot.hubbard committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

/**
 * Plugin implementation of the 'Google Map' field type.
 *
 * @FieldType(
 *   id = "google_map_field",
 *   label = @Translation("Google Map field"),
 *   description = @Translation("This field stores Google Map fields in the database."),
 *   default_widget = "google_map_field_default",
 *   default_formatter = "google_map_field_default"
 * )
 */
class GoogleMapFieldType extends FieldItemBase {

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field) {
27 28 29
    return [
      'columns' => [
        'name' => [
scot.hubbard's avatar
scot.hubbard committed
30 31 32
          'type' => 'varchar',
          'length' => 128,
          'not null' => FALSE,
33 34
        ],
        'lat' => [
scot.hubbard's avatar
scot.hubbard committed
35 36 37 38
          'type' => 'float',
          'size' => 'big',
          'default' => 0.0,
          'not null' => FALSE,
39 40
        ],
        'lon' => [
scot.hubbard's avatar
scot.hubbard committed
41 42 43 44
          'type' => 'float',
          'size' => 'big',
          'default' => 0.0,
          'not null' => FALSE,
45 46
        ],
        'zoom' => [
scot.hubbard's avatar
scot.hubbard committed
47 48 49
          'type' => 'int',
          'length' => 10,
          'not null' => FALSE,
50 51
        ],
        'type' => [
52 53 54
          'type' => 'varchar',
          'length' => 32,
          'not null' => FALSE,
55 56
        ],
        'width' => [
57 58 59
          'type' => 'varchar',
          'length' => 32,
          'not null' => FALSE,
60 61
        ],
        'height' => [
62 63 64
          'type' => 'varchar',
          'length' => 32,
          'not null' => FALSE,
65 66
        ],
        'marker' => [
67 68 69
          'type' => 'int',
          'length' => 10,
          'not null' => FALSE,
70
        ],
71 72 73 74 75 76 77 78 79 80
        'traffic' => [
          'type' => 'int',
          'length' => 10,
          'not null' => FALSE,
        ],
        'marker_icon' => [
          'type' => 'varchar',
          'length' => 512,
          'not null' => FALSE,
        ],
81
        'controls' => [
82 83 84
          'type' => 'int',
          'length' => 10,
          'not null' => FALSE,
85
        ],
86 87 88 89 90
        'infowindow' => [
          'type' => 'text',
          'size' => 'medium',
          'not null' => FALSE,
        ],
91 92
      ],
    ];
scot.hubbard's avatar
scot.hubbard committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this->get('lat')->getValue();
    return $value === NULL || $value === '';
  }

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['name'] = DataDefinition::create('string')
108
      ->setLabel(new TranslatableMarkup('Map Name'));
scot.hubbard's avatar
scot.hubbard committed
109 110

    $properties['lat'] = DataDefinition::create('string')
111
      ->setLabel(new TranslatableMarkup('Latitude'));
scot.hubbard's avatar
scot.hubbard committed
112 113

    $properties['lon'] = DataDefinition::create('string')
114
      ->setLabel(new TranslatableMarkup('Longitude'));
scot.hubbard's avatar
scot.hubbard committed
115 116

    $properties['zoom'] = DataDefinition::create('string')
117
      ->setLabel(new TranslatableMarkup('Map Zoom'));
scot.hubbard's avatar
scot.hubbard committed
118

119 120 121 122 123 124 125 126 127 128 129 130
    $properties['type'] = DataDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Map Type'));

    $properties['width'] = DataDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Map Width'));

    $properties['height'] = DataDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Map Height'));

    $properties['marker'] = DataDefinition::create('integer')
      ->setLabel(new TranslatableMarkup('Show marker'));

131 132 133 134 135 136
    $properties['traffic'] = DataDefinition::create('integer')
      ->setLabel(new TranslatableMarkup('Traffic Layer'));

    $properties['marker_icon'] = DataDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Custom marker'));

137 138 139
    $properties['controls'] = DataDefinition::create('integer')
      ->setLabel(new TranslatableMarkup('Show controls'));

140 141 142
    $properties['infowindow'] = DataDefinition::create('string')
      ->setLabel(new TranslatableMarkup('InfoWindow message'));

scot.hubbard's avatar
scot.hubbard committed
143 144 145 146
    return $properties;
  }

}