theme_image_formatter() is a wrapper around theme_image_style() and theme_image(), both of which allow attributes to be added to the <img> tag.

Attributes can be already be added to the link if it exists, via $variables['path']['options'].

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett’s picture

Status: Active » Needs review
FileSize
1.24 KB
1.26 KB

Attached both D7 and D8. Yay for cherry-pick.

tim.plunkett’s picture

Status: Needs review » Closed (duplicate)
tim.plunkett’s picture

Status: Closed (duplicate) » Needs review

Reopening to get this in first, then rename path to uri.

RobLoach’s picture

Would be nice if we just used straght up $variables there instead of having to switch over to $image for Drupal 8. Save us a couple lines of code. For Drupal 7 though, it looks fine.

RobLoach’s picture

Version: 8.x-dev » 7.x-dev
FileSize
1.24 KB

#1025796: Rename path to uri in image functions was committed, so this might not be needed for Drupal 8. For Drupal 7 though? Bot?

Status: Needs review » Needs work
Issue tags: -Needs backport to D7

The last submitted patch, drupal-1329586-1.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review
Issue tags: +Needs backport to D7

#5: drupal-1329586-1.patch queued for re-testing.

jthorson’s picture

I'm thinking we should be checking $item['attributes'] as well as $variable['attributes'] ... if I'm working with upstream code that sets a 'title', 'alt', and 'fid' attribute on an image and then returns that image, these all get tacked on to $item.

If I want it to appear in $variables['#attributes'] for proper rendering with this patch, I would then have to pull the 'fid' out of $item and plug it into $variables['#attributes'] later in the pre-process function. This feels hackish to me.

That said, I can also see the benefit of being able to tack on '#attributes' when defining the render array.

How about something like this?

$image['attributes'] = array_merge(
  (isset($item['attributes']) ? $item['attributes'] : array(),
  (isset($variables['attributes']) ? $variables['attributes'] : array()
);
RobLoach’s picture

What if it were to JUST check $item['attributes']? Looking at theme_image_formatter(), we see "item: An array of image data."..... Then looking at theme_image(), we see that the image data is along side the "attributes".....

Actually, I agree that we probably don't want to override it if it already exists... hmm.

tim.plunkett’s picture

Issue tags: -Needs backport to D7
FileSize
510 bytes

It makes it a little clearer that the attributes are for the image, not for the possible link.
But it makes it harder to document.

RobLoach’s picture

#10: drupal-1329586-10.patch queued for re-testing.

ultimateboy’s picture

Status: Needs review » Reviewed & tested by the community

#10 works great. I think it's time to RTBC.

webchick’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

1. We need some documentation of that newly accepted parameter.
2. Let's also add a test for this.

RobLoach’s picture

Status: Needs work » Needs review
FileSize
1.87 KB

1. We need some documentation of that newly accepted parameter.

It's technically not a new parameter, as it was expected in theme_image(), but absolutely ignored in theme_image_formatter(). I'm with you that more documentation never hurts though!

2. Let's also add a test for this.

Explicit test added!

Status: Needs review » Needs work

The last submitted patch, image-attributes.patch, failed testing.

ultimateboy’s picture

Status: Needs work » Needs review
jthorson’s picture

And just for the sake of completeness ...

Status: Needs review » Needs work

The last submitted patch, image-attributes-testonly.patch, failed testing.

jthorson’s picture

Status: Needs work » Reviewed & tested by the community
ultimateboy’s picture

#17: image-attributes-testonly.patch queued for re-testing.

RobLoach’s picture

Status: Reviewed & tested by the community » Needs work

The patch is missing the $item['attributes'] updates from #14.

RobLoach’s picture

Status: Needs work » Needs review
FileSize
1.87 KB

re-upped.

ultimateboy’s picture

Status: Needs review » Reviewed & tested by the community

Alright.. this is definitely ready to go.. good documentation, a test which has been shown to fail in #17 and a fully functional test-passing patch in #22.

jthorson’s picture

RobLoach: That was intentional. (ie. test-only). ;)

xjm’s picture

Hmm, can someone confirm that it's not needed in D8 (as per #5)?

RobLoach’s picture

Hmm, can someone confirm that it's not needed in D8 (as per #5)?

Correct, Drupal 8 got the more elegant solution over at #1025796: Rename path to uri in image functions, and has been in since November. Drupal 7 just gets this simple $item['attributes'] check. I posted the difference between the branched patches right here.

RobLoach: That was intentional. (ie. test-only). ;)

Ah, proof that this patch is needed :-) .

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Cool, that looks great!

Committed and pushed to 7.x. Yay!

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

ñull’s picture

Does this patch also include the title attribute? I have the title attribute set but I don't see it in the output.

ñull’s picture

Issue summary: View changes
Status: Closed (fixed) » Needs work
ñull’s picture

Status: Needs work » Closed (fixed)

False alarm. It was a JS issue.