Skip to content

Commit

Permalink
Issue #22. Add show_restricted and click_insert. (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
argiepiano authored Oct 16, 2024
1 parent 5f0e923 commit baf8b23
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
23 changes: 19 additions & 4 deletions fast_token_browser.module
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ function fast_token_browser_theme() {
'token_types' => array(),
'global_types' => TRUE,
'dialog' => TRUE,
'show_restricted' => FALSE,
'click_insert' => TRUE,
),
'file' => 'fast_token_browser.theme.inc',
);
Expand All @@ -70,6 +72,8 @@ function fast_token_browser_theme() {
'token_types' => array(),
'global_types' => TRUE,
'dialog' => FALSE,
'show_restricted' => FALSE,
'click_insert' => TRUE,
),
'file' => 'fast_token_browser.theme.inc',
);
Expand Down Expand Up @@ -98,8 +102,10 @@ function fast_token_browser_theme_registry_alter(&$theme_registry) {
function fast_token_browser_tree() {
$types = isset($_GET['token_types']) ? backdrop_json_decode($_GET['token_types']) : array();
$globals = isset($_GET['global_types']) ? backdrop_json_decode($_GET['global_types']) : TRUE;
$query = backdrop_get_query_parameters();
$show_restricted = $query['show_restricted'];

$out = fast_token_browser_tree_build_content($types, $globals);
$out = fast_token_browser_tree_build_content($types, $globals, $show_restricted);
$out = fast_token_browser_render($out);

if (backdrop_is_ajax()) {
Expand Down Expand Up @@ -148,8 +154,9 @@ function fast_token_browser_endpoint($type) {

$input = filter_input_array(INPUT_GET, $definition);
$ancestors = isset($input['ancestors']) ? backdrop_json_decode($input['ancestors']) : array();
$show_restricted = (isset($_GET['show_restricted']) && $_GET['show_restricted'] == 'true') ? TRUE : FALSE;

return fast_token_browser_build_level($type, $ancestors);
return fast_token_browser_build_level($type, $ancestors, $show_restricted);
}

/**
Expand Down Expand Up @@ -185,7 +192,7 @@ function fast_token_browser_endpoint_output($page_callback_result) {
* @return array
* The constructed array of types keyed by type.
*/
function fast_token_browser_tree_build_content($types = array(), $global_types = TRUE) {
function fast_token_browser_tree_build_content($types = array(), $global_types = TRUE, $show_restricted = FALSE) {
global $language;

$info = token_get_info();
Expand All @@ -203,6 +210,7 @@ function fast_token_browser_tree_build_content($types = array(), $global_types =
'token_types' => $types,
'global_types' => $global_types,
'language' => $language->langcode,
'show_restricted' => $show_restricted,
)));

$cid = 'token-browser-build:' . $hash;
Expand Down Expand Up @@ -284,10 +292,12 @@ function fast_token_browser_tree_build_content($types = array(), $global_types =
* @param array $ancestors
* The ancestors of the given type. This allows the complete token to be
* constructed.
* @param bool $show_restricted
* A flag to indicate if restricted tokens should be shown.
* @return array
* The constructed level of tokens as an array keyed by token.
*/
function fast_token_browser_build_level($type, array $ancestors = array()) {
function fast_token_browser_build_level($type, array $ancestors, $show_restricted) {
global $language;

$level = array();
Expand All @@ -305,6 +315,7 @@ function fast_token_browser_build_level($type, array $ancestors = array()) {
'type' => $type,
'ancestors' => $ancestors,
'language' => $language->langcode,
'show_restricted' => $show_restricted,
)));

if ($cache = cache_get($cid, 'cache_token')) {
Expand All @@ -318,6 +329,10 @@ function fast_token_browser_build_level($type, array $ancestors = array()) {
continue;
}

if (!$show_restricted && !empty($child_info['restricted'])) {
continue;
}

$raw_token_parts[] = $child;

if (!empty($child_info['dynamic'])) {
Expand Down
17 changes: 16 additions & 1 deletion fast_token_browser.theme.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function theme_fast_token_browser_link($variables) {
'attributes' => array(),
'query' => array(
'token' => backdrop_get_token('token-browser'),
'show_restricted' => $variables['show_restricted'] ? 1 : 0,
),
);

Expand All @@ -30,6 +31,13 @@ function theme_fast_token_browser_link($variables) {
$options['attributes']['target'] = '_blank';
}

// Pass show_restricted and click_insert flags.
$settings['tokenBrowser'] = array(
'restricted' => $variables['show_restricted'],
'click_insert' => $variables['click_insert'],
);
backdrop_add_js($settings, 'setting');

return l($variables['text'], 'token-browser/tree', $options);
}

Expand All @@ -41,7 +49,14 @@ function theme_fast_token_browser($variables) {
return theme_fast_token_browser_link($variables);
}

$content = fast_token_browser_tree_build_content($variables['token_types'], $variables['global_types']);
$content = fast_token_browser_tree_build_content($variables['token_types'], $variables['global_types'], $variables['show_restricted']);

// Pass show_restricted and click_insert flags.
$settings['tokenBrowser'] = array(
'restricted' => $variables['show_restricted'],
'click_insert' => $variables['click_insert'],
);
backdrop_add_js($settings, 'setting');

return fast_token_browser_render($content);
}
17 changes: 14 additions & 3 deletions js/fast-token-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
var tr_template;
var button_template;
var link_template;
var no_link_template;
var name_template;
var raw_template;
var description_template;
Expand All @@ -19,6 +20,7 @@
tr_template = document.createElement('tr');
button_template = document.createElement('button');
link_template = document.createElement('a');
no_link_template = document.createElement('span');
name_template = document.createElement('td');
raw_template = document.createElement('td');
description_template = document.createElement('td');
Expand Down Expand Up @@ -150,9 +152,15 @@
* The table row.
*/
function row(element, level, index) {
var click_insert = Backdrop.settings.tokenBrowser.click_insert;
var tr = tr_template.cloneNode(false);
var button = button_template.cloneNode(false);
var link = link_template.cloneNode(false);
if (click_insert) {
var link = link_template.cloneNode(false);
}
else {
var link = no_link_template.cloneNode(false);
}
var name = name_template.cloneNode(false);
var raw = raw_template.cloneNode(false);
var description = description_template.cloneNode(false);
Expand All @@ -163,8 +171,10 @@
button.addEventListener('click', expand);
button.innerHTML = 'Expand';

link.setAttribute('title', 'Select the token ' + element.raw + '. Click in a text field to insert it.');
link.addEventListener('click', select);
if (click_insert) {
link.setAttribute('title', 'Select the token ' + element.raw + '. Click in a text field to insert it.');
link.addEventListener('click', select);
}

name.setAttribute('data-token', element.token);
name.setAttribute('data-type', element.type);
Expand Down Expand Up @@ -207,6 +217,7 @@

parameters.ancestors = JSON.stringify(ancestors);
parameters.token = Backdrop.settings.fastTokenBrowser.token;
parameters.show_restricted = Backdrop.settings.tokenBrowser.restricted;

$.ajax({
'url': url,
Expand Down

0 comments on commit baf8b23

Please sign in to comment.