Version 1
[yaffs-website] / web / core / modules / field / src / Plugin / migrate / process / d6 / FieldFormatterSettingsDefaults.php
1 <?php
2
3 namespace Drupal\field\Plugin\migrate\process\d6;
4
5 use Drupal\migrate\ProcessPluginBase;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\Row;
8
9 /**
10  * Set the default field settings.
11  *
12  * @MigrateProcessPlugin(
13  *   id = "field_formatter_settings_defaults"
14  * )
15  */
16 class FieldFormatterSettingsDefaults extends ProcessPluginBase {
17
18   /**
19    * {@inheritdoc}
20    *
21    * Set field formatter settings when the map didn't map: for date
22    * formatters, the fallback format, for everything else, empty array.
23    */
24   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
25     // If the 1 index is set then the map missed.
26     if (isset($value[1])) {
27       $module = $row->getSourceProperty('module');
28       if ($module === 'date') {
29         $value = ['format_type' => 'fallback'];
30       }
31       elseif ($module === 'number') {
32         // We have to do the lookup here in the process plugin because for
33         // number we need to calculated the settings based on the type not just
34         // the module which works well for other field types.
35         return $this->numberSettings($row->getDestinationProperty('options/type'), $value[1]);
36       }
37       else {
38         $value = [];
39       }
40     }
41     return $value;
42   }
43
44   /**
45    * @param string $type
46    *   The field type.
47    * @param $format
48    *   The format selected for the field on the display.
49    *
50    * @return array
51    *   The correct default settings.
52    *
53    * @throws \Drupal\migrate\MigrateException
54    */
55   protected function numberSettings($type, $format) {
56     $map = [
57       'number_decimal' => [
58         'us_0' => [
59           'scale' => 0,
60           'decimal_separator' => '.',
61           'thousand_separator' => ',',
62           'prefix_suffix' => TRUE,
63         ],
64         'us_1' => [
65           'scale' => 1,
66           'decimal_separator' => '.',
67           'thousand_separator' => ',',
68           'prefix_suffix' => TRUE,
69         ],
70         'us_2' => [
71           'scale' => 2,
72           'decimal_separator' => '.',
73           'thousand_separator' => ',',
74           'prefix_suffix' => TRUE,
75         ],
76         'be_0' => [
77           'scale' => 0,
78           'decimal_separator' => ',',
79           'thousand_separator' => '.',
80           'prefix_suffix' => TRUE,
81         ],
82         'be_1' => [
83           'scale' => 1,
84           'decimal_separator' => ',',
85           'thousand_separator' => '.',
86           'prefix_suffix' => TRUE,
87         ],
88         'be_2' => [
89           'scale' => 2,
90           'decimal_separator' => ',',
91           'thousand_separator' => '.',
92           'prefix_suffix' => TRUE,
93         ],
94         'fr_0' => [
95           'scale' => 0,
96           'decimal_separator' => ',',
97           'thousand_separator' => ' ',
98           'prefix_suffix' => TRUE,
99         ],
100         'fr_1' => [
101           'scale' => 1,
102           'decimal_separator' => ',',
103           'thousand_separator' => ' ',
104           'prefix_suffix' => TRUE,
105         ],
106         'fr_2' => [
107           'scale' => 2,
108           'decimal_separator' => ',',
109           'thousand_separator' => ' ',
110           'prefix_suffix' => TRUE,
111         ],
112       ],
113       'number_integer' => [
114         'us_0' => [
115           'thousand_separator' => ',',
116           'prefix_suffix' => TRUE,
117         ],
118         'be_0' => [
119           'thousand_separator' => '.',
120           'prefix_suffix' => TRUE,
121         ],
122         'fr_0' => [
123           'thousand_separator' => ' ',
124           'prefix_suffix' => TRUE,
125         ],
126       ],
127     ];
128
129     return isset($map[$type][$format]) ? $map[$type][$format] : [];
130   }
131
132 }