public function instance_form_definition(\MoodleQuickForm $mform) {
        $field = $this->get_field();
        // Get the current calendar in use - see MDL-18375.
        $calendartype = \core_calendar\type_factory::get_calendar_instance();

        $config = $field->get('configdata');

        // Always set the form element to "optional", even when it's required. Otherwise it defaults to the
        // current date and is easy to miss.
        $attributes = ['optional' => true];

        if (!empty($config['mindate'])) {
            $attributes['startyear'] = $calendartype->timestamp_to_date_array($config['mindate'])['year'];
        }

        if (!empty($config['maxdate'])) {
            $attributes['stopyear'] = $calendartype->timestamp_to_date_array($config['maxdate'])['year'];
        }

        if (empty($config['includetime'])) {
            $element = 'date_selector';
        } else {
            $element = 'date_time_selector';
        }
        $elementname = $this->get_form_element_name();
        $mform->addElement($element, $elementname, $this->get_field()->get_formatted_name(), $attributes);
        $mform->setType($elementname, PARAM_INT);
        $mform->setDefault($elementname, time());
        if ($field->get_configdata_property('required')) {
            $mform->addRule($elementname, null, 'required', null, 'client');
        }
    }
public function instance_form_definition(\MoodleQuickForm $mform) {
        $field = $this->get_field();
        // Get the current calendar in use - see MDL-18375.
        $calendartype = \core_calendar\type_factory::get_calendar_instance();

        $config = $field->get('configdata');

        // Always set the form element to "optional", even when it's required. Otherwise it defaults to the
        // current date and is easy to miss.
        $attributes = ['optional' => true];

        if (!empty($config['mindate'])) {
            $attributes['startyear'] = $calendartype->timestamp_to_date_array($config['mindate'])['year'];
        }

        if (!empty($config['maxdate'])) {
            $attributes['stopyear'] = $calendartype->timestamp_to_date_array($config['maxdate'])['year'];
        }

        if (empty($config['includetime'])) {
            $element = 'date_selector';
        } else {
            $element = 'date_time_selector';
        }
        $elementname = $this->get_form_element_name();
        $mform->addElement($element, $elementname, $this->get_field()->get_formatted_name(), $attributes);
        $mform->setType($elementname, PARAM_INT);
        $mform->setDefault($elementname, time());
        if ($field->get_configdata_property('required')) {
            $mform->addRule($elementname, null, 'required', null, 'client');
        }
    }
public function instance_form_definition ( \ MoodleQuickForm $ mform ) { $ field = $ this -> get_field ( ) ; // Get the current calendar in use - see MDL-18375. $ calendartype = \ core_calendar \ type_factory :: get_calendar_instance ( ) ; $ config = $ field -> get ( 'configdata' ) ; // Always set the form element to "optional", even when it's required. Otherwise it defaults to the // current date and is easy to miss. $ attributes = [ 'optional' => true ] ; if ( ! empty ( $ config [ 'mindate' ] ) ) { $ attributes [ 'startyear' ] = $ calendartype -> timestamp_to_date_array ( $ config [ 'mindate' ] ) [ 'year' ] ; } if ( ! empty ( $ config [ 'maxdate' ] ) ) { $ attributes [ 'stopyear' ] = $ calendartype -> timestamp_to_date_array ( $ config [ 'maxdate' ] ) [ 'year' ] ; } if ( empty ( $ config [ 'includetime' ] ) ) { $ element = 'date_selector' ; } else { $ element = 'date_time_selector' ; } $ elementname = $ this -> get_form_element_name ( ) ; $ mform -> addElement ( $ element , $ elementname , $ this -> get_field ( ) -> get_formatted_name ( ) , $ attributes ) ; $ mform -> setType ( $ elementname , PARAM_INT ) ; $ mform -> setDefault ( $ elementname , time ( ) ) ; if ( $ field -> get_configdata_property ( 'required' ) ) { $ mform -> addRule ( $ elementname , null , 'required' , null , 'client' ) ; } }
Add fields for editing data of a date field on a context.

@param \MoodleQuickForm $mform
Add fields for editing data of a date field on a context .