Skip to content

Commit 789a08b

Browse files
authored
gpcc-field-indicators.php: Updated to use new standardized Field Indicators format.
1 parent 670f022 commit 789a08b

File tree

1 file changed

+48
-47
lines changed

1 file changed

+48
-47
lines changed

experimental/gpcc-field-indicators.php

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,66 @@
55
*
66
* Display visual source and target field indicators next to field labels in the form editor.
77
*/
8-
add_filter( 'gform_admin_pre_render', function( $form ) {
8+
add_filter( 'gform_field_content', function( $content, $field ) {
99

10-
if ( ! class_exists( 'GP_Copy_Cat' ) ) {
11-
return $form;
10+
if ( ! GFCommon::is_form_editor() || ! class_exists( 'GP_Copy_Cat' ) || ! $field->formId ) {
11+
return $content;
1212
}
1313

14-
$gpcc = new GP_Copy_Cat();
15-
$gpcc_fields = $gpcc->get_copy_cat_fields( $form );
14+
$gpcc = new GP_Copy_Cat( 'fake.php' );
15+
$gpcc_fields = $gpcc->get_copy_cat_fields( GFAPI::get_form( $field->formId ) );
1616
if ( empty( $gpcc_fields ) ) {
17-
return $form;
17+
return $content;
1818
}
1919

20-
add_filter( 'admin_footer', function() {
21-
?>
22-
<style>
23-
.gpcc-source .gform-field-label:after {
24-
content: 'GPCC: Source';
25-
color: #274524;
26-
margin: 0 0.5rem;
27-
background-color: #edf8ec;
28-
border: 1px solid #d7e8d5;
29-
border-radius: 40px;
30-
float: right;
31-
font-size: 0.6875rem;
32-
font-weight: 600;
33-
padding: 0.1125rem 0.4625rem;
34-
}
35-
36-
.gpcc-target .gform-field-label:after {
37-
content: 'GPCC: Target';
38-
color: #274524;
39-
margin: 0 0.5rem;
40-
background-color: #edf8ec;
41-
border: 1px solid #d7e8d5;
42-
border-radius: 40px;
43-
float: right;
44-
font-size: 0.6875rem;
45-
font-weight: 600;
46-
padding: 0.1125rem 0.4625rem;
47-
}
48-
</style>
49-
<?php
50-
} );
20+
if ( ! has_action( 'admin_footer', 'gpcc_field_indicator_styles' ) ) {
21+
add_filter( 'admin_footer', 'gpcc_field_indicator_styles' );
22+
}
5123

5224
$mappings = array();
5325
foreach ( $gpcc_fields as $_mappings ) {
5426
$mappings = array_merge( $_mappings );
5527
}
5628

57-
foreach ( $form['fields'] as &$field ) {
58-
foreach ( $mappings as $mapping ) {
59-
if ( $field->id == $mapping['source'] ) {
60-
$field->cssClass .= ' gpcc-source';
61-
}
62-
if ( $field->id == $mapping['target'] ) {
63-
$field->cssClass .= ' gpcc-target';
64-
}
29+
$spans = array();
30+
31+
foreach( $mappings as $mapping ) {
32+
if ( $field->id == $mapping['source'] ) {
33+
$spans['source'] = '<span class="gpcc-source gw-field-indicator">GPCC: Source</span>';
34+
}
35+
if ( $field->id == $mapping['target'] ) {
36+
$spans['target'] = '<span class="gpcc-target gw-field-indicator">GPCC: Target</span>';
6537
}
6638
}
6739

68-
return $form;
69-
} );
40+
$search = '<\/label>|<\/legend>';
41+
$replace = sprintf( '\0 %s', implode( '', $spans ) );
42+
$content = preg_replace( "/$search/", $replace, $content, 1 );
43+
44+
return $content;
45+
}, 11, 2 );
46+
47+
function gpcc_field_indicator_styles() {
48+
?>
49+
<style>
50+
.gw-field-indicator {
51+
margin: 0 0 0 0.6875rem;
52+
background-color: #ecedf8;
53+
border: 1px solid #d5d7e9;
54+
border-radius: 40px;
55+
font-size: 0.6875rem;
56+
font-weight: 600;
57+
padding: 0.1125rem 0.4625rem;
58+
vertical-align: text-top;
59+
}
60+
.gw-field-indicator + .gw-field-indicator {
61+
margin-left: 0;
62+
}
63+
.gpcc-source, .gpcc-target {
64+
color: #274524;
65+
background-color: #edf8ec;
66+
border-color: #d7e8d5;
67+
}
68+
</style>
69+
<?php
70+
}

0 commit comments

Comments
 (0)