Skip to content

Allow jForm to support data attribute#16141

Closed
nilesh-more wants to merge 67 commits intojoomla:stagingfrom
nilesh-more:local-staging
Closed

Allow jForm to support data attribute#16141
nilesh-more wants to merge 67 commits intojoomla:stagingfrom
nilesh-more:local-staging

Conversation

@nilesh-more
Copy link
Copy Markdown

@nilesh-more nilesh-more commented May 20, 2017

Pull Request for Issue #16034

Summary of Changes

  • 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.

Testing Instructions

  • Add data attribute in XML form, for example

    • Open administrator/components/com_users/models/forms/user.xml file
    • Add data attribute in xml field, like
       <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

Expected result

  • Output should be like this
     <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"
       />
         
  • Output snapshot
    screenshot from 2017-05-15 19 27 38

Actual result

  • Added data attribute is missing in final output
     <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"
    />

Documentation Changes Required

@dgrammatiko
Copy link
Copy Markdown
Contributor

dgrammatiko commented May 20, 2017

@nilesh-more the output needs to be escaped, e.g. data-action-type="click" the value needs htmlspecialchars($the_variable_name, ENT_COMPAT, 'UTF-8')

@nilesh-more
Copy link
Copy Markdown
Author

@DGT41 fixed. Please check once

Comment thread libraries/joomla/form/field.php Outdated
Comment thread libraries/joomla/form/fields/accesslevel.php Outdated
$onchange = !empty($this->onchange) ? ' onchange="' . $this->onchange . '"' : '';

// Initialize JavaScript field data attributes. For eg, data-action-type="click"
$dataAttribute = !empty($this->dataAttributeValues) ? ' ' . implode(" ", $this->dataAttributeValues) : '';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change implode(" ", to implode(' ',.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Quy resolved

Comment thread libraries/joomla/form/fields/combo.php Outdated
Comment thread libraries/joomla/form/fields/groupedlist.php Outdated
Comment thread libraries/joomla/form/fields/list.php Outdated
Comment thread libraries/joomla/form/fields/usergroup.php Outdated
Comment thread libraries/joomla/form/field.php Outdated
break;

case $this->dataAttributeName = $this->dataAttributeName ? $this->dataAttributeName : '' === $name:
$this->dataAttributeValues[] = $name . '="' . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '"';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is bad approach,
it does not allow to override value for an existing data- attribute, from outside,
and require of $this->dataAttributeName make it even more unintuitive and complicated.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here still need a better solution, I think

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fedik Please can you elaborate on what you mean by it does not allow to override value for an existing data- attribute, from outside, it will help me to troubleshoot

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example I have a field:

<field data-blabla="foo" />

and at some point of application execution I want to have another value for field data- :

$field->{'data-blabla'} = 'something else';

Your current approach does not allow to do this.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fedik, my understanding based on your latest comment is - basically, on the fly you want to change data attribute value

For eg,

  • I have one text field having two data attribute

            <form>
              <fieldset addfieldpath="../../.." name="demo_field">
                   <field type="text" name="name" data-first-name="Nilesh" data-country="BBB" name="name"/>
              </fieldset>
           </form>```
    
    
  • Now, suppose you want to change data attribute value form data-country="BBB" to data-country="INDIA" then you can simply use setFieldAttribute($field, $attribute, $value) in your layout file

    • for example,
      $this->form->setFieldAttribute('text', 'data-country', 'INDIA');
  • After execution, output is something like this

 <input type="text" 
      name="jform[name]" 
      id="jform_name" 
      data-first-name="Nilesh"  
      data-country="INDIA"  // output - updated value
     />
         

If you are referring/pointing something different then please explain

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nilesh-more But would the value need to change from Language to language ? And the problem is that if i was missing those language constants in a different language, would it not break ?

Copy link
Copy Markdown
Member

@Fedik Fedik Sep 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nilesh-more no, I meant this:

foreach ($form->getGroup(null) as $field) {
  $field->{'data-country'} = 'India';
  echo $field->renderField();
}

Copy link
Copy Markdown
Author

@nilesh-more nilesh-more Sep 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fedik Issue is resolved now.

  • Now, you can override value for an existing data-* attribute, from outside by using
        - $field->{'data-country'} = 'India'; 
       and 
        - $this->form->setFieldAttribute('text', 'data-country', 'INDIA');

@parthlawate
Copy link
Copy Markdown
Contributor

any update on this @nilesh-more any additional changes needed on this to merge ? @DGT41 any more fixes needed ?

Comment thread libraries/src/Joomla/CMS/Form/FormField.php
@zero-24
Copy link
Copy Markdown
Contributor

zero-24 commented Feb 26, 2019

Sorry din't saw that it where that much files to fix. So here we go.

@zero-24
Copy link
Copy Markdown
Contributor

zero-24 commented Feb 26, 2019

New tests required.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/16141.

@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Feb 26, 2019
@ghost ghost added the J3 Issue label Apr 5, 2019
@ghost ghost removed the J3 Issue label Apr 19, 2019
@ghost ghost changed the title Issue #16034 feat: Allow jForm to support data attribute Allow jForm to support data attribute Apr 19, 2019
@dgrammatiko
Copy link
Copy Markdown
Contributor

@wilsonge can you press the button to update this repo?

@dgrammatiko
Copy link
Copy Markdown
Contributor

I have tested this item ✅ successfully on 64e9d40


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/16141.

1 similar comment
@alikon
Copy link
Copy Markdown
Contributor

alikon commented Dec 4, 2019

I have tested this item ✅ successfully on 64e9d40


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/16141.

@alikon
Copy link
Copy Markdown
Contributor

alikon commented Dec 4, 2019

RTC


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/16141.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Dec 4, 2019
@HLeithner
Copy link
Copy Markdown
Member

Since this is a new feature it will not go into J3 but I would really like to have this in J4.

@Quy Quy added J4 Rebase and removed PR-staging RTC This Pull Request is Ready To Commit labels Dec 4, 2019
@dgrammatiko
Copy link
Copy Markdown
Contributor

@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

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Dec 4, 2019
@HLeithner
Copy link
Copy Markdown
Member

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.

@Fedik
Copy link
Copy Markdown
Member

Fedik commented Dec 5, 2019

@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.

@Quy Quy closed this Dec 5, 2019
@Quy Quy added PR-staging and removed J4 Rebase RTC This Pull Request is Ready To Commit labels Dec 5, 2019
@Quy
Copy link
Copy Markdown
Contributor

Quy commented Dec 5, 2019

Please test PR #27212 rebased for J4.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/16141.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.