Flippy still uses the old method of loading all available tokens on every content type editing page. This can cause massive memory usage during content type editing on large sites and can result in out of memory errors and memcache chunk errors. Let's use the on-demand JQuery method implemented in Token 7.x-1.2. Here is the sample implementation code provided by Dave Reid in the Token issue queue.

<?php
...
  // Show the token browser.
  $form['available_tokens'] = array(
    '#value' => 'Browse available tokens',
    '#theme' => 'token_tree',
    '#token_types' => array($instance['entity_type']),
    '#weight' => 100,
    '#dialog' => TRUE,
  );
?>

The '#dialog' => TRUE is enough to use the JQuery modal on systems with the newer (3 years old) version of Token and fall back to the old in-page loading method on sites that don't support it.

Comments

jamesrward created an issue. See original summary.

jamesrward’s picture

Issue summary: View changes
jamesrward’s picture

Issue summary: View changes
jamesrward’s picture

StatusFileSize
new374 bytes

Here is a one-line patch that resolves the issue but leaves the interface a little funky as you have to click twice to browse the tokens.

jamesrward’s picture

StatusFileSize
new806 bytes

And here is a version with the interface fixed to avoid the extra click.

jamesrward’s picture

StatusFileSize
new145.14 KB

And just to be thorough here are back-to-back runs with and without the patch #5 on our site.

158Mb less peak memory used, 413,256 fewer function calls, and while it shows about 2.5 seconds off of wall time it is actually more like 30 seconds given the extra javascript that runs when PHP is done it's job. I had to click "wait" 3 times in chrome to let it finish.

spaghettibolognese’s picture

Status: Active » Needs review
StatusFileSize
new727 bytes

Nice patch, makes the UI respond way better. One small change: it's better to use theme_token_tree_link() for this task.

From https://www.drupal.org/documentation/modules/token

The preferred method for displaying the token browsing UI is via the theme_token_tree_link() function provided by the token module. Using this method will place a link on the page that when clicked will open a modal window displaying a tree like interface for browsing available tokens. This method is preferred because the list of tokens can get quite large and displaying the entire token tree and the JavaScript required to provide the collapsible interface can slow down loading of pages. Since the token tree is likely not used the majority of time we prefer to defer loading until someone clicks the links requesting the list of tokens and then load it via and ajax call.

alxgl_old’s picture

Thanks a lot @SpaghettiBolognese for your patch ! It works like a charm and allows me to edit content types without crashing my entire website !

  • rli committed d10f1a9 on 7.x-1.x authored by SpaghettiBolognese
    Issue #2557229 by jamesrward, SpaghettiBolognese: Load tokens on demand...
rli’s picture

Status: Needs review » Fixed

Committed to dev. Thanks.

Status: Fixed » Closed (fixed)

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