markup.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * @file
  4. * Webform module markup component.
  5. */
  6. /**
  7. * Implements _webform_defaults_component().
  8. */
  9. function _webform_defaults_markup() {
  10. return array(
  11. 'name' => '',
  12. 'form_key' => NULL,
  13. 'pid' => 0,
  14. 'weight' => 0,
  15. 'value' => '',
  16. 'extra' => array(
  17. 'format' => NULL,
  18. 'private' => FALSE,
  19. 'display_on' => 'form',
  20. ),
  21. );
  22. }
  23. /**
  24. * Implements _webform_theme_component().
  25. */
  26. function _webform_theme_markup() {
  27. return array(
  28. 'webform_display_markup' => array(
  29. 'render element' => 'element',
  30. 'file' => 'components/markup.inc',
  31. ),
  32. );
  33. }
  34. /**
  35. * Implements _webform_edit_component().
  36. */
  37. function _webform_edit_markup($component) {
  38. $form = array();
  39. $form['value'] = array(
  40. '#type' => 'text_format',
  41. '#title' => t('Value'),
  42. '#default_value' => $component['value'],
  43. '#description' => t('Markup allows you to enter custom HTML into your form.') . ' ' . theme('webform_token_help', array('groups' => array('node', 'submission'))),
  44. '#weight' => -1,
  45. '#format' => $component['extra']['format'],
  46. '#element_validate' => array('_webform_edit_markup_validate'),
  47. );
  48. $form['display']['display_on'] = array(
  49. '#type' => 'select',
  50. '#title' => t('Display on'),
  51. '#default_value' => $component['extra']['display_on'],
  52. '#options' => array(
  53. 'form' => t('form only'),
  54. 'display' => t('viewed submission only'),
  55. 'both' => t('both form and viewed submission'),
  56. ),
  57. '#weight' => 1,
  58. '#parents' => array('extra', 'display_on'),
  59. );
  60. return $form;
  61. }
  62. /**
  63. * Element validate handler; Set the text format value.
  64. */
  65. function _webform_edit_markup_validate($form, &$form_state) {
  66. if (is_array($form_state['values']['value'])) {
  67. $form_state['values']['extra']['format'] = $form_state['values']['value']['format'];
  68. $form_state['values']['value'] = $form_state['values']['value']['value'];
  69. }
  70. }
  71. /**
  72. * Implements _webform_render_component().
  73. */
  74. function _webform_render_markup($component, $value = NULL, $filter = TRUE, $submission = NULL) {
  75. $element = array(
  76. '#type' => 'markup',
  77. '#title' => $filter ? NULL : $component['name'],
  78. '#weight' => $component['weight'],
  79. '#markup' => $component['value'],
  80. '#format' => $component['extra']['format'],
  81. '#theme_wrappers' => array('webform_element'),
  82. '#translatable' => array('title', 'markup'),
  83. '#access' => $component['extra']['display_on'] != 'display',
  84. '#webform_nid' => isset($component['nid']) ? $component['nid'] : NULL,
  85. '#webform_submission' => $submission,
  86. '#webform_format' => $component['extra']['format'],
  87. );
  88. if ($filter) {
  89. $element['#after_build'] = array('_webform_render_markup_after_build');
  90. }
  91. return $element;
  92. }
  93. /**
  94. * Helper function to replace tokens in markup component.
  95. *
  96. * Required to have access to current form values from $form_state.
  97. */
  98. function _webform_render_markup_after_build($form_element, &$form_state) {
  99. global $user;
  100. $node = node_load($form_element['#webform_nid']);
  101. $submission = NULL;
  102. // Convert existing submission data to an in-progress submission.
  103. $form_state_for_submission = $form_state;
  104. $form_state_for_submission['values']['submitted'] = $form_state['#conditional_values'];
  105. module_load_include('inc', 'webform', 'includes/webform.submissions');
  106. $submission = webform_submission_create($node, $user, $form_state_for_submission, TRUE, $form_element['#webform_submission']);
  107. // Replace tokens using the current or generated submission.
  108. $value = webform_replace_tokens($form_element['#markup'], $node, $submission, NULL, $form_element['#webform_format']);
  109. // If the markup value has been set by a conditional, display that value.
  110. $component = $form_element['#webform_component'];
  111. if ($node) {
  112. $sorter = webform_get_conditional_sorter($node);
  113. // If the form was retrieved from the form cache, the conditionals may not
  114. // have been executed yet.
  115. if (!$sorter->isExecuted()) {
  116. $sorter->executeConditionals(isset($submission) ? $submission->data : array());
  117. }
  118. $conditional_value = $sorter->componentMarkup($component['cid'], $component['page_num']);
  119. if (isset($conditional_value)) {
  120. // Provide original value, should conditional logic no longer set the
  121. // value.
  122. $form_element['#wrapper_attributes']['data-webform-markup'] = $value;
  123. if (is_string($conditional_value)) {
  124. $value = check_markup($conditional_value, $component['extra']['format']);
  125. }
  126. }
  127. }
  128. $form_element['#markup'] = $value;
  129. return $form_element;
  130. }
  131. /**
  132. * Implements _webform_display_component().
  133. */
  134. function _webform_display_markup($component, $value, $format = 'html', $submission = array()) {
  135. $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  136. $value = webform_replace_tokens($component['value'], $node, $submission, NULL, $component['extra']['format']);
  137. // If the markup value has been set by a conditional, display that value.
  138. if ($node && is_string($conditional_value = webform_get_conditional_sorter($node)->componentMarkup($component['cid'], $component['page_num']))) {
  139. $value = check_markup($conditional_value, $component['extra']['format']);
  140. }
  141. return array(
  142. '#weight' => $component['weight'],
  143. '#theme' => 'webform_display_markup',
  144. '#format' => $format,
  145. '#value' => $value,
  146. '#translatable' => array('title'),
  147. '#access' => $component['extra']['display_on'] != 'form',
  148. );
  149. }
  150. /**
  151. * Format the output of data for this component.
  152. */
  153. function theme_webform_display_markup($variables) {
  154. $element = $variables['element'];
  155. return $element['#access'] ? $element['#value'] : '';
  156. }