diff --git a/token_var.admin.inc b/token_var.admin.inc index d37e95d..0f78e68 100644 --- a/token_var.admin.inc +++ b/token_var.admin.inc @@ -5,87 +5,67 @@ * Administrative pages for the token variables. */ -/** - * Build token_var_settings_form form. - * - * @param array $form_state - * @return array The created form. +/** + * Build token_var_settings_form form. + * + * @param array $form_state + * @return array The created form. */ -function token_var_settings_form($form_state) { - $form = array(); - _token_var_get_variables($form); +function token_var_settings_form($form_state) { + $form = array(); + _token_var_get_variables($form); $form = system_settings_form($form); $form['#submit'][] .= 'token_var_settings_form_submit'; $form['#theme'] = 'token_var_token_variables_form'; - return $form; + return $form; } -/** - * Clear cache on token_var_settings_form submit. - * - * @param array $form_state - * - */ -function token_var_settings_form_submit($form_state) { - token_clear_cache(); +/** + * Clear cache on token_var_settings_form submit. + * + * @param array $form_state + * + */ +function token_var_settings_form_submit($form_state) { + token_clear_cache(); } -/** - * Add checkboxes to token_var_settings_form form. - * - * @param array $form - * +/** + * Add checkboxes to token_var_settings_form form. + * + * @param array $form + * */ function _token_var_get_variables(&$form) { global $conf; $options = array(); - foreach ($conf as $key => $var) { - $var = $conf[$key]; - if (!is_array($var)) { - $options[$key] = $key; - $form[$key]['value'] = array('#value' => '
' . $var . '
'); + $header = array( + 'name' => t("Variable Name"), + 'value' => t("Value"), + ); + + foreach ($conf as $key => $value) { + if (!is_array($value)) { + $options[$key] = array( + 'name' => $key, + 'value' => $value, + ); + } + elseif (is_array($value) && ($key != TOKENIZE_DRUPAL_VARIABLES_OPTIONS)) { + foreach ($value as $var_key => $var_value) { + $options[$key . "|" . $var_key] = array( + 'name' => $key . "|" . $var_key, + 'value' => $var_value, + ); + } } } $form[TOKENIZE_DRUPAL_VARIABLES_OPTIONS] = array( - '#type' => 'checkboxes', - '#options' => $options, - '#default_value' => variable_get(TOKENIZE_DRUPAL_VARIABLES_OPTIONS, array()), - ); - - $form['markup_data'] = array( - '#markup' => t('Check variables which you would like to be avaiable as tokens. Note: For backward compatibility all variables will be available as tokens while rendering.'), + '#type' => 'tableselect', + '#header' => $header, + '#options' => $options, + '#default_value' => variable_get(TOKENIZE_DRUPAL_VARIABLES_OPTIONS, array()), ); } - -/** - * Table theme token_var_settings_form form. - * - * @param array $form - * @return $output Themed form - */ -function theme_token_var_token_variables_form($form) { - $rows = array(); - - foreach (element_children($form['form']) as $key) { - $row = array(); - if (isset($form['form'][$key]['value'])) { - $variable = drupal_render($form['form'][TOKENIZE_DRUPAL_VARIABLES_OPTIONS][$key]); - $row[] = array('data' => $variable, 'class' => ''); - $row[] = array('data' => $form['form'][$key]['value']['#value']); - $rows[] = $row; - } - } - - // Individual table headers. - $header = array(); - $header[] = array('data' => t('Variables'), 'class' => ''); - $header[] = t('Value'); - $table_variables['header'] = $header; - $table_variables['rows'] = $rows; - $output = drupal_render($form['form']['markup_data']); - $output .= theme('table', $table_variables); - $output .= drupal_render_children($form['form']); - return $output; -} \ No newline at end of file diff --git a/token_var.info b/token_var.info index 616c61a..bdfee6c 100644 --- a/token_var.info +++ b/token_var.info @@ -1,4 +1,5 @@ name = Token Variable description = Allows you to use drupal variables as tokens core = 7.x -dependencies[] = token \ No newline at end of file +dependencies[] = token +configure = admin/config/tokenizevariables diff --git a/token_var.module b/token_var.module index 904f292..d8c2576 100644 --- a/token_var.module +++ b/token_var.module @@ -1,8 +1,8 @@ array( 'name' => t('Variables'), 'description' => t('Tokens related to drupal variables (Only variables that contain strings can be used).'), + ), + 'variables_array' => array( + 'name' => t('Variables Array'), + 'description' => t('Tokens related to drupal variables (Only variables that contain strings can be used).'), + ), ); $token_var_selected_variables = variable_get(TOKENIZE_DRUPAL_VARIABLES_OPTIONS, array()); - foreach ($token_var_selected_variables as $key) { - if ($key) { - $var = $conf[$key]; - if (!is_array($var)) { - $info['tokens']['variables'][$key] = array( - 'name' => $key, - 'description' => $var, - ); + + foreach ($conf as $key => $var) { + if ((!is_array($var)) && ($token_var_selected_variables[$key])) { + $info['tokens']['variables'][$key] = array( + 'name' => $key, + 'description' => $var, + ); + } + elseif (is_array($var) && ($key != TOKENIZE_DRUPAL_VARIABLES_OPTIONS)) { + foreach ($var as $var_key => $var_val) { + if ($token_var_selected_variables[$key . "|" . $var_key ]) { + $info['tokens']['variables_array'][$key . "|" . $var_key] = array( + 'name' => $key . "|" . $var_key, + 'description' => $var_val, + ); + } } } } @@ -43,33 +57,33 @@ function token_var_tokens($type, $tokens, array $data = array(), array $options $replacement[$original] = variable_get($name, ''); } } + elseif ($type == 'variables_array') { + foreach ($tokens as $name => $original) { + $key = substr($name, 0, strpos($name, '|')); + $index = substr($name, strpos($name, '|')+1); + $replacement_array = variable_get($key, ''); + if (is_array($replacement_array)) { + $replacement[$original] = $replacement_array[$index]; + } + else + $replacement[$original] = ''; + } + } return $replacement; } -/** - * Implements hook_menu(). - */ -function token_var_menu() { - $items = array(); - $items['admin/config/system/tokenizevariables'] = array( - 'title' => 'Tokenize Drupal Variables', - 'description' => 'Administer tokenization of drupal variables.', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('token_var_settings_form'), - 'access arguments' => array('administer token variables'), - 'file' => 'token_var.admin.inc', - ); - return $items; +/** + * Implements hook_menu(). + */ +function token_var_menu() { + $items = array(); + $items['admin/config/system/tokenizevariables'] = array( + 'title' => 'Tokenize Drupal Variables', + 'description' => 'Administer tokenization of drupal variables.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('token_var_settings_form'), + 'access arguments' => array('administer token variables'), + 'file' => 'token_var.admin.inc', + ); + return $items; } - -/** - * Implements hook_theme(). - */ -function token_var_theme() { - return array( - 'token_var_token_variables_form' => array( - 'render element' => 'form', - 'file' => 'token_var.admin.inc', - ), - ); -} \ No newline at end of file