PluginProbe
Advanced Forms for ACF / trunk
Advanced Forms for ACF vtrunk
1.9.0 1.9.1 1.9.2.1 1.9.3 1.9.3.1 1.9.3.2 1.9.3.3 1.9.3.4 1.9.3.5 1.9.3.6 1.9.3.7 1.9.3.8 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.3.1 1.0.3.2 1.0.3.3 1.0.4 1.1.0 1.1.1 1.2.0 1.3.0 All 51 releases
advanced-forms / acf / fields / af-render-content-field.php

af-render-content-field.php in Advanced Forms for ACF trunk, at acf/fields/af-render-content-field.php

109 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * A custom private ACF field used to render the results of a callback passed to it. This is merely a convenient wrapper
5 * that allows us to render any type of markeup we like when building the Advanced Forms UI. It's particularly useful
6 * when we want to set up sub fields on a repeater or flexi-content field and have those sub fields render display
7 * markup or some arbitrary field using acf_render_field_wrap().
8 *
9 * Note, if `label` AND `instructions` are both empty, the containing label element will be hidden by CSS.
10 *
11 * Usage example — passing callback as a value:
12 * [
13 * 'key' => 'field_some_field_key',
14 * 'type' => 'af_render_content',
15 * 'name' => 'some_field_name',
16 * 'label' => 'Some label here',
17 * 'instructions' => 'Some instructions go here',
18 * 'value' => function( $field ) {
19 * echo 'The content here...';
20 * acf_render_field_wrap([
21 * 'label' => 'Test',
22 * 'type' => 'textarea',
23 * 'name' => 'test',
24 * ]);
25 * },
26 * ]
27 *
28 * Usage example — passing callback as value parameter:
29 * [
30 * 'key' => 'field_some_field_key',
31 * 'type' => 'af_render_content',
32 * 'name' => 'some_field_name',
33 * 'label' => 'Some label here',
34 * 'instructions' => 'Some instructions go here',
35 * 'value' => [
36 * 'some' => 'data',
37 * 'render_function' => function( $field ) {
38 * echo 'The content here...';
39 * acf_render_field_wrap([
40 * 'label' => 'Test',
41 * 'type' => 'textarea',
42 * 'name' => 'test',
43 * ]);
44 * },
45 * ],
46 * ]
47 *
48 * Usage example — passing callback as field arg:
49 * [
50 * 'key' => 'field_some_field_key',
51 * 'type' => 'af_render_content',
52 * 'name' => 'some_field_name',
53 * 'label' => 'Some label here',
54 * 'instructions' => 'Some instructions go here',
55 * 'render_function' => function( $field ) {
56 * echo 'The content here...';
57 * acf_render_field_wrap([
58 * 'label' => 'Test',
59 * 'type' => 'textarea',
60 * 'name' => 'test',
61 * ]);
62 * },
63 * ]
64 */
65 class AF_Render_Content_Field extends acf_field {
66
67 function __construct() {
68 $this->name = 'af_render_content';
69 $this->label = _x( 'Render', 'noun', 'advanced-forms' );
70 $this->public = false;
71 $this->defaults = [];
72 $this->category = 'Forms';
73 parent::__construct();
74 add_filter( 'acf/field_wrapper_attributes', [ $this, '_add_hidden_label_class' ], 10, 3 );
75 }
76
77 function render_field( $field ) {
78
79 // First, check to see if the field value is callable.
80 // Next, check to see if the field value has a 'render_function' key with a callable assigned.
81 // Finally, check to see if the field array has a 'render_function' key with a callable assigned.
82 // Call the first callable found. This allows a lot of flexibility, particularly when using repeaters.
83 if ( ! empty( $field['value'] ) && is_callable( $field['value'] ) ) {
84 call_user_func( $field['value'], $field );
85
86 } else if ( ! empty( $field['value']['render_function'] ) && is_callable( $field['value']['render_function'] ) ) {
87 call_user_func( $field['value']['render_function'], $field );
88
89 } else if ( ! empty( $field['render_function'] ) && is_callable( $field['render_function'] ) ) {
90 call_user_func( $field['render_function'], $field );
91 }
92 }
93
94 public function _add_hidden_label_class( $wrapper, $field ) {
95 if ( $field['type'] !== $this->name ) {
96 return $wrapper;
97 }
98
99 // This could use further refinement to cover the various scenarios around description placement, etc.
100 if ( empty( $field['label'] ) and empty( $field['instructions'] ) ) {
101 $wrapper['class'] .= ' af-field-hide-label';
102 }
103
104 return $wrapper;
105 }
106
107 }
108
109 acf_register_field_type( new AF_Render_Content_Field() );