Projet

Général

Profil

Paste
Télécharger (5,57 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / components / markup.inc @ 8d02775b

1
<?php
2

    
3
/**
4
 * @file
5
 * Webform module markup component.
6
 */
7

    
8
/**
9
 * Implements _webform_defaults_component().
10
 */
11
function _webform_defaults_markup() {
12
  return array(
13
    'name' => '',
14
    'form_key' => NULL,
15
    'pid' => 0,
16
    'weight' => 0,
17
    'value' => '',
18
    'extra' => array(
19
      'format' => NULL,
20
      'private' => FALSE,
21
      'display_on' => 'form',
22
    ),
23
  );
24
}
25

    
26
/**
27
 * Implements _webform_theme_component().
28
 */
29
function _webform_theme_markup() {
30
  return array(
31
    'webform_display_markup' => array(
32
      'render element' => 'element',
33
      'file' => 'components/markup.inc',
34
    ),
35
  );
36
}
37

    
38
/**
39
 * Implements _webform_edit_component().
40
 */
41
function _webform_edit_markup($component) {
42
  $form = array();
43
  $form['value'] = array(
44
    '#type' => 'text_format',
45
    '#title' => t('Value'),
46
    '#default_value' => $component['value'],
47
    '#description' => t('Markup allows you to enter custom HTML into your form.') . ' ' . theme('webform_token_help', array('groups' => array('node', 'submission'))),
48
    '#weight' => -1,
49
    '#format' => $component['extra']['format'],
50
    '#element_validate' => array('_webform_edit_markup_validate'),
51
  );
52

    
53
  $form['display']['display_on'] = array(
54
    '#type' => 'select',
55
    '#title' => t('Display on'),
56
    '#default_value' => $component['extra']['display_on'],
57
    '#options' => array(
58
      'form' => t('form only'),
59
      'display' => t('viewed submission only'),
60
      'both' => t('both form and viewed submission'),
61
    ),
62
    '#weight' => 1,
63
    '#parents' => array('extra', 'display_on'),
64
  );
65

    
66
  return $form;
67
}
68

    
69
/**
70
 * Element validate handler; Set the text format value.
71
 */
72
function _webform_edit_markup_validate($form, &$form_state) {
73
  if (is_array($form_state['values']['value'])) {
74
    $form_state['values']['extra']['format'] = $form_state['values']['value']['format'];
75
    $form_state['values']['value'] = $form_state['values']['value']['value'];
76
  }
77
}
78

    
79
/**
80
 * Implements _webform_render_component().
81
 */
82
function _webform_render_markup($component, $value = NULL, $filter = TRUE, $submission = NULL) {
83

    
84
  $element = array(
85
    '#type' => 'markup',
86
    '#title' => $filter ? NULL : $component['name'],
87
    '#weight' => $component['weight'],
88
    '#markup' => $component['value'],
89
    '#format' => $component['extra']['format'],
90
    '#theme_wrappers' => array('webform_element'),
91
    '#translatable' => array('title', 'markup'),
92
    '#access' => $component['extra']['display_on'] != 'display',
93
    '#webform_nid' => isset($component['nid']) ? $component['nid'] : NULL,
94
    '#webform_submission' => $submission,
95
    '#webform_format' => $component['extra']['format'],
96
  );
97

    
98
  if ($filter) {
99
    $element['#after_build'] = array('_webform_render_markup_after_build');
100
  }
101

    
102
  return $element;
103
}
104

    
105
/**
106
 * Helper function to replace tokens in markup component.
107
 *
108
 * Required to have access to current form values from $form_state.
109
 */
110
function _webform_render_markup_after_build($form_element, &$form_state) {
111
  global $user;
112
  $node = node_load($form_element['#webform_nid']);
113
  $submission = NULL;
114

    
115
  // Convert existing submission data to an in-progress submission.
116
  $form_state_for_submission = $form_state;
117
  $form_state_for_submission['values']['submitted'] = $form_state['#conditional_values'];
118
  module_load_include('inc', 'webform', 'includes/webform.submissions');
119
  $submission = webform_submission_create($node, $user, $form_state_for_submission, TRUE, $form_element['#webform_submission']);
120

    
121
  // Replace tokens using the current or generated submission.
122
  $value = webform_replace_tokens($form_element['#markup'], $node, $submission, NULL, $form_element['#webform_format']);
123

    
124
  // If the markup value has been set by a conditional, display that value.
125
  $component = $form_element['#webform_component'];
126
  if ($node) {
127
    $sorter = webform_get_conditional_sorter($node);
128
    // If the form was retrieved from the form cache, the conditionals may not
129
    // have been executed yet.
130
    if (!$sorter->isExecuted()) {
131
      $sorter->executeConditionals(isset($submission) ? $submission->data : array());
132
    }
133
    $conditional_value = $sorter->componentMarkup($component['cid'], $component['page_num']);
134
    if (isset($conditional_value)) {
135
      // Provide original value, should conditional logic no longer set the
136
      // value.
137
      $form_element['#wrapper_attributes']['data-webform-markup'] = $value;
138
      if (is_string($conditional_value)) {
139
        $value = check_markup($conditional_value, $component['extra']['format']);
140
      }
141
    }
142
  }
143

    
144
  $form_element['#markup'] = $value;
145
  return $form_element;
146
}
147

    
148
/**
149
 * Implements _webform_display_component().
150
 */
151
function _webform_display_markup($component, $value, $format = 'html', $submission = array()) {
152
  $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
153
  $value = webform_replace_tokens($component['value'], $node, $submission, NULL, $component['extra']['format']);
154

    
155
  // If the markup value has been set by a conditional, display that value.
156
  if ($node && is_string($conditional_value = webform_get_conditional_sorter($node)->componentMarkup($component['cid'], $component['page_num']))) {
157
    $value = check_markup($conditional_value, $component['extra']['format']);
158
  }
159

    
160
  return array(
161
    '#weight' => $component['weight'],
162
    '#theme' => 'webform_display_markup',
163
    '#format' => $format,
164
    '#value' => $value,
165
    '#translatable' => array('title'),
166
    '#access' => $component['extra']['display_on'] != 'form',
167
  );
168
}
169

    
170
/**
171
 * Format the output of data for this component.
172
 */
173
function theme_webform_display_markup($variables) {
174
  $element = $variables['element'];
175
  return $element['#access'] ? $element['#value'] : '';
176
}