Skip to content

Commit

Permalink
MDL-51948 formslib: Support forcing LTR on form elements
Browse files Browse the repository at this point in the history
Part of MDL-55071
  • Loading branch information
Frederic Massart authored and danpoltawski committed Sep 23, 2016
1 parent 113efed commit fc7f69e
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/form/duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ function _createElements() {
}
$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
Expand Down
12 changes: 11 additions & 1 deletion lib/form/recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function toHtml() {
<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>
Expand Down Expand Up @@ -162,4 +162,14 @@ public function export_for_template(renderer_base $output) {
$context['html'] = $this->toHtml();
return $context;
}

/**
* Get force LTR option.
*
* @return bool
*/
public function get_force_ltr() {
return true;
}

}
43 changes: 40 additions & 3 deletions lib/form/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -109,13 +112,28 @@ function getFrozenHtml()
*
* @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;
}
}

Expand All @@ -128,4 +146,23 @@ function getHelpButton(){
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 changes: 33 additions & 0 deletions lib/form/textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -95,6 +98,16 @@ function setHiddenLabel($hiddenLabel){
* @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') . '" >' .
Expand Down Expand Up @@ -133,4 +146,24 @@ function getElementTemplateType(){
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 changes: 19 additions & 0 deletions lib/form/url.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ function toHtml(){
$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').'" >'.
Expand Down Expand Up @@ -180,4 +189,14 @@ public function export_for_template(renderer_base $output) {
$context['filepickerhtml'] = $this->toHtml();
return $context;
}

/**
* Get force LTR option.
*
* @return bool
*/
public function get_force_ltr() {
return true;
}

}
29 changes: 28 additions & 1 deletion lib/formslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,21 @@ function closeHeaderBefore($elementName){
$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.
Expand All @@ -1755,6 +1770,16 @@ function closeHeaderBefore($elementName){
*/
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);
}
}

/**
Expand All @@ -1764,7 +1789,9 @@ function setType($elementname, $paramtype) {
* @see MoodleQuickForm::setType
*/
function setTypes($paramtypes) {
$this->_types = $paramtypes + $this->_types;
foreach ($paramtypes as $elementname => $paramtype) {
$this->setType($elementname, $paramtype);
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===

Expand Down

0 comments on commit fc7f69e

Please sign in to comment.