1

When content get loaded through ajax, neither theme.css or js get applied. Below is code snippet,

controller

    $twig = \Drupal::service('twig');
        $twigFilePath = drupal_get_path('module', 'mythemename') . '/templates/test.html.twig';
        $template =  $twig->loadTemplate($twigFilePath);
      $markup = $template->render(array('gainerList' => $gainerList, 'loseList' => $looserList));

        $build = array(
            '#type' => 'markup',
            '#markup' => $markup,
            '#attached' => array(
                'css' => array(
                    drupal_get_path('themes', 'mythemename') . '/css/theme.css'
                )
            )
        );
        return new Response(render($build));

Is there any issue with above code?

Also below testing code does not hide the second

.

 $build = array(
            '#type' => 'markup',
            '#markup' => '<p>Demo text content</p> <p style="display:none">Demo text content</p>',
        );
        return new Response(render($build));

Js file code snippet:

(function ($, Drupal) {
  'use strict';

  Drupal.behaviors.company = {
    attach: function(context, settings) {
        $.ajax({
                url: "get-top-performing-fund",
                type: 'GET',
                success:function(data, error){
                    console.log(data);
                    $('#topPerforming').html();
                    $('#topPerforming').html(data);
                }
        });
    }
}
})(jQuery, Drupal);

1 Answer 1

0

Is there any issue with above code?

Yes, you're using the D7 approach. In D8 it is different. You need to create a libraries.yml file where you will define your CSS and JS files.

See:

Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 module

Adding stylesheets (CSS) and JavaScript (JS) to a Drupal 8 theme

Once you have a library you can attach it like this:

  '#attached' => array(
    'library' => array(
      'your_theme/library_name',
    ),
13
  • Thanks for quick response. I already tried that, but its not working. Even I tired to attach library in template itself {{ attach_library('themename/global-styling') }} but weird its not working
    – Sudha
    Commented Jul 5, 2018 at 9:53
  • @Sudha you need to be careful with your libraries.yml file. It is space sensitive. Wrong amount of spacing and it won't work. Also, clear all caches.
    – No Sssweat
    Commented Jul 5, 2018 at 9:55
  • 1
    yup. my theme name is associates and library name is global-styling. So it becomes associates/global-styling but its still not working. '#attached' => array( 'library' => array( 'associates/global-styling', )
    – Sudha
    Commented Jul 5, 2018 at 10:23
  • isn't global-styling being already being loaded from your theme?
    – No Sssweat
    Commented Jul 5, 2018 at 11:08
  • yes, but same does not work for ajax loaded content. :(
    – Sudha
    Commented Jul 5, 2018 at 12:06

Your Answer

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

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