You are here

token_insert_text.module in Token Insert 6.2

Same filename and directory in other branches
  1. 7.2 token_insert_text/token_insert_text.module

token insert module.

File

token_insert_text/token_insert_text.module
View source
<?php

/**
 * @file
 * token insert module.
 */

/**
 * Implements hook_elements().
 */
function token_insert_text_elements() {
  $type = array();
  if (user_access('use token insert')) {
    $type['textfield'] = array(
      '#post_render' => array(
        'token_insert_text_post_render',
      ),
    );
    $type['textarea'] = array(
      '#post_render' => array(
        'token_insert_text_post_render',
      ),
    );
  }
  return $type;
}
function token_insert_text_form_alter(&$form, $form_state, $form_id) {
  if ($form['#id'] == 'node-form' || $form['#id'] == 'comment-form') {
    foreach ($form as $key => $fieldset) {
      if (is_array($fieldset) && !empty($fieldset)) {
        if (isset($fieldset['#type']) && $fieldset['#type'] == 'fieldset') {
          foreach ($fieldset as $key2 => $element) {
            if (isset($element['#type']) && $element['#type'] == 'textarea') {
              $form[$key][$key2]['#token_insert'] = TRUE;
            }
          }
        }
        elseif (isset($fieldset['#type']) && $fieldset['#type'] == 'textarea') {
          $form[$key]['#token_insert'] = TRUE;
        }
        elseif (array_key_exists('body', $fieldset)) {
          $form[$key]['body']['#token_insert'] = TRUE;
        }
      }
    }
  }
}
function token_insert_text_post_render($content, $element) {
  if (isset($element['#token_insert']) && $element['#token_insert']) {
    module_load_include('inc', 'token_insert', 'token_insert');
    drupal_add_js(drupal_get_path('module', 'token_insert') . '/token_insert.js');
    drupal_add_js(drupal_get_path('module', 'token_insert_text') . '/token_insert_text.js');
    $content .= drupal_get_form('token_insert_text_combobox', $element['#name']);
  }
  return $content;
}
function token_insert_text_combobox(&$form_state, $name) {
  $options = token_insert_get_tokens();
  $form['token_insert_text_combobox'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#title' => t('Insert token'),
  );
  $form['token_insert_text_combobox']['token_insert_text_select'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#attributes' => array(
      'id' => "token-insert-text-select-{$name}",
    ),
  );
  $form['token_insert_text_combobox']['token_insert_text_button'] = array(
    '#type' => 'button',
    '#value' => t('Insert'),
    '#executes_submit_callback' => FALSE,
    '#attributes' => array(
      'class' => 'token-insert-text-button',
      'rel' => $name,
    ),
  );
  return $form;
}