I have SVG images that are added to nodes via svg_image_field module. They have internal links in them /load-parts-ajax/{nid}
. The path leads to a pop-up page view of content with this path /load-parts-ajax/%
. I am attempting open the view in a colorbox. I have this in my implementation in Drupal 7, which works fine. We installled the colorbox_node module, and used the colorbox-node
class and this was very easy.
For Drupal 9 however, I have colorbox installed, and it's library and have tried colorbox_load, with ng_lightbox, and I added the path to the allowed paths in the configuration with no dice. I am also trying to colorbox_simple_load to call the colorbox-load
class with no luck.
(I can hack render the whole view page site and all by stuffing cboxElement
after the class but I am certain that is wrong.)
Here is the .js:
(function ($, Drupal) {
Drupal.behaviors.MY_MODULE = {
attach: function (context, settings) {
$(document).ajaxStop(function() {
setTimeout(function () { //necessary to add a brief pause for colorbox to reintiate
$(".view-MY-POP-UP-VIEW", context).once().flexslider({
animation: "slides",
slideshow: false,
touch: false,
animationLoop: false,
});
}, 500);
});
$(".field--name-field-MY-IMAGE .svg_a", context).each(function(index, item) {
$(item).attr("href", $(item).attr("xlink:href"));
$(item).attr("data-inner-width", "304");
$(item).attr("data-inner-height", "532");
$(item).attr("class", "colorbox-load");
});
}
};
})(jQuery, Drupal);
And here is how I am calling my .js libraries, one where the SVG is rendered in a view, one for the node display.
/**
* Implements hook_views_pre_render().
*/
function MY_views_pre_render(\Drupal\views\ViewExecutable $view) {
if (isset($view) && $view->storage->id() == 'MY_VIEW') {
$view->element['#attached']['library'][] = 'MY_MODULE/MY-LIBRARY';
}
}
/**
* Implements hook_preprocess_node().
*/
function MY_preprocess_node(&$variables) {
$node = $variables['node'];
if ($node->bundle() == 'MY_NODE') {
$variables['#attached']['library'][] = 'MY_MODULE/MY-LIBRARY';
}
}
The attributes from the .js file are getting added to the image links in both cases, but the colorbox-load
class does not seem to get recognized as the links just open the view in a new window.
Here is the markup, which looks identical to the working drupal 7 markup (aside from using colorbox-load class instead of colorbox-node)
<a id="e3_hyperlink" xlink:title="" xlink:href="/load-parts-ajax/251" target="_blank" class="colorbox-load" href="/load-parts-ajax/251" data-inner-width="304" data-inner-height="532">
So before I wrongly open a bug report, is there something simple I am missing for implementing the colorbox-load
class properly, or should something like this work just like it is?
EDIT:
The colorbox_simple_load has this in it's project page:
$url = Url::fromUri('internal:/example.html');
$link = Link::fromTextAndUrl(t('View'), $url);
$output = $link->toRenderable();
$output['#attributes']['class'] = [
'colorbox-load',
];
colorbox_simple_load_page_attachments($output);
So I could put this in my custom module, but im not sure what sort of hook I would need to use to implement it on the SVG images.