This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharchitect.php
90 lines (76 loc) · 3.13 KB
/
architect.php
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
<?php class Architect {
# Architect Plugin Version
private static $version = "0.3.0";
# Blueprint Cache
private static $blueprints = [];
# Fetch all data for a Blueprint as an associative array
public static function blueprint ($template) {
if ( isset(static::$blueprints[$template]) ) {
return static::$blueprints[$template];
} else {
return static::$blueprints[$template] = yaml::decode(kirby()->get('blueprint', $template));
}
}
# Get data for a specific field in a template
public static function field_info ($template, $field) {
$fields = static::blueprint($template)['fields'];
return array_key_exists($field, $fields) ? $fields[$field] : false;
}
# Get all `options` for a select/radio/checkboxes field
public static function field_options ($template, $field) {
$info = static::field_info($template, $field);
return isset($info['options']) ? $info['options'] : [];
}
# Generate a `select` menu from a field's options
public static function field_options_menu ($template, $field, $required = null, $language = null) {
$menu = new Brick('select', [
'id' => $field,
'name' => $field
]);
$options = static::field_info($template, $field);
if ( $required === true || (is_null($required) && isset($options['required']) && $options['required']) ) {
# Don't pad with an empty option
$menu->attr('required', true);
} else {
$menu->append(new Brick('option', '', [
'value' => ''
]));
}
# Append actual selectable ones:
foreach ( static::field_options($template, $field) as $value => $labels ) {
$option = new Brick('option', static::field_option_label($template, $field, $value, $language), [
'value' => $value
]);
# Add the `option` element, unless it's value is present in the global blacklist.
if ( !in_array($value, c::get('architect.blacklist', [])) ) {
if ( r::get($field) == $value ) $option->attr('selected', true);
$menu->append($option);
}
}
return $menu;
}
# Get a localized `label` for a field
public static function field_label ($template, $key, $language = null) {
if ( is_null($language) ) $language = site()->language()->locale();
if ( $field = static::field_info($template, $key) ) {
$label = $field['label'];
if ( is_array($label) ) {
return isset($label[$language]) ? $label[$language] : $label[site()->defaultLanguage()->locale()];
} else {
return $label;
}
}
}
# Get a localized label for an field's value
public static function field_option_label ($template, $field, $value, $language = null) {
if ( is_null($language) ) $language = site()->language()->locale();
# Coerce $value into string— we need to use this to dereference an array in a moment!
if ( !is_string($value) ) $value = (string)$value;
$options = static::field_options($template, $field);
if ( is_array($options[$value]) ) {
return isset($options[$value][$language]) ? $options[$value][$language] : $options[$value][site()->defaultLanguage()->locale()];
} else {
return $options[$value];
}
}
}