Created
September 12, 2019 23:34
-
-
Save lincoln-chawora/2f850b2014ddadc349f806d28d3e81e4 to your computer and use it in GitHub Desktop.
How to add (class and data) attributes to a link through field preprocessor in drupal 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements hook_preprocess_field(). | |
*/ | |
function YOUR_THEME_preprocess_field(&$variables, $hook) { | |
if ($variables['element']['#bundle'] == 'bundle_name') { | |
$link_attributes = &$variables['items'][0]['#path']['options']['attributes']; | |
// Add colorbox-node class to class attribute | |
$link_attributes['class'] = ['colorbox-node']; | |
// Add data-href to link with path. | |
$link_attributes['data-href'] = $variables['items'][0]['#path']['path']; | |
// Add query parameters to url. | |
$variables['items'][0]['#path']['options']['query'] = array( | |
'width' => '500', | |
'height' => '500' | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment