bd81381799cc10bb5186f198c9091401be98fba2
[yaffs-website] / web / core / modules / datetime / src / Plugin / migrate / field / DateField.php
1 <?php
2
3 namespace Drupal\datetime\Plugin\migrate\field;
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 = "datetime",
12  *   type_map = {
13  *     "date" = "datetime",
14  *     "datestamp" =  "timestamp",
15  *     "datetime" =  "datetime",
16  *   },
17  *   core = {6,7}
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 processFieldValues(MigrationInterface $migration, $field_name, $data) {
37     switch ($data['type']) {
38       case 'date':
39         $from_format = 'Y-m-d\TH:i:s';
40         $to_format = 'Y-m-d\TH:i:s';
41         break;
42       case 'datestamp':
43         $from_format = 'U';
44         $to_format = 'U';
45         break;
46       case 'datetime':
47         $from_format = 'Y-m-d H:i:s';
48         $to_format = 'Y-m-d\TH:i:s';
49         break;
50       default:
51         throw new MigrateException(sprintf('Field %s of type %s is an unknown date field type.', $field_name, var_export($data['type'], TRUE)));
52     }
53     $process = [
54       'value' => [
55         'plugin' => 'format_date',
56         'from_format' => $from_format,
57         'to_format' => $to_format,
58         'source' => 'value',
59       ],
60     ];
61
62     $process = [
63       'plugin' => 'sub_process',
64       'source' => $field_name,
65       'process' => $process,
66     ];
67     $migration->mergeProcessOfProperty($field_name, $process);
68   }
69
70 }