2

First, I have tried for several hours to weed through the various search results of the keywords above. I've found several things that seem to answer this but they're for D7, one of them from 2012 and I am pretty sure things are gosh darn different now.

I am also NOT a programmer, nor a web dev professional, just someone trying to update a very old site that's worked fine but it's time to freshen. I have some experience as a content manager with Drupal 6, but never got into coding, building modules, editing tpl files, or anything like that. I am not afraid to copy a file, open it in an editor, change this or that, cut or paste, etc., but I don't see myself being able to build a module to do this.

And maybe there's no way to do this "easily."

I want to replicate a Google Map with a custom search feature and some other tweaks. It works nicely in the existing site and is basically 3 parts: a code here in the head. Some HTML and script in the body. And a call within the tag:

<body onload="load(); curvycurvy()" onunload="GUnload()">

All I am hoping to do is properly add this code on my new Drupal 8 homepage. I've used the Asset Injector Module to add the bulk of the script to the of the page and used to (I hope) restrict it to show only on the head of the first home page. I have chosen PHP for the Content and it seems to retain the script and such there on the page content.

But is there a way to add this code above into the body? Or failing that, a way to provide similar functionality? I feel like this shouldn't (theoretically) be too hard and plenty of people must have asked this same question, so I must be asking it wrong -- or perhaps people are more high level and DO create their own modules and such.

I was hoping that the Asset Injector has some tools like, "Add to the Body Tag" or such, that would have been exactly what I need.

Again, I apologize in advance for a newbie, perhaps bozo question, and yet feel like a fresh, D8-ready answer for it must be needed by more folks than just myself. Thank you for any help, suggestions, tutorials, or walkthroughs. Or if it's just beyond the capabilities of what Drupal does, I'll ditch the map. I like the map though. :-) Would love to keep it if it's feasible.

  • Are you using a custom theme on the website? Or a 3rd party theme? (Go to www.mysite.com/admin/appearance to check. If you are using a custom theme, it can be done fairly easily with a little bit of code. – 100pic Dec 19 '18 at 1:32
0

It's not really considered good practice, but since you are already using the Asset Injector Module, you could inject some jQuery to replace the existing code written in the body tag:

/* This replaces "<body onload="load(); curvycurvy()" */
$(document).on('load', function(){
   load(); 
   curvycurvy();
});

/* This replaces onunload="GUnload()"> */
$(window).unload(function() {
  GUnload();
});
0

This is fairly easy and covered well in the Javascript API section of the drupal docs.

Effectively you want a Javascript file that creates a Drupal behavior (this is automatically fired when the document is ready). Inside the behavior, just call curvycurvy();.

https://www.drupal.org/docs/8/api/javascript-api/javascript-api-overview

So, for starters...

Drupal.behaviors.initCurvy = {
  attach: function (context, settings) {
    curvycurvy();
  }
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.