-
-
Notifications
You must be signed in to change notification settings - Fork 45
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
Add Attributes helper #60
Conversation
At the moment this is a very basic helper without any further functions. I missed translation and validation of attributes like in |
@froschdesign This could indeed be developed a bit further. I have some ideas I am working on, so marked this as draft for now. |
@froschdesign @weierophinney I have now added an |
|
d54eef7
to
bdc4282
Compare
A simple I still think that a real added value would be the functions from the abstract view helper of laminas-form. Enhanced with the support of application wide configuration for custom attributes. |
My original implementation was array-based, but an I can assure that just this simple functionality brings huge value for the style of templates that are common with Drupal, at least! But the functions from laminas-form would be a great addition too. |
No it is but the storing for multiple usage of the same attributes can be done via |
Do you mean something like I considered that style of approach but it does add extra logic to the helper, and does not seem as object-oriented as the store based approach. I also thought about the possible risk of ID collisions. Is this type of storage already implemented in some existing helper? |
No no! Instead of: $attributes = new AttributeStore(['class' => '…', 'title' => '…']);
echo $this->htmlAttributes($attributes);
echo $this->htmlAttributes($attributes); this: $attributes = ['class' => '…', 'title' => '…'];
echo $this->htmlAttributes($attributes);
echo $this->htmlAttributes($attributes); or: $attributes = new ArrayObject(['class' => '…', 'title' => '…']);
echo $this->htmlAttributes($attributes);
echo $this->htmlAttributes($attributes); No need for extra utility classes. |
That was basically the first version submitted in this PR. But dogfooding it I soon noticed that I am constantly repeating checks whether So I find that the Example: $attributes = $attributes ?? [];
$attributes['class'] = (array)($attributes['class'] ?? []);
$attributes['class'][] = 'nav';
$attributes['class'][] = 'navbar-nav';
echo $this->attributes($attributes); with $attributes = $this->attributes($attributes ?? []);
$attributes->addValue('class', ['nav', 'navbar-nav']);
echo $attributes; There might be a more elegant way to do the first version, but the main point is that with the utility class this logic is in just one place, and additional features like checks can also be added at already the individual attribute stage, instead of only for the entire set in the end. |
And my main point is: add this checks to the helper. No need for utility class. 😄 |
I'm afraid I don't understand. In the examples you provided, the checking is done to the entire array? But it needs be done when adding attributes to the array. And if |
|
I have a few ideas, but I need some time for a short elaboration. |
Ok, so meanwhile I have made an attempt to improve the class-based approach. I am not sure if two different escaper variables are actually required, but chose to be on the safe side. Based on a quick look at laminas-form, it might be possible to just "drop in" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good; I've suggested some improvements to make the various code paths more clear, and to reduce conditional nesting.
Ping me when you're ready for merge!
679a1e6
to
8858bd3
Compare
@weierophinney Thanks! I have now applied your suggestions. Just to be sure: the class names and packages are fine, and there should be two variables for the escaper? This is still a very minimal implementation and many useful features could be added, including features from the abstract view helper of laminas-form. More features could also be added in a subclass, of course. If this were to be merged in the current state, would it still be easy to add non-BC-breaking features? Or would it be better to "dogfood" this for a while longer? |
b8d4aae
to
279c938
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really looking great, and I hate to provide more changes, but it's all in the interest of polishing it up and getting it ready for release.
As noted below: this branch requires a minimum supported PHP version of 7.3, so new functionality can make full use of parameter and return typehints, including scalar hints and the iterable
pseudo-type.
Finally: please create a document in the docs/book/helpers/
folder detailing the new helper and its usage; you can use one of the others in that directory as a template. When done, add it to the list in the mkdocs.yml
file.
At that point, this will be ready to go!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few small changes are required. The most important is the name of the helper.
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Co-authored-by: Matthew Weier O'Phinney <matthew@weierophinney.net> Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Co-authored-by: Matthew Weier O'Phinney <matthew@weierophinney.net> Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
Co-authored-by: Frank Brückner <info@froschdesignstudio.de> Signed-off-by: Aleksi Peebles <aleksi@iki.fi>
11db998
to
b625250
Compare
Trailing whitespace Signed-off-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
Adds entry to TOC, and adds version in which it was added. Signed-off-by: Matthew Weier O'Phinney <matthew@weierophinney.net>
Signed-off-by: Aleksi Peebles aleksi@iki.fi
Description
A new helper to print out HTML tag attributes as discussed in the forum.
This is a minimal implementation, basically just moving existing methods from
AbstractHtmlElement
to a trait, used by a separate helper for more general use.