Hi there!

First, great module, was exactly what I was looking for! Plus, the user levels functionality you added, while not exactly what I needed, did point to solving another request from my client.

I was wondering if it would be useful for this module to add in some CSS classes to the realname_theme() function, so that themers could target them, instead of having to upload images.

I think you would only need to do something like the following in realname_theme(), after the image code (starting after line 82 in realname_theme.inc)

// Format user levels as css here
foreach ($object->roles as $rid => $role) {
  // Make a nice css value out of the role title
  $l_opts['attributes']['class'] = preg_replace("/[][\\`~!@#$%^&*(){}:;<>,.?\/_|\s-]/","",strtolower($role));;
}

For better CSS readability, might want to break this into two lines, removing special characters first, and then replacing spaces with a dash:

// Format user levels as css here
foreach ($object->roles as $rid => $role) {
  // Make a nice css value out of the role title
  $l_opts['attributes']['class'] = preg_replace("/[][\\`~!@#$%^&*(){}:;<>,.?\/_|\s-]/","",strtolower($role));;
}

Seems to work for me....thoughts? I couldn't quite figure out another way to add classes while using your module at the same time, since you override theme_username().

Using:
RealName v1.3
Drupal 6.16

Comments

Stephen Scholtz’s picture

er...forgot that users can have multiple roles...code should be:

// Format user levels as css here
foreach ($object->roles as $rid => $role) {
  // Make a nice css value out of the role title
  $l_opts['attributes']['class'] = $l_opts['attributes']['class'] . preg_replace("/[][\\`~!@#$%^&*(){}:;<>,.?\/_|\s-]/","",strtolower($role)) . " ";
}

...and...

// Format user levels as css here
foreach ($object->roles as $rid => $role) {
  // Make a nice css value out of the role title
  $temp = preg_replace("/[][\\`~!@#$%^&*(){}:;<>,.?\/_|-]/","",strtolower($role)); // strip special chars
  $temp = preg_replace("/\s/","-", $temp); // replace spaces with dashes
  $l_opts['attributes']['class'] = $l_opts['attributes']['class'] . $temp . " ";  // Assign and add a space
}

...and one too many semicolons in there too.

nancydru’s picture

Status: Needs review » Active

"Needs review" indicates that an actual patch is attached.

BTW, http://api.drupal.org/api/function/form_clean_id/6 might be better than preg-replace.

nancydru’s picture

Status: Active » Fixed

Committed to 6.x-1.x-dev.

Stephen Scholtz’s picture

Nice, thanks for pointing me at form_clean_id, another tool in my belt. :) And I'll use "needs review/active" correctly next time. Cheers!

Stephen Scholtz’s picture

Version: 6.x-1.3 » 6.x-1.x-dev
Status: Fixed » Active

Checked out the dev version of the module and saw your change. Thanks!

However, the class addition doesn't seem to be catching all the time. For example, the classes don't show up in the "Who's Online" Block unless the line that puts in the classes is outside of the if/else check...

Original...

        // Format user levels here.
        foreach ($object->roles as $rid => $role) {
          if (!isset($roles[$rid])) {
            // Use empty here because NULL is not set.
            $pic = variable_get('realname_user_level_'. $rid, '');
            $roles[$rid] = empty($pic) ? '' : file_create_url($pic);
            // Make a nice css value out of the role title
            $l_opts['attributes']['class'] .= form_clean_id(strtolower($role)) . ' ';

          }
          if ($roles[$rid]) {
            $name .= '<img src="'. $roles[$rid] .'" alt="'. $role .'" title="'. $role .'" />';
            $l_opts['html'] = TRUE;
          }
        }

Tweaked...

        // Format user levels here.
        foreach ($object->roles as $rid => $role) {
          if (!isset($roles[$rid])) {
            // Use empty here because NULL is not set.
            $pic = variable_get('realname_user_level_'. $rid, '');
            $roles[$rid] = empty($pic) ? '' : file_create_url($pic);
          }
          // Make a nice css value out of the role title
          $l_opts['attributes']['class'] .= form_clean_id(strtolower($role)) . ' ';
          if ($roles[$rid]) {
            $name .= '<img src="'. $roles[$rid] .'" alt="'. $role .'" title="'. $role .'" />';
            $l_opts['html'] = TRUE;
          }
        }

Pretty sure this is because I'm not using any images for my user levels, so $roles[$rid] ends up being set to nothing.

hass’s picture

Status: Active » Closed (outdated)