0

I've created a behaviours in JS but when I'm not connected as admin it show in the console this error:

Can't find variable: Drupal

When I write console.log(Drupal) in the console explorer it show too the error.

How I can fix this error ?

Edit :

I've defined my library by this way :

myc_facebook:
  version: 1.x
  js:
    js/script.js: {}
    js/timeago.js: {}
  dependencies:
    - core/jquery
    - core/drupalSettings

I integrate my library in a block by this way :

public function build() {
        $config = $this->getConfiguration();

        // Check if the conf
        if (empty($config['token']) || empty($config['pagename']) || empty($config['pageurl']) || empty($config['limit']))
            return array('err' => array('#markup' => t('You might configure this block.')));

        $build = [];
        // Create the wrapper to include facebook posts
        $build['facebook']['timeline']['#markup'] = '<div id="myc-fb-posts" class="slider"><ul class="slides"></ul></div>';
        // Push conf vars
        foreach ($config as $key => $value) {
            $build['#attached']['drupalSettings']['myc_fb'][$key] = $value;
        }
        // Includes libraries
        $build['#attached']['library'][] = 'myc_facebook/myc_facebook';

        return $build;
    }
  • 1
    You should post your code. Without your code is impossible to know what's wrong. Give as much information as you can. – sanzante Dec 21 '16 at 13:43
  • For debugging switch off javascript aggregation, so that you can see which js files are loaded (or in your case are not loaded). – 4k4 Dec 21 '16 at 14:06
  • it's not code problem because on the explorer console when I write console.log(Drupal) it shows "Can't find variable: Drupal". Then for the anonymous user Drupal isn't load. – Kpone Dec 21 '16 at 14:08
3

Sounds like you're simply missing the dependency on core/drupal (and posibly also core/jquery) in your library, so that Drupal and JQuery get loaded, which by default it doesn't for anonymous users.

| improve this answer | |
  • Check main post I updated – Kpone Dec 21 '16 at 14:09
  • So you're missing the core/drupal dependency as @Berdir mentioned – Clive Dec 21 '16 at 14:20
  • 1
    core/drupal and core/drupalSettings are two different things. Add core/drupal as the first dependency and rebuild the cache and attach your libraries to the page. – Kevin Dec 21 '16 at 14:35

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.