0

I have two days I'm trying to develop my custom module. I want to display map in block but nothing happened. how i can called css an js file and show them in page or block?

function exemple_init() {
  drupal_add_js('http://maps.googleapis.com/maps/api/js?sensor=false', 'external');
}

function exemple_block_info() {
  $blocks['exemple'] = array(
    'info' => t('Google Map Exemple'),
    'cache' => DRUPAL_NO_CACHE
  );
  return $blocks;
}

function exemple_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'exemple':
        $block['content'] = '';
  }
  return $block;
}
1

Firstly adding a JS file globally in hook_init function is wrong unless you want the file to be loaded on each page of your site. To add a JS file to a block use the #attached property in hook_block_view

eg:


'#attached' => array(
      'css' => array(
        drupal_get_path('module', 'mymodule') . '/css/mymodule.css',
      ),
      'js' => array(
        drupal_get_path('module', 'mymodule') . '/js/mymodule.js',
    ),
  ),

Secondly, I assume you want to show an address from Google Maps in your block. In that case pull the embed code from Google Maps https://support.google.com/maps/answer/3544418?hl=en and use that embed code in the block.

Sample Code for it: http://pastebin.com/5h1c2SBU

Though still curious about the JS file.

  • Agreed. And even simpler for a static location to just add a block via block UI and paste code in there with appropriate filter setting. – J. Reynolds Jun 12 '15 at 5:13

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.