Developer hooks for GravityMath
Developers can change how GravityMath calculates and what it prints with the filters and actions on this page. Each one goes in a code snippet on your site. The GravityMath developer documentation on gravitykit.dev has the parameters of every hook on this page and the file each one is in.
Live totals in DataTables Views #
With GravityMath 2.14.0 and DataTables 3.15.0 or newer, a [gravitymath scope="visible"] total in a Custom Content widget of a DataTables View follows the table. Each time the table shows other rows, your site works out the total again over the rows on screen. The [gravitymath] shortcode explains the visible scope. The filters below change this for one View or for all of them.
gk/gravitymath/datatables/visible/live
Live totals are on by default. When this filter returns false for a View, the totals in its Custom Content widgets print nothing.
/**
* Turns off live totals in the DataTables View with ID 123.
*
* @param bool $enabled Whether scope="visible" totals follow
* the table.
* @param \GV\View $view The View.
*
* @return bool
*/
add_filter(
'gk/gravitymath/datatables/visible/live',
function ( $enabled, $view ) {
if ( 123 === (int) $view->ID ) {
return false;
}
return $enabled;
},
10,
2
);gk/gravitymath/datatables/visible/max-tokens
One table updates up to 20 totals each time it draws. This filter changes that number, and the totals past it print nothing.
/**
* Lets the DataTables View with ID 123 update up to 30 totals
* on each draw.
*
* @param int $max The most totals one table updates. Default: 20.
* @param \GV\View $view The View.
*
* @return int
*/
add_filter(
'gk/gravitymath/datatables/visible/max-tokens',
function ( $max, $view ) {
if ( 123 === (int) $view->ID ) {
return 30;
}
return $max;
},
10,
2
);gk/gravitymath/datatables/visible/hidden-fields
By default, a total prints nothing when it reads a field that the table doesn’t show as a column. A visitor who shows one row at a time could otherwise read each row’s value of that field from the total. This filter lets such totals follow the table. It receives the IDs of the fields the total reads that aren’t columns, so you can allow only the fields that are safe to show.
/**
* Lets totals in the DataTables View with ID 123 read field 8,
* which the table does not show as a column.
*
* @param bool $allowed Whether the totals follow the table.
* Default: false.
* @param \GV\View $view The View.
* @param string[] $fields The field IDs the totals read that the table
* does not show.
*
* @return bool
*/
add_filter(
'gk/gravitymath/datatables/visible/hidden-fields',
function ( $allowed, $view, $fields ) {
$other_fields = array_diff( $fields, array( '8' ) );
if ( 123 === (int) $view->ID && array() === $other_fields ) {
return true;
}
return $allowed;
},
10,
3
);gk/gravitymath/datatables/visible/max-rows
In a Preloaded (Client-side) table, a total covers up to 1,000 rows by default, or the largest number in the View’s rows-per-page menu if that is higher. When a visitor shows more rows than that, such as with All in the Show entries menu, the total goes blank. This filter changes the limit. It doesn’t affect the Ajax (Server-side) mode.
/**
* Lets totals in the Preloaded DataTables View with ID 123 cover up to
* 5,000 rows.
*
* @param int $max The most rows a total covers. Default: 1000, or
* the largest number in the rows-per-page menu.
* @param \GV\View $view The View.
*
* @return int
*/
add_filter(
'gk/gravitymath/datatables/visible/max-rows',
function ( $max, $view ) {
if ( 123 === (int) $view->ID ) {
return 5000;
}
return $max;
},
10,
2
);Note: Not sure where to add the code? See Where to put code samples.
Shortcode output and formulas #
This prints 0 where a [gravitymath] shortcode has no result, such as a formula GravityMath can’t work out:
/**
* Prints 0 where a [gravitymath] shortcode has no result.
*
* @param string $result The shortcode output.
*
* @return string|int
*/
add_filter( 'gravityview/math/shortcode/output', function ( $result ) {
return ( '' === $result ) ? 0 : $result;
} );| Hook | What it changes |
|---|---|
gravityview/math/shortcode/output | The shortcode’s result, before it’s printed. |
gravityview/math/shortcode/before | The formula, before GravityMath works it out. Using GravityMath inside a GravityView Custom Content field uses it to keep merge tags for GravityMath. |
gravityview/math/shortcode/default_value | The default_value parameter, the number that replaces an empty field value. |
gravityview/math/precision | The most decimal places a result can have, 16 by default. |
gravityview/math/thousands_sep | The thousands separator GravityMath removes from field values before it calculates. By default, it’s the separator of the site’s language. |
gravityview/math/sanitize | The formula after GravityMath cleans it. |
gravityview/math/init (action) | Runs when the math engine starts, so you can add your own functions and constants. |
Table footer calculations #
| Hook | What it changes |
|---|---|
gravityview/ | The [gravitymath] shortcode that GravityMath builds for a column’s result. |
gravityview/ | A column’s result, before it’s shown. |
gravityview/ | How a total of time durations is shown. |
gk/ | Which field types offer a calculation, and which calculation types each one offers. |
How to add field calculations to the table footer explains the settings these hooks work on.
Totals over many entries #
| Hook | What it changes |
|---|---|
gravityview/math/entries/visible | The entries a scope="visible" total covers. |
gravityview/math/aggregate_data/modify_field_value | Gets each field value of a scope="view" or scope="visible" total before it’s added up. For an entry without a value, that value is an empty string. |
gk/gravitymath/scope/form/transform-meta-value | The SQL that turns a saved value into a number for scope="form" totals. By default, it casts the value to a decimal. |
gk/gravitymath/use_db_cache | Whether GravityMath caches the results of totals over a form’s entries. |
An entry without a value in the field reaches the filter as an empty string, and GravityMath leaves it out of averages, minimums, and maximums. Before GravityMath 2.14.1, fields such as Radio Buttons and Option fields passed 0 instead. This counts those entries as 0 for field 7 of form 97, as default_value="0" does in a single shortcode. Totals with scope="form" don’t run this filter.
/**
* Counts entries without a value in field 7 of form 97 as 0,
* so averages divide by every entry and the lowest value can be 0.
*
* @param float|string $value The value GravityMath calculates
* with. An empty string when the
* entry has no value.
* @param string $original_value The value saved in the entry.
* @param string $field_id The field or input ID.
* @param array $entry The entry.
* @return float|string
*/
add_filter(
'gravityview/math/aggregate_data/modify_field_value',
function ( $value, $original_value, $field_id, $entry ) {
$form_id = (int) $entry['form_id'];
if ( 97 === $form_id && '7' === (string) $field_id ) {
return '' === $value ? 0 : $value;
}
return $value;
},
10,
4
);Summaries #
| Hook | What it changes |
|---|---|
gk/gravitymath/summary/entries | The entries a summary calculates over. |
gk/gravitymath/summary/data | The rows of a summary table, before it’s shown. |
gk/gravitymath/summary/headers | The column headings of a summary table. |
gk/gravitymath/summary/template | The template file that prints a summary table. |
How to display summaries using a block, widget, or shortcode explains summaries.
Notices and debugging #
| Hook | What it changes |
|---|---|
gravityview/math/accuracy_message | The warning notice, “* Results may not be accurate.” by default. |
gravityview/math/no_results_message | The error notice, “** No Results Currently Available.” by default. |
gravityview/math/admin_notice | The line that tells administrators why they can see a notice. |
gravityview/math/suppress_errors | Whether notices are hidden. Return true to hide them. |
gravityview/math/debug | Whether a shortcode is added to the debug report. |
gravityview/math/debug_message | The text of each message in the debug report. |
Debugging calculations in GravityMath explains the notices and the debug report.
