Version 1
[yaffs-website] / web / core / modules / datetime / src / Plugin / migrate / field / d6 / DateField.php
1 <?php
2
3 namespace Drupal\datetime\Plugin\migrate\field\d6;
4
5 use Drupal\migrate\Plugin\MigrationInterface;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
8
9 /**
10  * @MigrateField(
11  *   id = "date",
12  *   type_map = {
13  *     "date" = "datetime",
14  *     "datestamp" =  "timestamp",
15  *     "datetime" =  "datetime",
16  *   },
17  *   core = {6}
18  * )
19  */
20 class DateField extends FieldPluginBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public function getFieldWidgetMap() {
26     return [
27       'date' => 'datetime_default',
28       'datetime' => 'datetime_default',
29       'datestamp' => 'datetime_timestamp',
30     ];
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function getFieldFormatterMap() {
37     // See d6_field_formatter_settings.yml and
38     // FieldPluginBase::processFieldFormatter().
39     return [];
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function processFieldValues(MigrationInterface $migration, $field_name, $data) {
46     switch ($data['type']) {
47       case 'date':
48         $from_format = 'Y-m-d\TH:i:s';
49         $to_format = 'Y-m-d\TH:i:s';
50         break;
51       case 'datestamp':
52         $from_format = 'U';
53         $to_format = 'U';
54         break;
55       case 'datetime':
56         $from_format = 'Y-m-d H:i:s';
57         $to_format = 'Y-m-d\TH:i:s';
58         break;
59       default:
60         throw new MigrateException(sprintf('Field %s of type %s is an unknown date field type.', $field_name, var_export($data['type'], TRUE)));
61     }
62     $process = [
63       'value' => [
64         'plugin' => 'format_date',
65         'from_format' => $from_format,
66         'to_format' => $to_format,
67         'source' => 'value',
68       ],
69     ];
70
71     $process = [
72       'plugin' => 'iterator',
73       'source' => $field_name,
74       'process' => $process,
75     ];
76     $migration->mergeProcessOfProperty($field_name, $process);
77   }
78
79 }