Last active
May 14, 2024 11:48
-
-
Save saifsultanc/f285579996b1a0f2bdde5120d1d9f774 to your computer and use it in GitHub Desktop.
gp-nested-forms /gpnf-hide-total-column-for-non-logged-in-users.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Gravity Perks // Nested Forms // Hide Total Column for Non-Logged in Users | |
* https://gravitywiz.com/documentation/gravity-forms-nested-forms/ | |
*/ | |
add_filter( 'gp_template_output_nested-entries', function( $markup, $located_template, $load, $args ) { | |
ob_start(); | |
/** | |
* @var array $nested_fields An array of GF_Field objects. | |
* @var array $nested_form The form object of the nested form. | |
* @var array $nested_field_ids An array of nested field IDs. | |
* @var array $entries An array of child entries submitted from the current Nested Form field. | |
* @var array $labels An array of labels used in this template. | |
* @var array $aria_labels An array of labels used for screen readers. | |
* @var array $actions An array of HTML strings used to display field actions. | |
* @var bool $enable_duplication Can child entries be duplicated? | |
* @var int $column_count The number of columns. | |
* @var string $add_button The markup for the "Add Entry" button. | |
* @var string $add_button_message The markup for situational messages related to the "Add Entry" button. | |
*/ | |
extract( $args ); | |
if ( ! has_action( 'wp_footer', 'gpnf_hec_output_script' ) ) { | |
add_action( 'wp_footer', 'gpnf_hec_output_script' ); | |
add_action( 'gform_preview_footer', 'gpnf_hec_output_script' ); | |
} | |
?> | |
<div class="gpnf-nested-entries-container ginput_container"> | |
<table class="gpnf-nested-entries"> | |
<thead> | |
<tr> | |
<?php foreach ( $nested_fields as $nested_field ) : ?> | |
<th class="gpnf-field-<?php echo $nested_field['id']; ?>" data-bind="visible: window.gpnfHecColumnHasValue( '<?php echo $nested_field['id']; ?>', entries() );"> | |
<?php | |
if ( $nested_field->type == 'total' && ! is_user_logged_in() && ! rgget( 'uid' ) ) { | |
continue; | |
} | |
echo GFCommon::get_label( $nested_field ); | |
?> | |
</th> | |
<?php endforeach; ?> | |
<th class="gpnf-row-actions"> </th> | |
</tr> | |
</thead> | |
<tbody data-bind="visible: entries().length, foreach: entries"> | |
<tr data-bind="attr: { 'data-entryid': id }"> | |
<?php foreach ( $nested_fields as $nested_field ) : | |
if ( $nested_field->type == 'total' && ! is_user_logged_in() && ! rgget( 'uid' ) ) { | |
continue; | |
} ?> | |
<td class="gpnf-field" data-bind="html: f<?php echo $nested_field['id']; ?>.label,visible: window.gpnfHecColumnHasValue( '<?php echo $nested_field['id']; ?>', $parent.entries() );"> </td> | |
<?php endforeach; ?> | |
<td class="gpnf-row-actions"> | |
<ul> | |
<li class="edit"><a href="#" data-bind="click: $parent.editEntry"><?php echo $labels['edit_entry']; ?></a></li> | |
<li class="delete"><a href="#" data-bind="click: $parent.deleteEntry"><?php echo $labels['delete_entry']; ?></a></li> | |
</ul> | |
</td> | |
</tr> | |
</tbody> | |
<tbody data-bind="visible: entries().length <= 0"> | |
<tr class="gpnf-no-entries" data-bind="visible: entries().length <= 0"> | |
<td colspan="<?php echo $column_count; ?>"> | |
<?php echo $labels['no_entries']; ?> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
<?php echo $add_button; ?> | |
</div> | |
<?php | |
return ob_get_clean(); | |
}, 10, 5 ); | |
function gpnf_hec_output_script() { | |
?> | |
<script> | |
function gpnfHecColumnHasValue( fieldId, entries ) { | |
var hasValue = false; | |
jQuery.each( entries, function( i, entry ) { | |
if( ( entry[ fieldId ] && entry[ fieldId ].label ) || ( entry[ `f${fieldId}` ] && entry[ `f${fieldId}` ].label ) ) { | |
hasValue = true; | |
} | |
} ); | |
return hasValue; | |
} | |
</script> | |
<?php | |
} | |
function gpnf_hec_column_has_value( $field_id, $entries ) { | |
foreach ( $entries as $entry ) { | |
if ( rgar( $entry, $field_id ) ) { | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment