<?php
if (isset($link_options['attributes']['class'])) {
$link_options['attributes']['class'] = array($link_options['attributes']['class']);
}
?>
The above doesn't work when passing an array of classes with a preprocess function in the way that classes are normally added to things in Drupal. Like this:
<?php
function mymodule_preprocess_node(&$vars) {
$vars['content']['field_link'][0]['#element']['attributes']['class'] = array('class1', 'class2');
}
?>
A potential solution is as follows, although something more elegant should probably done.
<?php
if (isset($link_options['attributes']['class']) && !is_array($link_options['attributes']['class'])) {
$link_options['attributes']['class'] = array($link_options['attributes']['class']);
}
?>
Comments
Comment #1
wernerglinka commentedI just ran into this problem. I am using multiple classes to add icon fonts to links. Multiple classes must be in an array, one class per array key. That is how the menu system creates the links. The link module puts multiple classes into an array as well but all classes are located in ['options']['attributes']['class'][0]. So I don't think the above will work since $link_options['attributes']['class'] is always set.
Comment #2
wernerglinka commentedIn link.module at line 922 there is this code:
if you change this to:
This adds an array of classes rather than a string of classes to the classes array.
Comment #3
mfernea commentedI think both ideas are good. I'm uploading a patch based on both.
Comment #4
cinnamon commentedNot sure if this is related to the original problem/use case, but we still have problems with link module and classes.
As it turned out it was a serialized array of classes! This patch also accounts for that
Comment #5
cinnamon commentedNevermind, although I did see serialized data with dpm, it turned out we still got the fatal array to string conversion error :(
Am on a deadline and a link field is not that hard to implement, sorry....
Comment #6
dqdCan anyone can pick on this? Reproduce? And/Or confirm this bug again please? If this is still an issue, I would love to see progress on this and chime in later, but there are other issues of link module already on my table. Thanks for the patches so far ...
Comment #7
mfernea commentedYes, this is still an issue on 7.x-1.x branch. The patch at #4 doesn't fix the array problem. So, I would go for #3. Please check, maybe I'm subjective :).
Comment #8
dalemoore commentedIs there a preferred method for adding classes programmatically to the anchor rather than through the UI in the Additional Classes input? I've been trying to add additional classes using template_preprocess_field but it's completely ignored.
Here's a non-working example:
Comment #9
ambient.impactI can confirm I've run into this with 7.x-1.4. I set classes as an array in
template_preprocess_field(), and I got adrupal_attributes()error about a string to array conversion, and the classes didn't apply. The patch in #3 seems to fix that, and give us the expected behaviour. I hope this can be released soon. :|Comment #10
ambient.impact@dalemoore: I'd suggest opening a separate issue/support request in the future. At first glance, it looks like you're looping through all field items (good), but then setting the class only on the first index (
$variables['items'][0]- not so good), which might be the culprit. Either use$deltain place of0there, or do what I usually do and make$itema reference (&$item) in theforeachstatement, allowing you to set the class without the full nested array:If none of that works, the standard advice is to check the function name to make sure you've got the name of your theme or module right, and also to clear the cache so Drupal will pick up the function.
Comment #12
pifagor commentedFixed
Comment #13
pifagor commentedComment #14
ciss commentedNote: This change was reverted in 6f1bf5 (no associated issue).