Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

How to permanently customise jquery.ui.datepicker


Reply

6 Posts   490 Views

Avatar
tazzydemon

13 November 2013 at 9:05am Community Member, 122 Posts

My site currently used a custom version of framework/thirdparty/jquery-ui/datepicker/i18n/jquery.ui.datepicker-en-GB.js so that I can have year selection and a default start date.

All very well but a git pull or framework file update will destroy it.

Id there a default place I can put a copy so it will be found or is there a way I can tell the framework to look somewhere else

Julian

Avatar
Devlin

13 November 2013 at 10:16pm (Last edited: 13 November 2013 10:24pm), Community Member, 215 Posts

You need to overload DateField_View_JQuery and add your javascript file to the requirements.

class myDateField_View_JQuery extends DateField_View_JQuery {
   
   /**
    * @param String $html
    * @return
    */
   public function onAfterRender($html) {
      $html = parent::onAfterRender($html);
      
      Requirements::javascript('mydatefield.js');
      
      return $html;
   }
   
}

Object::useCustomClass('DateField_View_JQuery', 'myDateField_View_JQuery');

Avatar
tazzydemon

18 November 2013 at 12:07pm (Last edited: 18 November 2013 12:14pm), Community Member, 122 Posts

Thanks a lot. I would have had no idea where to look for this.

Now I see its in framework/forms/DateField.php and I observe that at the moment there are no other form elements that have the same treatment - however calendar is very particular about its use and may indeed vary on one site.

Julian

Avatar
tazzydemon

18 November 2013 at 1:52pm (Last edited: 18 November 2013 1:54pm), Community Member, 122 Posts

There seems to be a snag.

DateField_View_JQuery is only used for static method calls that I can tell, which means I can't useCustomClass() on it as it's never created with the factory method Object::create().

So its back to the drawing board. ..

Avatar
Devlin

18 November 2013 at 10:09pm (Last edited: 19 November 2013 9:20am), Community Member, 215 Posts

it's never created with the factory method Object::create().

Of course it is. It's in DateField::FieldHolder()... Line 118... Works fine for me.

if ($this->getConfig('showcalendar')) {
   // TODO Replace with properly extensible view helper system
   $d = DateField_View_JQuery::create($this);

~~

may indeed vary on one site

Well, then you need to overload DateField() itself and add your code as required.

class MyDateField extends DateField{
   function FieldHolder($properties = array()) {
      $html = parent::FieldHolder($properties);
      Requirements::javascript('mydatefield.js');
      return $html;
   }
}
$myDateField = MyDateField::create('date');
// or
$myDateField = new MyDateField('date');

Avatar
tazzydemon

22 November 2013 at 11:42am Community Member, 122 Posts

I'll have another look. I did a searc for that and did not find it.

I realise I could have extended and customised it in where it was finally called but I wanted to override the Class in such a way as when the repos are refreshed that change does not get erased anywhere including userdefined forms module