The required variable attributes should be listed first, and the optional attributes should indicate that they are optional, as per coding standards.

Also, it bears mentioning that attributes are now required, whereas in D6 they were optional. A few contrib modules have been hit by this.

Comments

dave reid’s picture

Why is the attributes value required now?

jacine’s picture

This has to be a bug, no?

damien tournoud’s picture

Status: Needs review » Needs work

attributes is definitely not required.

pillarsdotnet’s picture

Failure to provide at least an empty array() results in a PHP notice.

From the code:

function theme_image($variables) {
  $attributes = $variables['attributes'];
  $attributes['src'] = file_create_url($variables['path']);

  foreach (array('width', 'height', 'alt', 'title') as $key) {

    if (isset($variables[$key])) {
      $attributes[$key] = $variables[$key];
    }
  }

  return '<img' . drupal_attributes($attributes) . ' />';
}

That first line will result in an "Undefined index" notice.

pillarsdotnet’s picture

Status: Needs work » Needs review
dave reid’s picture

@pillarsdotnet: Prove to me that doing theme('image', array('path' => 'pathtofile.png')); causes a problem and that the theme system fails to merge in the default attributes variable of an empty array?

pillarsdotnet’s picture

StatusFileSize
new667 bytes

Run the attached code snippet in php. Mine produces the following output:

$ php test.php

Notice: Undefined index: attributes in /home/bobvin/Downloads/test.php on line 15
jacine’s picture

I tested this a little, and I'm not seeing notices for theme_image(). However, I am seeing a similar problem with theme_html_tag() as far as defaults not merging properly, so maybe there is a problem here that only happens under certain circumstances? This is what I tested with.

function mytheme_page_alter(&$page) {
  // Attributes is required here. Ok.
  $page['highlighted']['new_stuff'] = array(
    '#type' => 'container',
    '#attributes' => array('class' => 'my-container'),
  );
  // Attributes should be optional, but it's not working properly.
  $page['highlighted']['new_stuff']['heading'] = array(
    '#theme' => 'html_tag',
    '#tag' => 'h2',
    '#value' => t("I'm a Druplicon"),
    '#attributes' => array(), // Failure to include this throws the errors pasted below.
  );
  // No notice.
  $page['highlighted']['new_stuff']['image'] = array(
    '#theme' => 'image',
    '#path' => 'misc/druplicon.png',
    '#alt' => 'Druplicon',
  );
  // No notice this way either.
  $page['highlighted']['new_stuff']['imageasmarkup'] = array(
    '#markup' => theme('image', array('path' => 'misc/druplicon.png', 'alt' => 'Druplicon')),
  );

Notice: Undefined index: #attributes in theme_html_tag() (line 1904 of /Users/jacine/Sites/drupal-7/includes/theme.inc).
Recoverable fatal error: Argument 1 passed to drupal_attributes() must be an array, null given, called in /Users/jacine/Sites/drupal-7/includes/theme.inc on line 1904 and defined in drupal_attributes() (line 2253 of /Users/jacine/Sites/drupal-7/includes/common.inc).

dave reid’s picture

@pillarsdotnet: You are incorrectly using the theme system. You call theme functions using the literal theme() function. Use theme('image', ...) and not theme_image(...).

dave reid’s picture

@Jacine: Because html_tag is defined as render element, do the errors go away if you use '#type' => 'html_tag' rather than '#theme' => 'html_tag'?

pillarsdotnet’s picture

@Dave Reid: Either the theme_image() function has required parameters or it doesn't. Telling me that I shouldn't be using it at all is quite beside the point.

And I *am* getting "Undefined index" notices in my log, and in every case, a backtrace() shows them coming from a theme('image' that fails to include an attributes key.

I will produce a repeatable test case, starting with a clean minimal Drupal-7 install, shortly.

dave reid’s picture

Status: Needs review » Needs work

Please read:

http://api.drupal.org/api/drupal/includes--theme.inc/function/theme/6 - "All requests for theme hooks must go through this function."

http://techylinguist.com/code-snippet/drupal-6-theming-functions-images-... - "Where's the call to theme_image()? We NEVER call theme functions directly."

marcingy’s picture

This is duplicate in effect of this issue http://drupal.org/node/1001990. This isn't a bug this someone missusing the api.

dave reid’s picture

Yes, if you can reproduce with latest 7.x-dev core with Standard or minimal install, and no contrib modules, that would be useful. Please also include what function/module calls theme('image') from your backtrace info.

jacine’s picture

Status: Needs work » Needs review

@davereid, Yep, the errors do go away with type. Sorry, my bad. I didn't realize that theme function only works for render elements. Separate issue though, I guess. :)

FYI, I also tried printing directly in a template file, with theme_image() and still I'm not seeing any notices.

pillarsdotnet’s picture

StatusFileSize
new1.93 KB

If the function is for internal use only, it should say so.

marcingy’s picture

Status: Needs review » Closed (won't fix)

Please read this thread http://drupal.org/node/1001990 - the function already links to http://api.drupal.org/api/drupal/modules--system--theme.api.php/group/th... which explains that it is internal only. There is nothing to fix here.

The api page even gives an example - http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_image/7

Therefore, this variable defaults to an empty string, but can be set to NULL for the attribute to be omitted. Usually, neither omission nor an empty string satisfies accessibility requirements, so it is strongly encouraged for code calling theme('image') to pass a meaningful value for this variable.

pillarsdotnet’s picture

Status: Closed (won't fix) » Closed (works as designed)

@marcingy -- Thanks for the education and links.

dp07Daniel’s picture

Issue summary: View changes

What it means for internal use? Do not have a solution?