Version: current

     1 <?php
     2
     3 /**
     4  * Renders a textfield that will return a set of results from a callback URL
     5  * as the user types. Useful for search forms.
     6  *
     7  * By default, the field will look on the current controller for a function named
     8  * "autocomplte", but it can be passed a custom function or a custom URL. For globally
     9  * accessible fields, it's a good idea to use a custom URL, so it doesn't attempt a
    10  * request for something like /Security/autocomplete.
    11  *
    12  * Any template can be returned in the callback function, but to make things easier,
    13  * you can use AutoCompleteField::render($resultSet) to fall back on the default
    14  * template.
    15  */
    16 class AutoCompleteField extends TextField {
    17
    18     /**
    19      * Render a default template of results.
    20      * Note: You must have functions "AutoCompleteTitle()" and "AutoCompleteSummary"
    21      * defined on the returned objects.
    22      * @param DataObjectSet $results The results to render in the autocomplete box
    23      * @return SSViewer
    24      */
    25     public static function render($results) {
    26         if(!$results) return false;
    27
    28         $template = new SSViewer('AutoComplete_default');
    29         return $template->process(new ArrayData(array (
    30             'Results' => $results
    31         )));
    32     }
    33
    34     /**
    35      * Constructor for AutoCompleteField
    36      * @param string $name The name of the field
    37      * @param string $title The label for the field
    38      * @param string $url A function or a URL for the results callback
    39      * @param string $val A default value (will be knocked out on focus)
    40      */
    41     public function __construct($name, $title, $url = "autocomplete", $val = null) {
    42         parent::__construct($name, $title, $val);
    43         if(!stristr($url,'/')) {
    44             // url was passed as a function name.. use the current controller
    45             $url = Controller::curr()->Link($url);
    46         }
    47         $this->addExtraClass("autocomplete_input");
    48         $this->addExtraClass("{'url' : '$url'}");
    49     }
    50
    51     /**
    52      * Require the dependencies and render the field
    53      * Note: The wrapper div is a hack. Position:relative would not work against
    54      * an input field in most browsers. :-(
    55      * @return string
    56      */
    57     public function Field() {
    58         Requirements::javascript(THIRDPARTY_DIR.'/jquery/jquery.js');
    59         Requirements::javascript(THIRDPARTY_DIR.'/jquery-metadata/jquery.metadata.js');
    60         Requirements::javascript('dataobject_manager/code/autocomplete_field/javascript/autocomplete_field.js');
    61         Requirements::css('dataobject_manager/code/autocomplete_field/css/autocomplete_field.css');
    62         return '<div class="autocomplete_holder">'.parent::Field().'<div class="autocomplete_results"></div></div>';
    63     }
    64 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -