Ok I see what you are doing. I don’t think there is a way to pass in the data attribute the way you are trying to do it. You have to work round Sugars limitations.
I had to do something very similar recently and the way I went about it was to dynamically add the options to the select in the view via JavaScript.
The first thing to do is create a drop-down field in studio as normal but don’t add any options to it. Then place the field on the editview.
Then I created a new custom view.edit file for my module.
In the view file I do my query and echo out JavaScript from the php based on the results from the query. Basically I create two arrays in JavaScript. One for contacts and the other for users.
/**
* Provides dropdown list of Users or Contacts based on linked project resources
* See modules/AM_ProjectHolidays/showResources.js for on change function
*/
class AM_ProjectHolidaysViewEdit extends ViewEdit
{
public function display()
{
global $mod_strings;
if ($_REQUEST['return_module'] == 'Project'){
$project = new Project();
$project->retrieve($_REQUEST['return_id']);
//Get project resources (users & contacts)
$resources1 = $project->get_linked_beans('project_users_1','User');
$resources2 = $project->get_linked_beans('project_contacts_1','Contact');
//sort into descending order
ksort($resources1);
ksort($resources2);
echo "<script type='text/javascript'>";
echo "var users = [];";
echo "var contacts = [];";
foreach($resources1 as $user){
echo "var user = ['".$user->id."', '".$user->name."', 'User'];";
echo "users.push(user);";
}
foreach($resources2 as $contact){
echo "var user = ['".$contact->id."', '".$contact->name."', 'Contact'];";
echo "contacts.push(user);";
}
echo "</script>";
}
parent::display();
}
Then I created a custom js file and referenced it from the editviewdefs
custom/modules/AM_ProjectHolidays/showResources.js
In the js file I dynamically added the select options to the dropdown based on the arrays I created above.
Here is the example code I wrote for this in Jquery:
function showResources(value){
var resource_type = value.value;
$("#resourse_select").css("visibility", "visible");
if(resource_type == 'User'){
//Clear options before inserting new ones
$("#resourse_select").find('option').remove();
$.each(users, function(index, value){
$("#resourse_select").append($("<option>",{
value: value[0],
text: value[1]
}));
});
showResourcesName();
}
else if(resource_type == 'Contact'){
$("#resourse_select").find('option').remove();
$.each(contacts, function(index, value){
$("#resourse_select").append($("<option>",{
value: value[0],
text: value[1]
}));
});
showResourcesName();
}
}
function showResourcesName(){
//Populate name with the selected user name
var text = $("#resourse_select option:selected" ).text();
$("#name").val(text);
}
You should be able to do something very similar to achieve what you are trying to do.