Hi,

What is the best way to add css classes to the link on a rendered entity ?
I want to add the drupal css class "button" so that it is rendered like an HTML button.
By the way this could be a nice feature to add a checkbox "Make it a button instead of a link" don't you think ?
I know that's what the Button Field module already does, but it actually did not work for me so Rules link saved my day.

Comments

Nicolas Bouteille’s picture

For now I have added the "button" css class directly in the module on line 197 of rules_link.module

'#attributes' => array('class' => array('rules-link button'), 'rel' => 'nofollow'),

but I would like to know how I should do it in my template.tpl.php file or if there is another and better way.

Nicolas Bouteille’s picture

I actually also want to add a description below the button... so that makes me wonder, is the link a Drupal Field that I could actually theme using field--field-name.tpl.php ?

Nicolas Bouteille’s picture

Ok for now I have hardcoded my description inside the "#suffix" parameter on line 199. This is so wrong + prevents me from using more than one Rules link on the whole website.
Really need some help on how I'm supposed to do that the right way. Thanks

Adon Irani’s picture

Category: support » feature

I've got the same use case here.
(Thanks Nicolas for the helpful info above).

Hoping to see this supported in a future release.

Kind regards,
Adon

Ismail Cherri’s picture

Issue summary: View changes

Hi,
I've stumbled upon the same problem and here's how I solved it without hacking the module code:

function YOURTHEMENAME_preprocess_rules_link(&$vars) {
	if ($vars['rules_link']->name == 'RULES_LINK_MACHINE_NAME'){
		$vars['classes_array'][] = 'CLASSES YOU WANT TO ADD';
		$vars['attr'] = drupal_attributes(array(
					'title' => $vars['title'], 
					'class' => $vars['classes_array'],
					'rel' => 'nofollow',
				));
	}
}

Insert this in your theme template.php function then clear your cache.
Hope this helps someone out there :)

Nicolas Bouteille’s picture

Awesome thanks!
This time I needed a way to change the text with some HTML, perfect place to do it!

bazzmann’s picture

Hi there, to us this function break the rules link functionality and it not pass the arguments correctly, displaying the json values instead.

Someone has tested it, without problems?

Thanks in advance,
M.

bazzmann’s picture

SOLVED: we solved the issue including the generated classes in the array to let ajax do the magic.

Thanks. :)
M.

apmsooner’s picture

Status: Active » Closed (fixed)

Another option is overriding the included tpl.php file accordingly. Nevertheless, this issue sounds resolved.

maxplus’s picture

Hi,
I have tried #5 but I'm getting this error

Recoverable fatal error: Argument 1 passed to drupal_attributes() must be of the type array, string given, called in .../sites/all/modules/contrib/rules_link/rules_link.module on line 478 and defined in drupal_attributes() (regel 2435 van .../includes/common.inc).

I used the advise of #9 (I have put rules-lin.tpl.php in my theme folder and changed it a little bit) but then all rules links are themed in the same way.
=> It would be cool if we could create tpl files for specific rules links, something like rules-link--machine-name.tpl.php

apmsooner’s picture

@maxplus,
I agree it would be nice to have more specific templates and while I tried myself to add a template suggestion by rules link name, my template never seems to get picked up in my theme. If i copy the basic rules-link.tpl.php file to my theme... it loads fine. Maybe someone else can comment on why this doesn't work:

/**
 * Implements hook_preprocess().
 * Preprocess theme variables.
 */
function MY_THEME_preprocess_rules_link(&$variables) {
  $rules_link = $variables['rules_link'];
  $variables['theme_hook_suggestions'][] = 'rules_link__' . $rules_link->name;
}