Skip to content

Commit adf2dde

Browse files
authored
enhance(directives): Add support for customizing the @field format value (Fixes #43) (#97)
2 parents 3e15669 + 6efe9b7 commit adf2dde

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

src/Directives/Acf.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,26 @@
6363
$expression = Util::parse($expression);
6464

6565
if (Util::isIdentifier($expression->get(2))) {
66-
return "<?php echo get_field({$expression->get(0)}, {$expression->get(2)})[{$expression->get(1)}]; ?>";
66+
if (empty($expression->get(3))) {
67+
$expression->put(3, 'true');
68+
}
69+
70+
return "<?php echo get_field({$expression->get(0)}, {$expression->get(2)}, {$expression->get(3)})[{$expression->get(1)}]; ?>";
6771
}
6872

6973
if (Util::isIdentifier($expression->get(1))) {
70-
return "<?php echo get_field({$expression->get(0)}, {$expression->get(1)}); ?>";
74+
if (empty($expression->get(2))) {
75+
$expression->put(2, 'true');
76+
}
77+
78+
return "<?php echo get_field({$expression->get(0)}, {$expression->get(1)}, {$expression->get(2)}); ?>";
79+
}
80+
81+
if (empty($expression->get(2))) {
82+
$expression->put(2, 'true');
7183
}
7284

73-
return "<?php echo get_field({$expression->get(0)})[{$expression->get(1)}]; ?>";
85+
return "<?php echo get_field({$expression->get(0)}, null, {$expression->get(2)})[{$expression->get(1)}]; ?>";
7486
}
7587

7688
return "<?php echo get_field({$expression}); ?>";

tests/Unit/AcfTest.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,39 +86,71 @@
8686

8787
$compiled = $this->compile($directive);
8888

89-
expect($compiled)->toEqual("<?php echo get_field('item')['key']; ?>");
89+
expect($compiled)->toEqual("<?php echo get_field('item', null, true)['key']; ?>");
9090
});
9191

9292
it('compiles correctly with post ID', function () {
9393
$directive = "@field('item', 1)";
9494

9595
$compiled = $this->compile($directive);
9696

97-
expect($compiled)->toEqual("<?php echo get_field('item', 1); ?>");
97+
expect($compiled)->toEqual("<?php echo get_field('item', 1, true); ?>");
9898
});
9999

100100
it('compiles correctly with key and post ID', function () {
101101
$directive = "@field('item', 'key', 1)";
102102

103103
$compiled = $this->compile($directive);
104104

105-
expect($compiled)->toEqual("<?php echo get_field('item', 1)['key']; ?>");
105+
expect($compiled)->toEqual("<?php echo get_field('item', 1, true)['key']; ?>");
106+
});
107+
108+
it('compiles correctly with post ID and formatting', function () {
109+
$directive = "@field('item', 1, false)";
110+
111+
$compiled = $this->compile($directive);
112+
113+
expect($compiled)->toEqual("<?php echo get_field('item', 1, false); ?>");
114+
});
115+
116+
it('compiles correctly with key, post ID and formatting', function () {
117+
$directive = "@field('item', 'key', 1, false)";
118+
119+
$compiled = $this->compile($directive);
120+
121+
expect($compiled)->toEqual("<?php echo get_field('item', 1, false)['key']; ?>");
106122
});
107123

108124
it('compiles correctly with post object', function () {
109125
$directive = "@field('item', \$post->ID)";
110126

111127
$compiled = $this->compile($directive);
112128

113-
expect($compiled)->toEqual("<?php echo get_field('item', \$post->ID); ?>");
129+
expect($compiled)->toEqual("<?php echo get_field('item', \$post->ID, true); ?>");
114130
});
115131

116132
it('compiles correctly with key and post object', function () {
117133
$directive = "@field('item', 'key', \$post->ID)";
118134

119135
$compiled = $this->compile($directive);
120136

121-
expect($compiled)->toEqual("<?php echo get_field('item', \$post->ID)['key']; ?>");
137+
expect($compiled)->toEqual("<?php echo get_field('item', \$post->ID, true)['key']; ?>");
138+
});
139+
140+
it('compiles correctly with post object and formatting', function () {
141+
$directive = "@field('item', \$post->ID, false)";
142+
143+
$compiled = $this->compile($directive);
144+
145+
expect($compiled)->toEqual("<?php echo get_field('item', \$post->ID, false); ?>");
146+
});
147+
148+
it('compiles correctly with key, post object and formatting', function () {
149+
$directive = "@field('item', 'key', \$post->ID, false)";
150+
151+
$compiled = $this->compile($directive);
152+
153+
expect($compiled)->toEqual("<?php echo get_field('item', \$post->ID, false)['key']; ?>");
122154
});
123155
});
124156

0 commit comments

Comments
 (0)