-
Notifications
You must be signed in to change notification settings - Fork 45
/
BootstrapFieldList.php
85 lines (67 loc) · 2.13 KB
/
BootstrapFieldList.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
<?php
class BootstrapFieldList extends Extension {
/**
* A list of ignored fields that should not take on Bootstrap transforms
* @var array
*/
protected $ignores = array ();
/**
* Transforms all fields in the FieldList to use Bootstrap templates
* @return FieldList
*/
public function bootstrapify() {
foreach($this->owner as $f) {
$sng = Injector::inst()->get($f->class, true, ['dummy', '']);
if(isset($this->ignores[$f->getName()])) continue;
// if we have a CompositeField, bootstrapify its children
if($f instanceof CompositeField) {
$f->getChildren()->bootstrapify();
continue;
}
// If we have a Tabset, bootstrapify all Tabs
if($f instanceof TabSet) {
$f->Tabs()->bootstrapify();
}
// If we have a Tab, bootstrapify all its Fields
if($f instanceof Tab) {
$f->Fields()->bootstrapify();
}
// If the user has customised the holder template already, don't apply the default one.
if($sng->getFieldHolderTemplate() == $f->getFieldHolderTemplate()) {
$template = "Bootstrap{$f->class}_holder";
if(SSViewer::hasTemplate($template)) {
$f->setFieldHolderTemplate($template);
}
else {
$f->setFieldHolderTemplate("BootstrapFieldHolder");
}
}
// If the user has customised the field template already, don't apply the default one.
if($sng->getTemplate() == $f->getTemplate()) {
foreach(array_reverse(ClassInfo::ancestry($f)) as $className) {
$bootstrapCandidate = "Bootstrap{$className}";
$nativeCandidate = $className;
if(SSViewer::hasTemplate($bootstrapCandidate)) {
$f->setTemplate($bootstrapCandidate);
break;
}
elseif(SSViewer::hasTemplate($nativeCandidate)) {
$f->setTemplate($nativeCandidate);
break;
}
}
}
}
return $this->owner;
}
/**
* Adds this field as ignored. Should not take on boostrap transformation
*
* @param string $field The name of the form field
* @return FieldList
*/
public function bootstrapIgnore($field) {
$this->ignores[$field] = true;
return $this->owner;
}
}