Skip to content

Instantly share code, notes, and snippets.

@rafaehlers
Created March 25, 2024 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaehlers/803c452c4fe03c16cf8c4d9aa92899ac to your computer and use it in GitHub Desktop.
Save rafaehlers/803c452c4fe03c16cf8c4d9aa92899ac to your computer and use it in GitHub Desktop.
Refreshes a View using DataTables after inline editing a field
<?php // DO NOT COPY THIS LINE - Code shared by Rob Davis
namespace GravityView_Refresher_26096;
class Refresher {
// Form ID in question.
public $form_id = 18;
// View ID in question.
public $view_id = 26096;
// Field IDs in question.
public $field_id = array( 227, 228 );
public function __construct() {
// Inline Edit entry if type=select updated.
add_filter( 'gravityview-inline-edit/entry-updated/select', array( $this, 'gvdtr_get_fresh_entry_data' ), 10, 5 );
// Modify the entry output before the request is returned.
add_filter( 'gravityview/datatables/output/entry', array( $this, 'gvdtr_add_wrappers_to_the_fields' ), 10, 3 );
// Modify whether to use the cache or not.
add_filter( 'gravityview_use_cache', array( $this, 'gvdtr_stop_cache' ) );
}
/**
* update Ajax response with fresh data
*
* @param bool|WP_Error $update_result True: the entry has been updated by Gravity Forms or WP_Error if there was a problem.
* @param array $entry The Entry Object that's been updated.
* @param int $form_id The Form ID.
* @param GF_Field|null $gf_field The field that's been updated, or null if no field exists (for entry meta).
* @param array $original_entry Original entry, before being updated.
*/
public function gvdtr_get_fresh_entry_data( $update_result, $entry, $form_id, $gf_field, $original_entry ) {
if ( $this->form_id === intval( $form_id ) ) {
if ( ! is_wp_error( $update_result ) ) {
$entry = \GFAPI::get_entry( $entry['id'] );
// Here we add all the fields we want to be updated dynamically.
if ( ! empty( $this->form_id ) ) {
$update_result = array();
foreach ( $this->field_id as $field_id ) {
$update_result[] = array(
'selector' => 'span.dynamic-field[data-entry="' . $entry['id'] . '"][data-field="' . $field_id . '"]',
'has_calculation' => true,
'value' => $entry[ $field_id ],
'data' => array(
'display_value' => $entry[ $field_id ],
),
);
}
}
}
}
return $update_result;
}
/**
* Add wrappers on the items in the table to JavaScript can find them
*
* @param array $temp Array of values for the entry, one item per field being rendered by \GV\Field_Renderer().
* @param \GV\View $view Current View being processed.
* @param array $entry Current Gravity Forms entry array.
*/
public function gvdtr_add_wrappers_to_the_fields( $temp, $view, $entry ) {
if ( $this->view_id === $view->ID ) {
if ( ! empty( $view->fields->all() ) ) {
foreach ( $view->fields->all() as $key => $view_field ) {
if ( in_array( intval( $view_field->id ), $this->field_id ) ) {
// Wrap the item into some HTML so it can be recognized by JavaScript.
$temp[ $key ] = '<span class="dynamic-field" data-entry="'. $entry['id'] . '" data-field="'. $view_field->id. '">' . $temp[ $key ] . '</span>';
}
}
}
}
return $temp;
}
/**
* Simply returns `false`
*/
public function gvdtr_stop_cache() {
return false;
}
}
new \GravityView_Refresher_26096\Refresher();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment