MDL-51948 formslib: Support forcing LTR on form elements

Part of MDL-55071
This commit is contained in:
Frederic Massart
2016-09-23 10:54:49 +01:00
committed by Dan Poltawski
parent 113efed5b0
commit fc7f69e7e8
7 changed files with 140 additions and 6 deletions
+3 -1
View File
@@ -144,7 +144,9 @@ class MoodleQuickForm_duration extends MoodleQuickForm_group {
}
$this->_elements = array();
// E_STRICT creating elements without forms is nasty because it internally uses $this
$this->_elements[] = $this->createFormElement('text', 'number', get_string('time', 'form'), $attributes, true);
$number = $this->createFormElement('text', 'number', get_string('time', 'form'), $attributes, true);
$number->set_force_ltr(true);
$this->_elements[] = $number;
unset($attributes['size']);
$this->_elements[] = $this->createFormElement('select', 'timeunit', get_string('timeunit', 'form'), $this->get_units(), $attributes, true);
// If optional we add a checkbox which the user can use to turn if on
+11 -1
View File
@@ -114,7 +114,7 @@ class MoodleQuickForm_recaptcha extends HTML_QuickForm_input implements templata
<span class="recaptcha_only_if_image"><label for="recaptcha_response_field">' . $strenterthewordsabove . '</label></span>
<span class="recaptcha_only_if_audio"><label for="recaptcha_response_field">' . $strenterthenumbersyouhear . '</label></span>
<input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
<input type="text" id="recaptcha_response_field" name="recaptcha_response_field" class="text-ltr" />
<input type="hidden" name="recaptcha_element" value="dummyvalue" /> <!-- Dummy value to fool formslib -->
<div><a href="javascript:Recaptcha.reload()">' . $strgetanothercaptcha . '</a></div>
<div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type(\'audio\')">' . $strgetanaudiocaptcha . '</a></div>
@@ -162,4 +162,14 @@ class MoodleQuickForm_recaptcha extends HTML_QuickForm_input implements templata
$context['html'] = $this->toHtml();
return $context;
}
/**
* Get force LTR option.
*
* @return bool
*/
public function get_force_ltr() {
return true;
}
}
+40 -3
View File
@@ -47,6 +47,9 @@ class MoodleQuickForm_text extends HTML_QuickForm_text implements templatable {
/** @var bool if true label will be hidden */
var $_hiddenLabel=false;
/** @var bool Whether to force the display of this element to flow LTR. */
protected $forceltr = false;
/**
* constructor
*
@@ -109,13 +112,28 @@ class MoodleQuickForm_text extends HTML_QuickForm_text implements templatable {
*
* @return string
*/
function toHtml(){
function toHtml() {
// Add the class at the last minute.
if ($this->get_force_ltr()) {
if (!isset($this->_attributes['class'])) {
$this->_attributes['class'] = 'text-ltr';
} else {
$this->_attributes['class'] .= ' text-ltr';
}
}
if ($this->_flagFrozen) {
return $this->getFrozenHtml();
}
$html = $this->_getTabs() . '<input' . $this->_getAttrString($this->_attributes) . ' />';
if ($this->_hiddenLabel){
$this->_generateId();
return '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
$this->getLabel().'</label>'.parent::toHtml();
$this->getLabel() . '</label>' . $html;
} else {
return parent::toHtml();
return $html;
}
}
@@ -128,4 +146,23 @@ class MoodleQuickForm_text extends HTML_QuickForm_text implements templatable {
return $this->_helpbutton;
}
/**
* Get force LTR option.
*
* @return bool
*/
public function get_force_ltr() {
return $this->forceltr;
}
/**
* Force the field to flow left-to-right.
*
* This is useful for fields such as URLs, passwords, settings, etc...
*
* @param bool $value The value to set the option to.
*/
public function set_force_ltr($value) {
$this->forceltr = (bool) $value;
}
}
+33
View File
@@ -50,6 +50,9 @@ class MoodleQuickForm_textarea extends HTML_QuickForm_textarea implements templa
/** @var bool if true label will be hidden */
var $_hiddenLabel=false;
/** @var bool Whether to force the display of this element to flow LTR. */
protected $forceltr = false;
/**
* constructor
*
@@ -95,6 +98,16 @@ class MoodleQuickForm_textarea extends HTML_QuickForm_textarea implements templa
* @return string
*/
function toHtml(){
// Add the class at the last minute.
if ($this->get_force_ltr()) {
if (!isset($this->_attributes['class'])) {
$this->_attributes['class'] = 'text-ltr';
} else {
$this->_attributes['class'] .= ' text-ltr';
}
}
if ($this->_hiddenLabel){
$this->_generateId();
return '<label class="accesshide" for="' . $this->getAttribute('id') . '" >' .
@@ -133,4 +146,24 @@ class MoodleQuickForm_textarea extends HTML_QuickForm_textarea implements templa
return 'default';
}
}
/**
* Get force LTR option.
*
* @return bool
*/
public function get_force_ltr() {
return $this->forceltr;
}
/**
* Force the field to flow left-to-right.
*
* This is useful for fields such as code or configuration snippets.
*
* @param bool $value The value to set the option to.
*/
public function set_force_ltr($value) {
$this->forceltr = (bool) $value;
}
}
+19
View File
@@ -101,6 +101,15 @@ class MoodleQuickForm_url extends HTML_QuickForm_text implements templatable {
$id = $this->_attributes['id'];
$elname = $this->_attributes['name'];
// Add the class at the last minute.
if ($this->get_force_ltr()) {
if (!isset($this->_attributes['class'])) {
$this->_attributes['class'] = 'text-ltr';
} else {
$this->_attributes['class'] .= ' text-ltr';
}
}
if ($this->_hiddenLabel) {
$this->_generateId();
$str = '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
@@ -180,4 +189,14 @@ EOD;
$context['filepickerhtml'] = $this->toHtml();
return $context;
}
/**
* Get force LTR option.
*
* @return bool
*/
public function get_force_ltr() {
return true;
}
}
+28 -1
View File
@@ -1745,6 +1745,21 @@ class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless {
$renderer->addStopFieldsetElements($elementName);
}
/**
* Set an element to be forced to flow LTR.
*
* The element must exist and support this functionality. Also note that
* when setting the type of a field (@link self::setType} we try to guess the
* whether the field should be force to LTR or not. Make sure you're always
* calling this method last.
*
* @param string $elementname The element name.
* @param bool $value When false, disables force LTR, else enables it.
*/
public function setForceLtr($elementname, $value = true) {
$this->getElement($elementname)->set_force_ltr($value);
}
/**
* Should be used for all elements of a form except for select, radio and checkboxes which
* clean their own data.
@@ -1755,6 +1770,16 @@ class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless {
*/
function setType($elementname, $paramtype) {
$this->_types[$elementname] = $paramtype;
// All param types, except TEXT and NOTAGS will be forced LTR. This will not always get
// it right, but it should be accurate in most cases. When inaccurate use setForceLtr().
if (!in_array($paramtype, [PARAM_TEXT, PARAM_NOTAGS])
&& $this->elementExists($elementname)
&& ($element =& $this->getElement($elementname))
&& method_exists($element, 'set_force_ltr')) {
$element->set_force_ltr(true);
}
}
/**
@@ -1764,7 +1789,9 @@ class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless {
* @see MoodleQuickForm::setType
*/
function setTypes($paramtypes) {
$this->_types = $paramtypes + $this->_types;
foreach ($paramtypes as $elementname => $paramtype) {
$this->setType($elementname, $paramtype);
}
}
/**
+6
View File
@@ -87,6 +87,12 @@ information provided here is intended especially for developers.
ALTER DATABASE moodle SET standard_conforming_strings = on;
ALTER DATABASE moodle SET search_path = 'moodle,public'; -- Optional, if you wish to use a custom schema.
You can set these options against the database or the moodle user who connects.
* Some form elements have been refined to better support right-to-left languages. In RTL,
most fields should not have their direction flipped, a URL, a path to a file, a number, ...
are always displayed LTR. Input fields and text areas now will best guess whether they
should be forced to be displayed in LTR based on the PARAM type associated with it. You
can call $mform->setForceLtr($elementName, true/false) on some form fields to manually
set the value.
=== 3.1 ===