We still need some way to provide assets (css, js, etc) related to the view, right? If this is implemented, I did not see where it was at. Thanks

Comments

Caseledde’s picture

The core caching mechanism is aware of related attachements.

There is an attribute for renderable arrays called '#attached'. This is an array with needed js, css and/or libraries. It will be cached together with the rendered output.

The responsible funktion drupal_process_attached() will be called everytime, regardless of whether the display is cached or not. This function will call drupal_add_js() and similar functions.

fmizzell’s picture

Status: Active » Fixed

I was not aware of #attached. thanks

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

ey’s picture

Version: » 7.x-1.0
Issue summary: View changes
Status: Closed (fixed) » Active

Sorry for re-opening the issue but I couldn't get it working on my project. I had some drupal_add_js and drupal_add_css calls in my template_preprocess_node implementation.

I've updated those to use #attached attribute instead, they are working for the first page load as expected, but after the view is cached, the css and js files won't load.

Am I adding them to a wrong place?

My current implementation:

function mymodule_preprocess_node(&$variables) {
  if ($variables['type'] == 'mytype') {
    $content = &$variables['content'];

    $module_path = drupal_get_path('module', 'mymodule');

    $content['#attached']['js'][] = array(
      'data' => $module_path . '/js/myjs.js',
      'type' => 'file',
    );

    if ($variables['view_mode'] == 'full') {
      $node['#attached']['css'][] = array(
        'data' => $module_path . '/css/mycss.css',
        'type' => 'file',
        'media' => 'screen',
      );
    }
  }
}

Thanks.

Caseledde’s picture

Hi,

the preprocess seems a bit late. You may tryout hook_node_view_alter() or hook_entity_view_alter() instead.

ey’s picture

Status: Active » Closed (fixed)

Hi Caseledde, thanks a lot, it worked :)