Projet

Général

Profil

Paste
Télécharger (3,74 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / token_insert / token_insert_text / token_insert_text.module @ 74f6bef0

1
<?php
2
/**
3
 * @file
4
 * token insert module.
5
 */
6
function token_insert_text_element_info_alter(&$types) {
7
  if (user_access('use token insert')) {
8
    $types['textarea']['#post_render'][] = 'token_insert_text_post_render';
9
    $types['textfield']['#post_render'][] = 'token_insert_text_post_render';
10
  }
11
}
12

    
13

    
14
function token_insert_text_form_alter(&$form, &$form_state, $form_id) {
15
  if ($form_id == 'field_ui_field_edit_form' && user_access('administer token insert settings')) {
16
    $entity = $form['#instance']['entity_type'];
17
    $bundle = $form['#instance']['bundle'];
18
    $field = $form['#instance']['field_name'];
19
    $form['token_insert'] = array(
20
      '#title' => t('Token Insert'),
21
      '#type' => 'fieldset',
22
      '#collapsible' => FALSE,
23
    );
24

    
25
    $form['token_insert']['use_for_token_insert'] = array(
26
      '#type' => 'checkbox',
27
      '#title' => t('Use Token Insert for this field'),
28
      //variable_get('token_insert_content_type_*typename*_*fieldname*, 0)
29
      '#default_value' => variable_get('token_insert_' . $entity . '_' . $bundle . '_' . $field, 0),
30
      '#description' => t('If selected Token Insert will be available for this field'),
31
    );
32

    
33
    $form['#submit'][] = 'token_insert_text_add_bundle';
34

    
35
  }
36
}
37

    
38
function token_insert_text_add_bundle($form, &$form_state) {
39
  $entity = $form['#instance']['entity_type'];
40
  $bundle = $form['#instance']['bundle'];
41
  $field = $form['#instance']['field_name'];
42
  variable_set('token_insert_' . $entity . '_' . $bundle . '_' . $field,  $form_state['values']['use_for_token_insert']);
43
}
44

    
45
function token_insert_text_post_render($content, $element) {
46
  if (isset($element['#entity_type']) && isset($element['#bundle'])) {
47
    if (variable_get('token_insert_' . $element['#entity_type'] . '_' . $element['#bundle'] . '_' . $element['#field_name'], 0) == 1) {
48
      $content = _token_insert_add_token_insert($content, $element);
49
    }
50
  }
51
  if (isset($element['#token_insert']) && $element['#token_insert']) {
52
    $content = _token_insert_add_token_insert($content, $element);
53
  }
54

    
55
  return $content;
56
}
57

    
58
function _token_insert_add_token_insert($content, $element) {
59
  module_load_include('inc', 'token_insert', 'token_insert');
60
  drupal_add_js(drupal_get_path('module', 'token_insert') . '/token_insert.js');
61
  drupal_add_js(drupal_get_path('module', 'token_insert_text') . '/token_insert_text.js');
62
  if (!isset($element['summary'])) {
63
    $contentarray = token_insert_text_combobox($element);
64
    $content .= drupal_render($contentarray);
65
  }
66
  if (isset($element['#name'])) {
67
   $content .= drupal_render(drupal_get_form('token_insert_text_combobox', $element['#name']));
68
  }
69
  return $content;
70
}
71

    
72
function token_insert_text_combobox($element) {
73
  $form = array();
74
  if (isset($element['#title'])) {
75
    $options = token_insert_get_tokens();
76
    $name = isset($element['#field_name']) ? $element['#field_name'] : $element['#name'];
77
    $settings['token_insert']['buttons']["token-insert-text-button-$name"] = $element['#id'];
78
    drupal_add_js($settings, array('type' => 'setting')) ;
79
    $form['token_insert_text_combobox'] = array(
80
      '#type' => 'fieldset',
81
      '#collapsible' => TRUE,
82
      '#title' => t('Insert token'),
83
    );
84
    $form['token_insert_text_combobox']['token_insert_text_select'] = array(
85
      '#type' => 'select',
86
      '#options' => $options,
87
      '#attributes' => array(
88
        'id' => array("token-insert-text-select-$name"),
89
      ),
90
    );
91

    
92
    $form['token_insert_text_combobox']['token_insert_text_button'] = array(
93
      '#type' => 'button',
94
      '#value' => t('Insert'),
95
      '#executes_submit_callback' => FALSE,
96
      '#attributes' => array(
97
        'class' => array('token-insert-text-button'),
98
        'rel' => $name,
99
        'id' => array("token-insert-text-button-$name"),
100
      ),
101
    );
102
  }
103
  return $form;
104

    
105
}