Score:0

How do I add colorbox-load classes and attributes to links that come from an svg image?

cn flag

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.

sonfd avatar
in flag
My first thought is that it’s because you’re adding the color box classes and metadata via js.
tonytheferg avatar
cn flag
Can you post an alternate solution?
sonfd avatar
in flag
Add the class and attributes to the links before they're rendered. (Don't use javascript to add them after the page has loaded.)
tonytheferg avatar
cn flag
What is the best way to do that?
tonytheferg avatar
cn flag
The links are on an svg image, so i don't think they are accessible to Drupal backend.
sonfd avatar
in flag
Ok, alternatively, I think you could try re-attaching behaviors, e.g. at the end of your behavior, run `Drupal.attachBehaviors('.field--name-field-MY-IMAGE')`. I think the issue is that colorbox has already looked for links with the colorbox class before you add the class to your links. Using Drupal.attachBehaviors should force colorbox to recheck your svg's links
tonytheferg avatar
cn flag
classes were wrong, will update the question `$(item).attr("class", "use-ajax"); $(item).attr("data-dialog-type", "colorbox");` should be used
sonfd avatar
in flag
Did you use `Drupal.attachBehaviors()`? or was this resolved in another way? Your last comment says you're going to update the question with some new javascript you tried, however I don't see it in the question.
tonytheferg avatar
cn flag
I plan on posting what I did as an answer when I get time. I did add `Drupal.attachBehaviors()`, but I had to do that so the the classes would get added for anonymous users. The initial reason it wasn't working was that the classes I was using were not the proper classes to use alongside colorbox_load module.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.