First of all, forgive me, this is my first attempt at twig. This patch: #1806426: Incorrect syntax in poll-results.twig causing it to fail needs to be committed before the following can be tested... create a poll and view the results to see the output.

I'm just wondering what needs to be done here to get the attributes ( e.g. data-max="1" data-value="1" ) to actually print in the template. Currently, everything works except for the HTML5 data attributes aren't printed, and the following error displays:

User error: "class" is an invalid render array key in element_children() (line 6251 of core/includes/common.inc).
User error: "class" is an invalid render array key in element_children() (line 6251 of core/includes/common.inc).
User error: "class" is an invalid render array key in element_children() (line 6251 of core/includes/common.inc).

Should a preprocess function be created for theme_meter to handle this portion of theme_meter?

  $attributes = $variables['attributes'];

  foreach (array('form', 'high', 'low', 'max', 'min', 'optimum', 'value') as $attribute) {
    if (!empty($variables[$attribute])) {
      // This function was initially designed for the <meter> tag, but due to
      // the lack of browser and styling support for it, we're currently using
      // it's attributes as HTML5 data attributes.
      $attributes['data-' . $attribute] = $variables[$attribute];
    }
  }

I tried pulling it into the template itself, using something like the following, but I can't get anything but a list of the values to print out.. and it seems to me it shouldn't really be in the template anyway. Advice please?

{% for attribute in ['form', 'high', 'low', 'max', 'min', 'optimum', 'value'] %}
...
{% endfor %}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

KrisBulman’s picture

Issue summary: View changes

added link to patch

Fabianx’s picture

Status: Needs review » Needs work

KrisBulman: Thanks a lot for the work!

Please do:

preprocess

$variables['attributes'] = new Attribute($variables['attributes']);

theme:

<tag {{ attributes }}>

or if the class is important:

<tag class="{{ attributes.class }}" {{- attributes }}>
anthonyR’s picture

@Fabianx:
I've seen this pattern before in the sandbox:
<tag class="{{ attributes.class }}" {{- attributes }}>

But this would give an empty class attribute if attributes.class is empty. Shouldn't we add checks for the property around the attribute everywhere this occurs?

Fabianx’s picture

anthoyR: I know.

I think about using {{ attributes }}|merge({ class: ["my-class"] })

instead but I think its too complicated.

Maybe we can keep the "class" and use some twig cleanup magic.

anthonyR’s picture

Sorry for hijacking this issue, but it's something that occurs in almost every template.
This is one of the things we should decide a pattern for so that everyone uses it.
Otherwise we need to patch all the templates again when we decide what to do with it. Separate issue?

KrisBulman’s picture

Status: Needs work » Needs review
FileSize
732 bytes

Requested changes are now in and HTML5 attributes appear OK.

Note: there is a problem when adding new questions to a poll, this seems to be coming from theme_table, and should have nothing to do with this patch. Possibly related to #1778968: Convert theme_table to Twig

An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /system/ajax
StatusText: OK
ResponseText: Recoverable fatal error: Argument 1 passed to drupal_attributes() must be an array, object given, called in /core/includes/theme.inc on line 1893
Fabianx’s picture

Status: Needs review » Needs work

Can you please add the patch?

KrisBulman’s picture

Status: Needs work » Needs review
FileSize
732 bytes

That would help!

KrisBulman’s picture

Status: Needs review » Needs work

sorry, let me reroll that. missing the twig template

KrisBulman’s picture

Status: Needs work » Needs review
FileSize
2.16 KB

OK, here it is.

Fabianx’s picture

Status: Needs review » Needs work
+++ b/core/themes/stark/templates/theme.inc/meter.twigundefined
@@ -0,0 +1,37 @@
+ * - high: A number specifying the range that is considered to be a high
+ *   value.
+ * - low: A number specifying the range that is considered to be a low value.
+ * - max: A number specifying the maximum value of the range.
+ * - min: A number specifying the minimum value of the range.

Hmm, this docs do not match that this is just in the attributes.

I have not seen original function, yet, but this unfortunately needs still some little work.

Good job so far!

KrisBulman’s picture

this docs do not match that this is just in the attributes.

I don't follow, can you clarify please?

Here is what's in the original function:

/**
 * Returns HTML for a meter.
 *
 * @param $variables
 *   An associative array containing:
 *   - display_value: The textual representation of the meter bar.
 *   - form: A string specifying one or more forms to which the <meter> element
 *     belongs separated by spaces.
 *   - high: A number specifying the range that is considered to be a high
 *     value.
 *   - low: A number specifying the range that is considered to be a low value.
 *   - max: A number specifying the maximum value of the range.
 *   - min: A number specifying the minimum value of the range.
 *   - optimum: A number specifying what value is the optimal value for the
 *     gauge.
 *   - value: A number specifying the current value of the gauge.
 *   - percentage: A number specifying the current percentage of the gauge.
 *   - attributes: Associative array of attributes to be placed in the meter
 *     tag.
 */

API docs: http://api.drupal.org/api/drupal/core!includes!theme.inc/function/theme_meter/8

Fabianx’s picture

Status: Needs work » Needs review

Woops, my fault. Really sorry, looks seriously really good.

Could you attach some test code that calls this function for easy testing?

That would be totally great!

Just from looking at it, I'd say its RTBC, but I have not tested, yet.

KrisBulman’s picture

I'd love to, but not sure how to go about it, do we have any existing tests set up for converted functions to template_preprocess functions?

Here is the original documentation on how to override theme_meter, which dropped the data- from the html5 attribute and moved from a <div> to a <meter> tag.

Fabianx’s picture

Here? Where is here?

KrisBulman’s picture

as usual, i forget the most important part
http://drupal.org/node/1432244

and this is the original ticket in which the function got created
http://drupal.org/node/1229442

KrisBulman’s picture

Steps to test this function:

  1. Enable the poll module
  2. Create a poll
  3. Vote on the poll
  4. View the results and inspect the meters

output shows
theme-meter html inspection - original

modifying "data" to "foo" in the preprocess does change the attribute:
theme-meter html inspection - mod

Whatever else you need I am willing to provide.

Fabianx’s picture

Assigned: Unassigned » psynaptic
Status: Needs review » Reviewed & tested by the community

Thats very nice!

RTBC then!

psynaptic’s picture

I'll test and commit this soon. Sorry about the delay.

podarok’s picture

Status: Reviewed & tested by the community » Needs work

status

KrisBulman’s picture

OK, i see this needs work only based on your status change podarok, can you elaborate so it can be cleaned up?

podarok’s picture

status change based on #18

KrisBulman’s picture

Status: Needs work » Needs review

OK, if more testing is needed, it should be needs review

not sure what else I can do to move it forward without review

pixelwhip’s picture

Status: Needs review » Reviewed & tested by the community

The patch in #9 looks good to me and works as expected. Marking back to RTBC.

podarok’s picture

Status: Reviewed & tested by the community » Fixed

#9 commited / push to front-end

thanks!!!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

updated for clarification