Coming from #1147048: AdvAgg, LABjs, CTools - JS Settings Issues

I'll be working on a patch for CTools to make this work with AdvAgg's various hooks. If it doesn't do what we need it to out of the box then I will adjust advagg accordingly to make it happen.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mikeytown2’s picture

Title: Use AdvAgg Hooks instead of hacks in ctools_ajax_page_preprocess » CTools - Use AdvAgg Hooks instead of hacks in ctools_ajax_page_preprocess
Status: Active » Needs review
FileSize
2.64 KB

Adjustments have been made to advagg.
Here is the patch for CTools.
hook_js_replacements is no longer used, and it doesn't write a script tag to the closure anymore. Will also grab JS files loaded in other places besides the header and footer.

merlinofchaos’s picture

+  // If advagg is enabled this happens in ctools_advagg_js_header_footer_alter()
+  // and ctools_advagg_css_pre_alter().
+  if (module_exists('advagg') && variable_get('advagg_enabled', defined('ADVAGG_ENABLED'))) {
+    return;
+  }

Can we do something a little bit smarter than this? Is there an advagg hook that runs prior to the page preprocess, for example, that I could just set a variable rather than doing a module_exists? It would be nicer if this were more generic since there could be other reasons to avoid ctools doing this and possibly other modules could set that.

mikeytown2’s picture

advagg uses a hook_init. I'm thinking we could have a variable called "ctools_ajax_page_preprocess". In advagg_init I can set $conf['ctools_ajax_page_preprocess'] = FALSE;.

The variable check is the bare minimum needed to get this working (i would have to re-implement ctools_process_js_files). It would be nice if ctools_process_js_files() took $scripts as the input rather than $scope; similar to ctools_process_css_files(). Placing advagg hooks in ctools would be nice, but I can implement those hooks in advagg; have them run if CTOOLS_AJAX_INCLUDED is defined.

Here are 3 patches; the bare minimum, minimum and a patch that uses advagg hooks in ctools.

merlinofchaos’s picture

I think I prefer the min but I will listen to discussion about whether CTools should support AdvAgg or the reverse. Obviously my typical stance is that I try to push support out, but AdvAgg ties so closely to this that I can see supporting it in CTools.

That said, variable_get() is probably bad -- since variable_set() will cause a cache write every time it's called. A ctools_static() is probably a better choice than a variable_set() since the latter also persists and I'd rather do this on a per page load basis.

I totally don't mind modifying ctools_process_js_files but I'm curious if $contrib is using it. I would be more comfortable if we changed it in such a way as to not break the function signature, by adding an extra $scripts = NULL argument

if (empty($scripts)) {
  // existing behavior
}

That is the least invasive change and wont' cause someone ELSE using this to break.

mikeytown2’s picture

Good call with ctools_process_js_files. The way I was going to use variable_get didn't involve variable_set; but that might not be true for someone else. Here is the min patch again with your changes.

As for advagg hooks, if you want to dive in deep and use the $conf['advagg_css_render_function'] & $conf['advagg_js_render_function'] to have aggregates used in ctools_ajax_render instead of loading each file, that can be done; but it would require you to send the CSS & JS files that are already loaded back in the ajax request. What you have works well, just bringing this up as it gives a theme option for the CSS & JS files, something that could be useful for ajax. The other thing that could be useful is allowing anonymous functions or function callbacks in the js settings http://drupal.org/node/1148204#comment-4437152.

merlinofchaos’s picture

I think we're almost done.

Let's add a function that sets the static so that we can document what the static does and why one would use it easily. How about...ctools_skip_js_aggregation($value = FALSE);

mikeytown2’s picture

Added documentation to the functions this patch touches and fixed some spelling errors in the documentation.

New function is called ctools_ajax_run_page_preprocess()

merlinofchaos’s picture


