Skip to content

Instantly share code, notes, and snippets.

@aimahdi
Last active November 16, 2022 06:39
Show Gist options
  • Select an option

  • Save aimahdi/218198d1ecd401ba1847bdf10a154a88 to your computer and use it in GitHub Desktop.

Select an option

Save aimahdi/218198d1ecd401ba1847bdf10a154a88 to your computer and use it in GitHub Desktop.
add_filter('fluentform_rendering_field_data_select', function ($data, $form) {
$targetFormID = 2;
//google sheet shared as CSV link
$csvUrl = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vSQM0ay3KgEmTp64M7ZZ5n5QXHDsSzaaJcVCuIHBvgMIw32foNpdfd4jNnUMua8b5Mavb6E4jox_nic/pub?output=csv';
$columName = 'Opex Vessel Categories'; // 'Players' is the column name
$uniqueData = false; // remove duplicate values
if ($form->id != $targetFormID) {
return $data;
}
// check if the name attribute is 'dropdown' , it is by default dropdown for the first dropdown input
if (\FluentForm\Framework\Helpers\ArrayHelper::get($data, 'attributes.name') != 'dropdown') {
return $data;
}
$result=[] ;
if (!class_exists('CSVParser')) {
require_once(FLUENTFORMPRO_DIR_PATH . 'libs/CSVParser/CSVParser.php');
}
$csvParser = new \CSVParser;
$content = file_get_contents($csvUrl);
$csvParser->load_data($content);
$rows = $csvParser->parse($csvParser->find_delimiter());
if(!$rows) {
return $data;
}
$headers = array_shift($rows); // remove the first item
$headerIndex = array_search($columName, $headers);
foreach ($rows as $row) {
if(!empty($row[$headerIndex])) {
$value = "";
for($i=0; $i<sizeof($row); $i++){
$value = $value." ".$row[$i];
}
$result[]= [
"label" => $value,
"value" => $row[$headerIndex],
"calc_value" => ""
];
}
}
$result = ($uniqueData === true) ? array_map("unserialize", array_unique(array_map("serialize", $result))) : $result;
// Merging with existing options here
$data['settings']['advanced_options'] = array_merge($data['settings']['advanced_options'], $result );
return $data;
}, 10, 2);
@Taliabiasevi
Copy link
Copy Markdown

Hi Aimahdi!

I have a list that I want to use as a dropdown in 3 forms.
How would you use this code to include the dropdown in several forms? Is it possible? Or should I repeat the whole code entirely and change the Form ID?

Many thanks!!!

@aimahdi
Copy link
Copy Markdown
Author

aimahdi commented Nov 16, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment