1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Models; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\FieldModel; |
7
|
|
|
use Craft\FieldGroupModel; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Schematic Field Model. |
11
|
|
|
* |
12
|
|
|
* A schematic field model for mapping data |
13
|
|
|
* |
14
|
|
|
* @author Nerds & Company |
15
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
16
|
|
|
* @license MIT |
17
|
|
|
* |
18
|
|
|
* @link http://www.nerds.company |
19
|
|
|
*/ |
20
|
|
|
class Field |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @return FieldFactory |
24
|
|
|
*/ |
25
|
|
|
protected function getFieldFactory() |
26
|
|
|
{ |
27
|
|
|
return Craft::app()->schematic_fields->getFieldFactory(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param FieldModel $field |
32
|
|
|
* @param $includeContext |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
public function getDefinition(FieldModel $field, $includeContext) |
37
|
|
|
{ |
38
|
|
|
$definition = [ |
39
|
|
|
'name' => $field->name, |
40
|
|
|
'required' => $field->required, |
41
|
|
|
'instructions' => $field->instructions, |
42
|
|
|
'translatable' => $field->translatable, |
43
|
|
|
'type' => $field->type, |
44
|
|
|
'settings' => $field->settings, |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
if ($includeContext) { |
48
|
|
|
$definition['context'] = $field->context; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (isset($definition['settings']['sources'])) { |
52
|
|
|
$definition['settings']['sources'] = Craft::app()->schematic_sources->getMappedSources($field->type, $definition['settings']['sources'], 'id', 'handle'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (isset($definition['settings']['source'])) { |
56
|
|
|
$definition['settings']['source'] = Craft::app()->schematic_sources->getSource($field->type, $definition['settings']['source'], 'id', 'handle'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $definition; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $fieldDefinition |
64
|
|
|
* @param FieldModel $field |
65
|
|
|
* @param string $fieldHandle |
66
|
|
|
* @param FieldGroupModel|null $group |
67
|
|
|
* @param bool $force |
68
|
|
|
*/ |
69
|
|
|
public function populate(array $fieldDefinition, FieldModel $field, $fieldHandle, FieldGroupModel $group = null, $force = false) |
70
|
|
|
{ |
71
|
|
|
$field->name = $fieldDefinition['name']; |
72
|
|
|
$field->handle = $fieldHandle; |
73
|
|
|
$field->required = $fieldDefinition['required']; |
74
|
|
|
$field->translatable = $fieldDefinition['translatable']; |
75
|
|
|
$field->instructions = $fieldDefinition['instructions']; |
76
|
|
|
$field->type = $fieldDefinition['type']; |
77
|
|
|
$field->settings = $fieldDefinition['settings']; |
78
|
|
|
|
79
|
|
|
if ($group) { |
80
|
|
|
$field->groupId = $group->id; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (isset($fieldDefinition['settings']['sources'])) { |
84
|
|
|
$settings = $fieldDefinition['settings']; |
85
|
|
|
$settings['sources'] = Craft::app()->schematic_sources->getMappedSources($field->type, $settings['sources'], 'handle', 'id'); |
86
|
|
|
$field->settings = $settings; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (isset($fieldDefinition['settings']['source'])) { |
90
|
|
|
$settings = $fieldDefinition['settings']; |
91
|
|
|
$settings['source'] = Craft::app()->schematic_sources->getSource($field->type, $settings['source'], 'handle', 'id'); |
92
|
|
|
$field->settings = $settings; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|