-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFieldtypeYaml.module
138 lines (124 loc) · 3.66 KB
/
FieldtypeYaml.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
use \owzim\FieldtypeYaml\FTY;
/**
* Class definition of FieldtypeYaml
*
* A Fieldtype module for ProcessWire CMS/CMF
*
* See README.md for usage instructions.
*
* @author Christian (owzim) Raunitschka <git@raunitschka.de>
* @copyright Copyright (c) 2015, Christian Raunitschka
*
* @version 0.3.0
*
* @filesource
*
* @see https://github.com/owzim/pw-fieldtype-yaml
* @see http://raunitschka.de
* @see http://www.processwire.com
*/
class FieldtypeYaml extends FieldtypeTextarea
{
/**
* $parseCache
*
* @var array
*/
protected static $parseCache = array();
public static function getModuleInfo()
{
return array(
'title' => 'Fieldtype YAML',
'summary' => 'Field that stores YAML data and formats it as an object, when requested.',
'version' => '0.3.0',
'author' => 'owzim',
'icon' => 'code',
'requires' => array('PHP>=5.4','ProcessWire>=2.5.5'),
);
}
/**
* init
*
*/
public function init()
{
parent::init();
require_once(__DIR__ . '/owzim/FieldtypeYaml/Autoloader.php');
spl_autoload_register('owzim\FieldtypeYaml\Autoloader::autoload');
}
/**
* formatValue
*
* @param Page $page
* @param Field $field
* @param string $value
* @return object
*/
public function ___formatValue(Page $page, Field $field, $value)
{
return $this->getCachedValue($page, $field, $value);
}
/**
* getCachedValue
*
* @param Page $page
* @param Field $field
* @param string $value
* @return object
*/
public function getCachedValue(Page $page, Field $field, $value)
{
$cacheKey = "{$page->id}_{$field->id}";
if (!isset(self::$parseCache[$cacheKey])) {
return self::$parseCache[$cacheKey] = $this->parseValue($page, $field, $value);
} else {
return self::$parseCache[$cacheKey];
}
}
/**
* parseValue
*
* @param Page $page
* @param Field $field
* @param string $value
* @return object
*/
public function ___parseValue(Page $page, Field $field, $value)
{
return FTY::parseYAML($value, $field->yamlParseAs, $field->get('label|name'));
}
/**
* getConfigInputfields
*
* @param Field $field
* @return InputfieldWrapper
*/
public function ___getConfigInputfields(Field $field)
{
$forbidden = array('contentType', 'textformatters');
$parentInputfields = parent::___getConfigInputfields($field);
$inputfields = new InputfieldWrapper();
foreach ($parentInputfields as $inputfield) {
if (!in_array($inputfield->name, $forbidden)) {
$inputfields->append($inputfield);
}
}
$inputfields->add(array(
'type' => 'InputfieldRadios',
'name' => 'yamlParseAs',
'label' => $this->_('Parse as'),
'options' => array(
FTY::PARSE_AS_WIRE_DATA => $this->_('WireArray/WireData'),
FTY::PARSE_AS_OBJECT => $this->_('Object'),
FTY::PARSE_AS_ASSOC => $this->_('Associative Array'),
),
'value' => !isset($field->yamlParseAs) ? FTY::DEFAULT_PARSE_AS : (int) $field->yamlParseAs,
'optionColumns' => 1,
'description' => $this->_(
"If WireArray/WireData is chosen, the parsed array/object has full support for things like **\$person->get('title|name')** or **\$people->find('age>20')**"
),
));
return $inputfields;
}
}