+  $run_hook = &ctools_static(__FUNCTION__, TRUE);
+  $run_hook = $value;
+}
+
+/**
+ * Implement hook_preprocess_page. Process variables for page.tpl.php
+ *
+ * @param $variables
+ *   The existing theme data structure.
+ */
 function ctools_ajax_page_preprocess(&$variables) {
+  // See if we will be running this hook.
+  $run_hook = &ctools_static(__FUNCTION__, TRUE);

They can't both use __FUNCTION__ -- that makes them separate variables.

mikeytown2’s picture

good eye

mikeytown2’s picture

Title: CTools - Use AdvAgg Hooks instead of hacks in ctools_ajax_page_preprocess » CTools - Allow ctools_ajax_page_preprocess to not be ran & added documentation to includes/ajax.inc
mikeytown2’s picture

In regards to closer advagg integration; You don't have to send back every CSS/JS file loaded in the aggregate for ctools_ajax_render(). Just need to send back the aggregated MD5 value and CSS/JS files that are outside of the aggregate; advagg keeps a record of what files are in the aggregate so you can look up the md5 and figure out what files where loaded on the page.

JS code for pulling CSS and JS files loaded on the page.

var scriptTags = document.getElementsByTagName("script");
var scriptFiles = Array();
for (i=0; i < scriptTags.length; i++) {
  if (scriptTags[i].src !== null && scriptTags[i].src.length > 0) {
    // extract list of script files from src attribute
    scriptFiles.push(scriptTags[i].src)
  }
}
console.log('Scripts', scriptFiles);

var cssFiles = Array();
for (i=0; i<document.styleSheets.length; i++) {
  if (document.styleSheets[i].href !== null) {
    cssFiles.push(document.styleSheets[i].href);
  }
}
console.log('CSS', cssFiles);
mikeytown2’s picture

an idea when the patch in #9 will hit? The other comments are ideas, #9 is code that would be nice to have in ctools. If this is not going to be committed let me know; I have other hack-ier ways to work around this.

merlinofchaos’s picture

Sorry, have been stuck with client work and haven't had much free times for the issue queues the last 2 weeks.

Peter Bowey’s picture

#9 patch works for me with ctools + advagg
Thanks Mike!

naeluh’s picture

2 questions -

- Is this only for 6.x-1.x-dev or can this be used with the stable release of ctools
- Is this for panels 2 or 3 or does that not matter '

Cause I applied the patch on #9 and I am still returning the same problems with ajax

I am running panels 2 and ctools 6.x-1.8

thanks!

mikeytown2’s picture

@naeluh
What are your ajax problems?

naeluh’s picture

@mikeytown2
weird now its working I think I just needed to clear the cache or something, I will let you know if I experience any thing out of the ordinary thanks!
#9 worked for me!

jthomasbailey’s picture

Trying it out because I was having trouble with advagg and ajax flags, it helps a little but it isn't perfect. The throbber and the "you flagged this!" text doesn't show up and flagging occasionally doesn't work, haven't figured out what breaks it yet.

jthomasbailey’s picture

Nevermind, I think my problem has to do with the Highslide modal I'm opening the flags up in.

crea’s picture

I have an issue with ctools_ajax_render(). Together with AdvAgg it outputs unprocessed system.css which breaks the theme after an ajax request.
Should this patch cover that too, Mike ? The patch doesn't fix the issue with CTools dev snapshot from 2010-10-16.

mikeytown2’s picture

@crea
Can you give more details of what happened VS what you expected to happen? What do you mean by "unprocessed"?

crea’s picture

@mikeytown2
After ajax request unprocessed (I mean non-aggregated) system.css styles override aggregated css styles, so design breaks (a little). Ofcourse I expect design not to break with ajax. Looks like system.css styles should be already inside the aggregated files.
You can test it with my Devel Query Ajax module which uses CTools.

jcisio’s picture

Status: Needs review » Reviewed & tested by the community

I've just updated ctools and this patch still works flawlessly.

soulfroys’s picture

The patch in #9 solve my problem:
With Advagg (6.x-1.9) + Vote Up/Down (6.x-3.2), after voting, the layout breaks in a horrible way...

Thanks!

japerry’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Fixed

Looks good, fixed!

  • japerry committed b46af3d on 6.x-1.x authored by mikeytown2
    Issue #1149792 by mikeytown2: CTools - Allow ctools_ajax_page_preprocess...

Status: Fixed » Closed (fixed)

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