Basic is good for me, I can't deal with anything extremely sophisticated right now. If I can't get autocomplete working soon my boss will start asking me questions. So yes lets take this a step further.
I followed the instructions: modified page.php, logged into the admin, ran dev/build and entered the URL.
The result was a blank page with some cryptic "I can't let you do that, Dave" message on it. My log outputted this: 'Unknown column 'Page.Test' in 'field list' (http://[root]/admin)" so I don't think I did that right. I'll try that again.
meanwhile my previous code in my module has just decided it wants to be broken. Now it tells me this: "TypeError: jQuery(this).autocomplete is not a function".
[EDIT]
I tried again, this time ensuring that I had the updated code from www.balbus.tk and I used a brand new server with nothing else on it.
balbusautocomplete.php
function setSourceClass($classname) {
SS_Log::log(new Exception('Autocomplete: setSourceClass('.$className.')'), SS_Log::NOTICE);
$this->sourceClass = $classname;
}
/*
* set the sourcefield
*/
function setSourceField($fieldname) {
SS_Log::log(new Exception('Autocomplete: setSourceField('.$fieldName.')'), SS_Log::NOTICE);
$this->sourceField = $fieldname;
}
/*
* Override the existing TextField method and add
* the jQuery requirements and the new field ettributes:
* src - url called by ajax to retrieve the suggestions
* autocomplete - flag setting autocompletion 'on' or 'off'
*/
function Field() {
SS_Log::log(new Exception('Autocomplete: Field()'), SS_Log::NOTICE);
// Note: sometimes loading new JavaScripts in the CMS
// will not work. Looks like the assets/_CombinedFiles
// are the culprits (in that case just remove them)
// Get jQuery from SilverStripe thirdparty
Requirements::javascript(
THIRDPARTY_DIR . '/jquery/jquery.js'
);
// Get the UI from Google (the SilverStripe way)
Requirements::javascript(
'http://ajax.googleapis.com/ajax/libs/jqueryui'.
'/1.8.1/jquery-ui.min.js'
);
Requirements::css(
'http://ajax.googleapis.com/ajax/libs/jqueryui'.
'/1.8.1/themes/base/jquery-ui.css'
);
// NEW: Since inline JavaScript isn't loaded by jQueryOnDemand,
// an external file is needed to set things in motion:
Requirements::javascript(
'balbusessentials/javascript/BalbusAutocompleteField.js'
);
// altered 'text' class to 'text AutocompleteField'
$attributes = array(
'type' => 'text',
'class' => 'text AutocompleteField ' . ($this->extraClass()? $this->extraClass(): ''),
'id' => $this->id(),
'name' => $this->Name(),
'value' => $this->Value(),
'tabindex' => $this->getTabIndex(),
'maxlength' => ($this->maxLength) ? $this->maxLength : null,
'size' => ($this->maxLength) ? min( $this->maxLength, 30 ) : null
);
if($this->disabled) $attributes['disabled'] = 'disabled';
// NEW: add een attribute to enable autocompletion
$attributes['autocomplete'] = 'on';
// NEW: add a sourceClass/sourcefield, if there is one
$querystring = http_build_query(array(
'sclass' => $this->sourceClass,
'sfield' => $this->sourceField
), '', '&');
// NEW: add the current link with the Action=suggest
// parameter added as a new src parameter
$attributes['src'] =
parse_url($this->Link(),PHP_URL_PATH) . '/suggest?' . $querystring;
// Create the tag
SS_Log::log(new Exception('Autocomplete: creating Tag: '.http_build_query($attributes)), SS_Log::NOTICE);
return $this->createTag('input', $attributes);
}
function suggest() {
SS_Log::log(new Exception('Autocomplete: suggest()'), SS_Log::NOTICE);
$items = array(
array('value' => 'London'),
array('value' => 'Paris'),
array('value' => 'Rome')
);
SS_Log::log(new Exception('Autocomplete: suggesting: '.implode($items)), SS_Log::NOTICE);
return json_encode($items);
}
balbusautocomplete.js
alert("autocomplete running");
try{
BalbusAutocompleteField = Class.create();
}catch(exception){
alert("class create failed: "+exception);
}
try{
BalbusAutocompleteField.applyTo('input.AutocompleteField');
}catch(exception){
alert("class apply failed: "+exception);
}
BalbusAutocompleteField.prototype = {
// initialize Autocomplete fields
initialize: function() {
// only initialize once
if (!this.strSource){
// get source string src attribute
this.strSource = jQuery(this).attr('src');
alert("autocomplete source found: "+this.strSource);
// remove src attribute from the textfield
jQuery(this).attr('src', '');
try{
// jQuery Autocomplete
jQuery(this).autocomplete({
source: this.strSource,
minLength: 2
});
}catch(exception){
alert("autocomplete failed: "+exception);
}
alert("autocomplete end");
} else {
alert("source not found");
}
}
};
page.php
public static $db = array(
'Test' => 'Varchar'
);
public static $has_one = array(
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Content.Autocomplete', array(
new BalbusAutocompleteField (
$name = 'Test',
$title = 'Autocomplete testfield'
)
));
return $fields;
}
The link to http://[root]/admin/EditForm/field/Test/suggest?term=par definitely returns the following message:
"I can't handle sub-URLs of a Form object."