FormBuilderProcessor::saveGoogleSheetsReady() method
Hook called before spreadsheet row saved, can optionally modify $event->return (spreadsheet row)
Hook example: Convert values before storage in spreadsheet. When sending entries to Google Sheets, some types of values won't be recognized by Google Sheets and may need conversion before they are sent to the spreadsheet.
A. Demonstrates converting a multi-value field (whether Checkboxes, SelectMultiple, AsmSelect, etc.) named 'favorite_colors' to a comma-separated string (CSV) for storage in one column.
B. Demonstrates converting a single selection Page field (represented by an ID integer) named 'country' to a Page title for storage in one column.
C. Demonstrates converting a multi-value selection Page field (represented by an array of page IDs) named 'categories' to newline-separated string of Page titles.
This protected method is for hooks to monitor and it is likely not intended to be called directly.
Example
$wire->addHookAfter('FormBuilderProcessor::saveGoogleSheetsReady', function($event) {
$row = $event->return; // spreadsheet row (array) that will be saved
$entry = $event->arguments(2); // entry (array) that row is based upon
foreach($row as $name => $value) {
if($name === 'favorite_colors' && is_array($value)) {
// A. convert check box array values to CSV string
$value = implode(', ', $value);
} else if($name === 'country' && ctype_digit("$value")) {
// B. convert page ID to page title
$value = $value ? $event->wire()->pages->get((int) $value)->title : '';
} else if($name === 'categories' && is_array($value)) {
// C. convert array of page IDs to newline-separated page titles
$value = count($value) ? $event-wire()->pages->getByID($value) : null;
$value = $value ? $value->implode("\n", "title") : '';
}
$row[$name] = $value;
}
$event->return = $row;
}
Internal usage
$array = $processor->saveGoogleSheetsReady(GoogleSheets $sheets, array $row, array $data);
Arguments
Name | Type(s) | Description |
---|---|---|
$sheets | GoogleSheets | |
$row | array | Spreadsheet row that will be saved |
$data | array | FormBuilder entry data |
Return value
array
Returns row of data that will be saved to Google sheets or empty if it should not be saved
Hooking $processor→saveGoogleSheetsReady(…)
You can add your own hook events that are executed either before or after the $processor->saveGoogleSheetsReady(…)
method is executed. Examples of both are included below. A good place for hook code such as this is in your /site/ready.php file.
Hooking before
The 'before' hooks are called immediately before each $processor->saveGoogleSheetsReady(…)
method call is executed. This type of hook is especially useful for modifying arguments before they are sent to the method.
$this->addHookBefore('FormBuilderProcessor::saveGoogleSheetsReady', function(HookEvent $event) {
// Get the object the event occurred on, if needed
$FormBuilderProcessor = $event->object;
// Get values of arguments sent to hook (and optionally modify them)
$sheets = $event->arguments(0);
$row = $event->arguments(1);
$data = $event->arguments(2);
/* Your code here, perhaps modifying arguments */
// Populate back arguments (if you have modified them)
$event->arguments(0, $sheets);
$event->arguments(1, $row);
$event->arguments(2, $data);
});
Hooking after
The 'after' hooks are called immediately after each $processor->saveGoogleSheetsReady(…)
method call is executed. This type of hook is especially useful for modifying the value that was returned by the method call.
$this->addHookAfter('FormBuilderProcessor::saveGoogleSheetsReady', function(HookEvent $event) {
// Get the object the event occurred on, if needed
$FormBuilderProcessor = $event->object;
// An 'after' hook can retrieve and/or modify the return value
$return = $event->return;
// Get values of arguments sent to hook (if needed)
$sheets = $event->arguments(0);
$row = $event->arguments(1);
$data = $event->arguments(2);
/* Your code here, perhaps modifying the return value */
// Populate back return value, if you have modified it
$event->return = $return;
});
$processor methods and properties
API reference based on ProcessWire core version 3.0.251