Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass any attribute value type to markup #31

Merged
merged 3 commits into from
May 19, 2022

Conversation

callinmullaney
Copy link
Contributor

@callinmullaney callinmullaney commented Nov 19, 2021

Summary

This PR fixes/implements the following bugs/features

  • Allow for non-string values to be passed to the add_attributes function within a Drupal twig template

Explain the motivation for making this change. What existing problem does the pull request solve?

  • It's sometimes necessary to pass an integer as a value for an attribute. In particular colspan for <td> elements. Currently, this value must be converted to a string BEFORE being passed to add_attributes()

Documentation Update (required)

  • none

How to review this PR

  • Edit any component and assign some test attrbitues to a variable: Note - we are passing an integer value directly to colspan and a boolean/string/integer multivalue via an array to data-test-attribute - The result of the boolean value should be 1
{% set test_attributes = test_attributes|default({
  'colspan': 3,
  'data-test-attribute': [TRUE, 'string', 123],
}) %}
  • Call the add_attributes function on an element within a Drupal twig template
<div {{ add_attributes(test_attributes) }}></div>
  • Inspect the markup and verify the above attributes are being properly rendered

Closing issues
emulsify-ds/emulsify-twig-extensions#4

Copy link
Collaborator

@ccjjmartin ccjjmartin left a comment

Choose a reason for hiding this comment

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

Got a question for you.

else {
continue;
$value = [$value];
Copy link
Collaborator

Choose a reason for hiding this comment

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

@callinmullaney I am thinking this is a little too open, it seems like your use case (original question) is to support integers. Should we not have another case to handle integers (convert them to a string)?

Currently this is what I am seeing we are processing:

  1. Arrays (flat) - technically nested arrays would just not work
  2. (object) Attribute
  3. Strings
  4. Default - Do nothing

I think this would read easier if we were using a switch statement here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ccjjmartin I updated this PR to use a switch statement like you suggested. The original scope of this issue was to handle passing integers type values but with the changes I made we can also handle boolean.

That being said, while nested arrays and objects would also be a nice thing to process if passed to add_atributes() I think it's a bit out of scope for the original issue. The switch statement framework established here would be a good jumping off point for another branch to add support.

Can you take a quick look at this and confirm the testing criteria above?

Copy link
Collaborator

@ccjjmartin ccjjmartin left a comment

Choose a reason for hiding this comment

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

@callinmullaney Code looks good, functional works with test described 👍

@github-actions
Copy link

🎉 This PR is included in version 2.1.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

3 participants