views_data_export_plugin_style_export_xml.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @file
  4. * Plugin include file for export style plugin.
  5. */
  6. /**
  7. * Generalized style plugin for export plugins.
  8. *
  9. * @ingroup views_style_plugins
  10. */
  11. class views_data_export_plugin_style_export_xml extends views_data_export_plugin_style_export {
  12. /**
  13. * Initialize a style plugin.
  14. */
  15. function init(&$view, &$display, $options = NULL) {
  16. // View is not set in option_definition(), so we fill defaults here if
  17. // options are empty.
  18. if (empty($options['root_node']) || empty($options['item_node'])) {
  19. $base_object_name = rtrim($view->base_table, 's');
  20. if (empty($options['root_node'])) {
  21. $options['root_node'] = $base_object_name . 's';
  22. }
  23. if (empty($options['item_node'])) {
  24. $options['item_node'] = $base_object_name;
  25. }
  26. }
  27. parent::init($view, $display, $options);
  28. }
  29. /**
  30. * Set options fields and default values.
  31. *
  32. * @return
  33. * An array of options information.
  34. */
  35. function option_definition() {
  36. $options = parent::option_definition();
  37. $options['transform'] = array(
  38. 'default' => TRUE,
  39. 'translatable' => FALSE,
  40. );
  41. $options['transform_type'] = array(
  42. 'default' => 'dash',
  43. 'translatable' => FALSE,
  44. );
  45. $options['root_node'] = array(
  46. 'default' => '',
  47. 'translatable' => FALSE,
  48. );
  49. $options['item_node'] = array(
  50. 'default' => '',
  51. 'translatable' => FALSE,
  52. );
  53. $options['no_entity_encode'] = array(
  54. 'default' => array(),
  55. 'translatable' => FALSE,
  56. );
  57. $options['cdata_wrapper'] = array(
  58. 'default' => array(),
  59. 'translatable' => FALSE,
  60. );
  61. return $options;
  62. }
  63. /**
  64. * Options form mini callback.
  65. *
  66. * @param $form
  67. * Form array to add additional fields to.
  68. * @param $form_state
  69. * State of the form.
  70. * @return
  71. * None.
  72. */
  73. function options_form(&$form, &$form_state) {
  74. parent::options_form($form, $form_state);
  75. $form['transform'] = array(
  76. '#type' => 'checkbox',
  77. '#title' => t('Transform spaces'),
  78. '#default_value' => $this->options['transform'],
  79. '#description' => t('Transform spaces to valid XML in field labels (spaces create invalid XML markup). Note that invalid XML tag characters will always be converted.'),
  80. );
  81. $form['transform_type'] = array(
  82. '#type' => 'select',
  83. '#title' => t('Transform type'),
  84. '#default_value' => $this->options['transform_type'],
  85. '#options' => array(
  86. 'dash' => t('Dash'),
  87. 'underline' => t('Underline'),
  88. 'camel' => t('camelCase'),
  89. 'pascal' => t('PascalCase'),
  90. ),
  91. '#process' => array('ctools_dependent_process'),
  92. '#dependency' => array(
  93. 'edit-style-options-transform' => array(TRUE),
  94. ),
  95. );
  96. $form['root_node'] = array(
  97. '#type' => 'textfield',
  98. '#title' => t('Root node'),
  99. '#default_value' => $this->options['root_node'],
  100. '#description' => t('The XML tag for the root node.'),
  101. );
  102. $form['item_node'] = array(
  103. '#type' => 'textfield',
  104. '#title' => t('Item node'),
  105. '#default_value' => $this->options['item_node'],
  106. '#description' => t('The XML tag for an item node.'),
  107. );
  108. $field_labels = $this->display->handler->get_field_labels();
  109. if (!empty($field_labels)) {
  110. $options = $field_labels;
  111. if (empty($this->options['no_entity_encode'])) {
  112. $this->options['no_entity_encode'] = array();
  113. }
  114. $form['no_entity_encode'] = array(
  115. '#type' => 'checkboxes',
  116. '#title' => t('Disable encoding of XML entities for these fields'),
  117. '#options' => $options,
  118. '#default_value' => $this->options['no_entity_encode'],
  119. '#description' => t('If checked field contents will be outputted '.
  120. '<em>without encoding</em> of XML entities. This is '.
  121. 'useful when when used in conjunction with a field '.
  122. 'formatter that outputs properly formatted and '.
  123. 'encoded XML data.'),
  124. );
  125. if (empty($this->options['cdata_wrapper'])) {
  126. $this->options['cdata_wrapper'] = array();
  127. }
  128. $form['cdata_wrapper'] = array(
  129. '#type' => 'checkboxes',
  130. '#title' => t('Fields value to wrapped using CDATA'),
  131. '#options' => $options,
  132. '#default_value' => $this->options['cdata_wrapper'],
  133. '#description' => t('If checked the fields content will be wrapped using the CDATA tag.'),
  134. );
  135. }
  136. }
  137. /**
  138. * Perform any necessary changes to the form values prior to storage.
  139. * There is no need for this function to actually store the data.
  140. *
  141. * @param $form
  142. * @param $form_state
  143. */
  144. function options_submit(&$form, &$form_state) {
  145. if (isset($form_state['values']['style_options']['no_entity_encode'])) {
  146. // Remove any options values set to 0
  147. $form_state['values']['style_options']['no_entity_encode'] = array_filter($form_state['values']['style_options']['no_entity_encode']);
  148. }
  149. if (isset($form_state['values']['style_options']['cdata_wrapper'])) {
  150. // Remove any options values set to 0
  151. $form_state['values']['style_options']['cdata_wrapper'] = array_filter($form_state['values']['style_options']['cdata_wrapper']);
  152. }
  153. }
  154. }