User tests: Successful: Unsuccessful:
Pull Request for Issue #16034
HTML5 is designed with extensibility in mind for data that should be associated with a particular element but need not have any defined meaning. data-* attributes allow us to store extra information on the standard, semantic HTML elements without other hacks such as non-standard attributes, extra properties on DOM.
However, custom attributes have no special meaning generally and are only special to the owner's application. They can be used to simplify an application's logic. By allowing JForm to support data attributes, it can be greatly aid development of extensions both core and third party.
Add data attribute in XML form, for example
<field name="name"
type="text"
description="COM_USERS_USER_FIELD_NAME_DESC"
label="COM_USERS_USER_FIELD_NAME_LABEL"
required="true"
size="30"
data-action-type="click" // add data attribute like this
/>
Login into administrator
Click on menu Users => Manage => Add New User
Inspect the filed = Name and check input type html
<input type="text"
name="jform[name]"
id="jform_name"
value=""
class="required invalid"
size="30"
required="required"
aria-required="true"
data-action-type="click" // output
autocomplete="off"
aria-invalid="true"
/>
<input type="text"
name="jform[name]"
id="jform_name"
value=""
class="required invalid"
size="30"
required="required"
aria-required="true"
autocomplete="off"
aria-invalid="true"
/>
Status | New | ⇒ | Pending |
Category | ⇒ | Layout Libraries |
Labels |
Added:
?
|
@dgt41 fixed. Please check once
any update on this @nilesh-more any additional changes needed on this to merge ? @dgt41 any more fixes needed ?
Category | Layout Libraries | ⇒ | Unit Tests Repository Administration com_admin SQL Postgresql MS SQL com_categories com_config com_content com_fields com_finder com_joomlaupdate com_languages com_menus |
Category | Unit Tests Repository Administration com_admin SQL Postgresql MS SQL com_categories com_config com_content com_fields com_finder com_joomlaupdate com_languages com_menus | ⇒ | Layout Libraries |
Labels |
Added:
?
|
Labels |
Removed:
?
|
Addittionaly.
Why to use JText
for each data-
value,
data-
value can be anything: int (123), boolean (true/false), json string,
I do not think that it is a good idea to use JText
here.
@Quy and @Fedik @parthlawate any additional changes needed on this to merge?
there still can be improved a lot. $dataAttributeName
, $dataAttributeValues
and getDataAttributes()
looks overcoded to me.
it can be more easy:
/**
* The data attributes of the form field.
* For example, data-action-type="click" data-action-type="change"
*
* @var string[]
*/
protected $dataAttributes = array();
which store attributes as key
=> value
$dataAttributes['data-country'] = 'data-country="India"'
Then in setup()
you just iterate thought all attributes and check for data-
foreach ($element->attributes() as $k => $v) {
if (strpos($attrName, "data-") === 0) {
$this->dataAttributes[$k] = $k . '="' . htmlspecialchars($v, ENT_COMPAT, 'UTF-8') . '"'
}
}
then in __get
, inside default:
:
if (strpos($name, "data-") === 0 && array_key_exists($name, $this->dataAttributes)) {
return $this->dataAttributes[$name]
}
in __set
inside default:
if (strpos($name, "data-") === 0) {
$this->dataAttributes[$k] = $name . '="' . htmlspecialchars($v, ENT_COMPAT, 'UTF-8') . '"'
}
and finally, if you still need this getDataAttributes()
:
public function getDataAttributes() {
return $this->dataAttributes;
}
I think it best that we can do for handling data-
attributes in JField
, if we want to have it.
Additionally, all @since
should be: @since __DEPLOY_VERSION__
@nilesh-more looks good
Please replace @since 3.8.1
to @since __DEPLOY_VERSION__
, so it will be set automatically
@parthlawate @Fedik Done with requested changes
@nilesh-more cool thanks!
I think it is ready for testing,
just no idea what wrong with Travis
The travis issue is
1) JFormFieldCheckboxesTest::testGetOptions
The field with two values did not produce the right options
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
'onchange' => ''
+ 'dataAttributes' => ''
@@ @@
'onchange' => ''
+ 'dataAttributes' => ''
)
)
/home/travis/build/joomla/joomla-cms/tests/unit/suites/libraries/joomla/form/fields/JFormFieldCheckboxesTest.php:685
@Fedik Yes... I'm looking into it.
@puneet0191 Need your help to figure out what wrong with Travis
I have tested this item
It worked for me on a text field.
(It did not work on a field of type radio and componentlayout.)
@bettyorganero Based on a @Fedik suggestion I have removed multi-language support.
cc: @Fedik
@bettyorganero @astridx can you please retest?
@nilesh-more ok thanks for your answer. It is unfortunate because some data-attributes are used to show content that in a multi-language site is necessary the translation. @Fedik
@franz-wohlkoenig I have tested yesterday night and works good the only problem is the multi-language support
Best
It is unfortunate because some data-attributes are used to show content that in a multi-language site is necessary the translation
Use JText::script()
and Joomla.JText._()
for frontend translation in your JavaScript. https://stackoverflow.com/a/30506820/1000711
@Fedik @nilesh-more
Can't we add a new parameter to decide or not of the use of JText ?
It could be done the way we do for
case 'translateLabel':
case 'translateDescription':
case 'translateHint':
we would have translate-data-action-type
we can add anything,
but, personally, I do not see much sense in it
data
attribute is mix of everything, it can be '0', 'true', '(function(){ some random code})()',
I do not think it is good to run everything through JText::_()
, even with a new parameter.
With this implementation, fields can now have data-* attributes defined in the XML. However how to make use of these attributes is left to the developer. So supporting JText isn't needed. Wherever needed the developer can do it at the display/implementation time.
Hello guys.
Use JText::script() and Joomla.JText._() for frontend translation in your JavaScript. https://stackoverflow.com/a/30506820/1000711
@Fedik thanks but this not work with the data attributes into my xml form file.
I not an expert programmer. So, I have my data attribute into my field:
data-tooltip="COM_PRODCATALOG_ENTERPRISE_WEBSITE_FORMAT"
I don't understand why the label constant works but the constant into my data-tooltip not works. Is for this reason I asked you for the possibility to support the multi-language option. Maybe is for this that not works
Thanks
<label>
and data-
attributes is 2 different things.
<label>
contain caption for the field, which obviously should be translated for multilanguage site,
data-
can contain anything, that developer add, this make no sense to translate by default, in same way as it with label.
see my previous comment #16141 (comment)
@Fedik I know that <label>
l and data-
are different things and I understood your point but is not always true or false. Some case like mine, I have a tooltip: data-tooltip="SOME_TEXT_HERE"
where the text working with data attributes and this text need to be translate.
Anyway, I will find a solution for this and I will you update, in any case thanks for your contribution is not bad work with data attributes now in joomla forms.
what script do you use for data-tooltip="SOME_TEXT_HERE"
?
@bettyorganero then it is easy, something like this:
$('.tooltipped').each(function(){
var $el = $(this);
$el.data('tooltip', Joomla.JText._($el.data('tooltip'))).tooltip();
});
And you have to call JText::script("BLABLA_TOOLTIP")
to add translation strings on to client side.
but it is not related to current pull request
The PR is only about adding support for data attributes which otherwise get ignored when generating the markup from a XML field definition. How to use the values is left upto the developer. In the future we can have more PRs that use specific attributes, but I vote to keep this PR simple for now.
Anything else that is stopping this from being merged ?
@Fedik @brianteeman Anything else that is blocking this from being merged as It's open since one year
I don't see any successful tests marked by real people yet.
I have tested this item
@nilesh-more so we need a second successfully Test ;-)
I have tested this item
Status | Pending | ⇒ | Ready to Commit |
Ready to Commit after two successful tests.
Thanks for tests, Guys.
@nilesh-more now Maintainer decide if merge.
Labels |
Added:
?
|
The build failed.
Tabs must be used to indent lines; spaces are not allowed :
The build failed.
Tabs must be used to indent lines; spaces are not allowed :
joomla-cms/layouts/joomla/form/field/color/advanced.phpLine 95 in f46c595
$dataAttribute;
@twister65 MR updated.
Should still be unescaped here, layouts should handle escaping the values.
Yes just fixed that with another commit.
Sorry din't saw that it where that much files to fix. So here we go.
Status | Ready to Commit | ⇒ | Pending |
Labels |
New tests required.
New tests required.
Labels |
Removed:
?
|
Title |
|
Labels |
Removed:
J3 Issue
|
I have tested this item
I have tested this item
Status | Pending | ⇒ | Ready to Commit |
RTC
Since this is a new feature it will not go into J3 but I would really like to have this in J4.
@HLeithner is it too much to ask an exception to the rule here from the PLT.
Let me explain this is a fundamental feature of the HTML5 and J4 is months away from release so in sort I'm kindly asking the PLT to bend the rule here
Thanks
I'm sorry @dgrammatiko there will be no new features in J3 except they are security related or critical for operation.
PLT doesn't changed this for this feature.
The focus is clearly on getting Joomla 4.0 released.
So it would be great if @nilesh-more can update this PR against J4 or if he is not longer interested someone else can do this.
@dgrammatiko I do not see much sense to push it to j3, now.
It is not much critical feature. Who was really need it, already has own workaround.
It is better to concentrate the time and resources on j4.
Status | Ready to Commit | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2019-12-05 15:13:28 |
Closed_By | ⇒ | Quy |
Please test PR #27212 rebased for J4.
@nilesh-more the output needs to be escaped, e.g.
data-action-type="click"
the value needshtmlspecialchars($the_variable_name, ENT_COMPAT, 'UTF-8')