0

I recently started working in collaboration with another drupal website, and we are trying to figure out how to get some of their content embedded into my site. They sent my some embed code for a widget, which is below (with some pieces changed for privacy:

<script type="text/javascript">
  widgetContext = { "url": "http://theirsite.com/mysite/[nid]", "width": "600", "height": 200, "widgetid": "web_widget_iframe_hash" };
</script>
<script id="web_widget_iframe_hash" src="http://theirsite.com/sites/all/modules/web_widgets/iframe/web_widgets_iframe.js">
</script>

The problem is that I cannot figure out how to get this embedded into my site. The widget generates calendars for venues, so the content is going to change based on which node is being viewed on my site. So, in my nodes, I added a field that stores the NID of the corresponding node on the other site so that the correct calendar is generated.

I'm trying to create a view, in order to dynamically call the above script for each page on my site. The view uses a token to take the NID field and place it into the first part of the script. However, when I place this code into a Custom Field, views strips the script tags and displays the rest of it as text. When I try to place it into a PHP field, it doesn't display at all.

Does anybody have a suggestion for how I can use the above code on my site? I'm at a bit of a loss.

1 Answer 1

1

You could add the JS in code.

e.g:

/**
 * Implements hook_init
 */
function mymodule_init(){
  if ($node = menu_get_object('node')) {
    if ($node->type == 'my_type') {
      $items = field_get_items('node', $node, 'field_my_nid_field');
      if (!empty($items[0]['value'])) {
        $nid = filter_xss($items[0]['value']);
        drupal_add_js('
        widgetContext = { "url": "http://theirsite.com/mysite/'.(int)$nid.'", "width": "600", "height": 200, "widgetid": "web_widget_iframe_hash" };
        ', array('type' => 'inline', 'scope' => 'header'));
      }
    }
  }
}

The other static script tag can be added in a custom Full HTML block / content pane or similar, where you want the content to display.

5
  • A couple of questions. In the second line of the code I originally showed, there's an id and a src, which I don't see in your implementation at all. Where are those involved in this? Also, how do I get the widget to actually render on the page? I'm assuming that hook_init calls the javascript, but then how/where will it display?
    – Mrweiner
    Feb 13, 2013 at 0:44
  • hook_init adds the javascript, the other JS can be added however you're doing it currently, as the other js has an "id" attr on the script tag, you can't add that with drupal_add_js, could do it with render() though. Feb 13, 2013 at 1:17
  • Okay I see. It's probably simplest to just place the second script into block. Is there some way to verify that the javascript from the module is actually being run? Because right now that second script is showing up on the page, in the block, just fine in firebug, but nothing is being rendered to the page.
    – Mrweiner
    Feb 13, 2013 at 3:48
  • You'll need a custom module to add the hook_init to, updated field_name to match your config ( e.g field_name and value vs target_id ) and also flush cache. Feb 13, 2013 at 4:27
  • Turns out it was an error on my end. I was inadvertently using an old version of the "widgetid." This works perfectly. Thanks so much.
    – Mrweiner
    Feb 14, 2013 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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