Comments

dawehner’s picture

The plan is to use the logic in the cache plugins to find all the changed css/js put it into the global view object
and use it on to return a really simple render-array with #attached on it.

The problem with this though it that you need to use a cache plugin
even you actually don't use one because the block cache is active.

So maybe we should better move the logic into the display/view or create a stub cache-plugin.

tim.plunkett’s picture

Issue tags: +VDC
bellesmanieres’s picture

Status: Active » Needs review
StatusFileSize
new1.56 KB

Patch attached, retrieves css and js from cache plugin, and add them as #attached to the block content.

dawehner’s picture

+++ b/lib/Drupal/views/Plugin/views/display/Block.phpundefined
@@ -68,7 +68,36 @@ class Block extends DisplayPluginBase {
+    $plugin_cache = $this->get_plugin('cache');
+    if (!$plugin_cache->cache_get('output')) {
+      $plugin_cache->gather_headers();

The cache is stored as a static cache on the display, so this code really gives you the information needed, just checked that.

+++ b/lib/Drupal/views/Plugin/views/display/Block.phpundefined
@@ -68,7 +68,36 @@ class Block extends DisplayPluginBase {
+    $attached = array(
+      'css' => $plugin_cache->storage['css'],
+      'js' => array(),
+    );
+
+    foreach ($plugin_cache->storage['js'] as $key => $args) {
+      if ($key !== 'settings') {
+        $attached['js'][] = $args;
+      }
+      else {
+        foreach ($args as $setting) {
+          $attached['js'][] = array(
+            'data' => $setting,
+            'type' => 'setting',
+          );
+        }
+      }

I'm wondering whether this code can be simplified by get something more usefull from the cache plugin.

bellesmanieres’s picture

Only thing I can see in term of simplification is avoiding the unnecessary iteration on files elements for js, as per attached patch.
One other solution could probably be to refactor CachePluginBase to set the $storage (and get_cache/set_cache) using an array(data, type) style, but seems quite an overhead as only settings won't be default "files" type.
Any other idea ?

dawehner’s picture

Status: Needs review » Needs work
+++ b/lib/Drupal/views/Plugin/views/display/Block.phpundefined
@@ -191,7 +216,7 @@ class Block extends DisplayPluginBase {
+        $this->save_block_cache($form_state['view']->name . '-' . $form_state['display_id'], $form_state['values']['block_caching']);
         break;
     }
   }
@@ -199,12 +224,12 @@ class Block extends DisplayPluginBase {

@@ -199,12 +224,12 @@ class Block extends DisplayPluginBase {
   /**
    * Block views use exposed widgets only if AJAX is set.
    */
-    function uses_exposed() {
-      if ($this->isAJAXEnabled()) {
-        return parent::uses_exposed();
-      }
-      return FALSE;
+  function uses_exposed() {
+    if ($this->isAJAXEnabled()) {
+      return parent::uses_exposed();
     }
+    return FALSE;
+  }
 
   /**
    * Update the block delta when you change the machine readable name of the display.
@@ -247,10 +272,10 @@ class Block extends DisplayPluginBase {

@@ -247,10 +272,10 @@ class Block extends DisplayPluginBase {
       $delta = md5($delta);
     }
     if (db_table_exists('block') && $bid = db_query("SELECT bid FROM {block} WHERE module = 'views' AND delta = :delta", array(
-        ':delta' => $delta))->fetchField()) {
+      ':delta' => $delta))->fetchField()) {
       db_update('block')
         ->fields(array(
-        'cache' => $cache_setting,
+          'cache' => $cache_setting,

As this will break some other patches please try to stay in the scope of the issue.

tim.plunkett’s picture

+++ b/lib/Drupal/views/Plugin/views/display/Block.phpundefined
@@ -68,7 +68,32 @@ class Block extends DisplayPluginBase {
-    $info['content'] = $this->view->render();
+    $content = $this->view->render();
+    // Gather needed CSS and JS into #attached property.
+    // This ensure they get cached by block cache alongside the output.
+    $plugin_cache = $this->get_plugin('cache');
+    if (!$plugin_cache->cache_get('output')) {
+      $plugin_cache->gather_headers();
+    }
+    // Adapt settings to #attached inline structure for settings.
+    if (isset($plugin_cache->storage['js']['settings']) && !empty($plugin_cache->storage['js']['settings'])) {
+      foreach ($plugin_cache->storage['js']['settings'] as $key => $args) {
+        $plugin_cache->storage['js'][$key] = array(
+          'data' => $args,
+          'type' => 'setting',
+        );
+      }
+      unset($plugin_cache->storage['js']['settings']);
+    }
+    $attached = array(
+      'css' => $plugin_cache->storage['css'],
+      'js' => $plugin_cache->storage['js'],
+    );
+
+    $info['content'] = array(
+      '#markup' => $content,
+      '#attached' => $attached,
+    );
     $info['subject'] = filter_xss_admin($this->view->get_title());
     if (!empty($this->view->result) || $this->get_option('empty') || !empty($this->view->style_plugin->definition['even empty'])) {

So what code is that actually replacing? Shouldn't there be deleted lines wherever it was added without #attached?

+++ b/lib/Drupal/views/Plugin/views/display/Block.phpundefined
@@ -68,7 +68,32 @@ class Block extends DisplayPluginBase {
@@ -191,7 +216,7 @@ class Block extends DisplayPluginBase {

@@ -191,7 +216,7 @@ class Block extends DisplayPluginBase {
         break;
       case 'block_caching':
         $this->set_option('block_caching', $form_state['values']['block_caching']);
-        $this->save_block_cache($form_state['view']->name . '-'. $form_state['display_id'], $form_state['values']['block_caching']);
+        $this->save_block_cache($form_state['view']->name . '-' . $form_state['display_id'], $form_state['values']['block_caching']);
         break;
     }
   }
@@ -199,12 +224,12 @@ class Block extends DisplayPluginBase {

@@ -199,12 +224,12 @@ class Block extends DisplayPluginBase {
   /**
    * Block views use exposed widgets only if AJAX is set.
    */
-    function uses_exposed() {
-      if ($this->isAJAXEnabled()) {
-        return parent::uses_exposed();
-      }
-      return FALSE;
+  function uses_exposed() {
+    if ($this->isAJAXEnabled()) {
+      return parent::uses_exposed();
     }
+    return FALSE;
+  }
 
   /**
    * Update the block delta when you change the machine readable name of the display.
@@ -247,10 +272,10 @@ class Block extends DisplayPluginBase {

@@ -247,10 +272,10 @@ class Block extends DisplayPluginBase {
       $delta = md5($delta);
     }
     if (db_table_exists('block') && $bid = db_query("SELECT bid FROM {block} WHERE module = 'views' AND delta = :delta", array(
-        ':delta' => $delta))->fetchField()) {
+      ':delta' => $delta))->fetchField()) {
       db_update('block')
         ->fields(array(
-        'cache' => $cache_setting,
+          'cache' => $cache_setting,
         ))
         ->condition('module', 'views')
         ->condition('delta', $delta)

This all looks unrelated. Good stuff for another issue though.

bellesmanieres’s picture

Status: Needs work » Needs review
StatusFileSize
new1.69 KB

Removed the "noise", hadn't spotted this had went in, sorry. Deleted code is only one line, as css/js were not added there at all prior to this.

aspilicious’s picture

Status: Needs review » Needs work
Issue tags: +Needs backport to D7, +VDC

The last submitted patch, 1748164-use_attached_for_block_js_css-8.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review
StatusFileSize
new1.72 KB

The file moved, and s/get_plugin/getPlugin/

Status: Needs review » Needs work

The last submitted patch, views-1748164-11.patch, failed testing.

moshe weitzman’s picture

Status: Needs work » Closed (duplicate)
dawehner’s picture

Version: 8.x-3.x-dev » 7.x-3.x-dev
Status: Closed (duplicate) » Patch (to be ported)

I think it would help to backport.

tim.plunkett’s picture

Version: 7.x-3.x-dev » 8.x-3.x-dev
Status: Patch (to be ported) » Closed (duplicate)

Dave opened a new issue: #1894736: Cannot use #attached to add general CSS/JS/Library to a View
Lets just let this one die.