views_handler_area_text.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_area_text.
  5. */
  6. /**
  7. * Views area text handler.
  8. *
  9. * @ingroup views_area_handlers
  10. */
  11. class views_handler_area_text extends views_handler_area {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function option_definition() {
  16. $options = parent::option_definition();
  17. $options['content'] = array(
  18. 'default' => '',
  19. 'translatable' => TRUE,
  20. 'format_key' => 'format',
  21. );
  22. $options['format'] = array(
  23. 'default' => NULL,
  24. );
  25. $options['tokenize'] = array(
  26. 'default' => FALSE,
  27. 'bool' => TRUE,
  28. );
  29. return $options;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function options_form(&$form, &$form_state) {
  35. parent::options_form($form, $form_state);
  36. $form['content'] = array(
  37. '#type' => 'text_format',
  38. '#default_value' => $this->options['content'],
  39. '#rows' => 6,
  40. '#format' => isset($this->options['format']) ? $this->options['format'] : filter_default_format(),
  41. '#wysiwyg' => FALSE,
  42. );
  43. // @todo Refactor token handling into a base class.
  44. $form['tokenize'] = array(
  45. '#type' => 'checkbox',
  46. '#title' => t('Use replacement tokens from the first row'),
  47. '#default_value' => $this->options['tokenize'],
  48. );
  49. // Get a list of the available fields and arguments for token replacement.
  50. $options = array();
  51. foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
  52. $options[t('Fields')]["[$field]"] = $handler->ui_name();
  53. }
  54. $count = 0;
  55. // This lets us prepare the key as we want it printed.
  56. foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
  57. $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->ui_name()));
  58. $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->ui_name()));
  59. }
  60. if (!empty($options)) {
  61. $output = '<p>'
  62. . t("The following tokens are available. If you would like to have the characters '[' and ']' please use the html entity codes '%5B' or '%5D' or they will get replaced with empty space.")
  63. . '</p>';
  64. foreach (array_keys($options) as $type) {
  65. if (!empty($options[$type])) {
  66. $items = array();
  67. foreach ($options[$type] as $key => $value) {
  68. $items[] = $key . ' == ' . check_plain($value);
  69. }
  70. $output .= theme('item_list',
  71. array(
  72. 'items' => $items,
  73. 'type' => $type,
  74. ));
  75. }
  76. }
  77. $form['token_help'] = array(
  78. '#type' => 'fieldset',
  79. '#title' => t('Replacement patterns'),
  80. '#collapsible' => TRUE,
  81. '#collapsed' => TRUE,
  82. '#value' => $output,
  83. '#id' => 'edit-options-token-help',
  84. '#dependency' => array(
  85. 'edit-options-tokenize' => array(1),
  86. ),
  87. '#prefix' => '<div>',
  88. '#suffix' => '</div>',
  89. );
  90. }
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function options_submit(&$form, &$form_state) {
  96. $form_state['values']['options']['format'] = $form_state['values']['options']['content']['format'];
  97. $form_state['values']['options']['content'] = $form_state['values']['options']['content']['value'];
  98. parent::options_submit($form, $form_state);
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function render($empty = FALSE) {
  104. $format = isset($this->options['format']) ? $this->options['format'] : filter_default_format();
  105. if (!$empty || !empty($this->options['empty'])) {
  106. return $this->render_textarea($this->options['content'], $format);
  107. }
  108. return '';
  109. }
  110. /**
  111. * Render a text area, using the proper format.
  112. */
  113. public function render_textarea($value, $format) {
  114. if ($value) {
  115. if ($this->options['tokenize']) {
  116. $value = $this->view->style_plugin->tokenize_value($value, 0);
  117. }
  118. return check_markup($value, $format, '', FALSE);
  119. }
  120. }
  121. }