TripalFieldFormatter.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. class TripalFieldFormatter {
  3. // The default lable for this field.
  4. public static $default_label = 'Tripal Field.';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array();
  7. // The list of default settings for this formatter.
  8. public static $default_settings = array();
  9. /**
  10. * Instantiates a new TripalFieldFormatter object.
  11. *
  12. * @param $field
  13. * An array containing the field data as returned by field_info_field()
  14. * @param $instance
  15. * (Optional). Set the instance of this field when one is available. This
  16. * is necessary when working with instance specific functions such as the
  17. * formatterSettingsForm, widgetForm, etc.
  18. */
  19. public function __construct($field, $instance = NULL) {
  20. $this->field = $field;
  21. $this->instance = $instance;
  22. }
  23. /**
  24. * Provides information about the formatter for this field.
  25. *
  26. * This function corresponds to the hook_field_formatter_info() function of
  27. * the Drupal Field API.
  28. *
  29. * This is a static function as it provides default values for all of the
  30. * formatters for this field type, and thus we don't need an instantiated
  31. * object to provide this information.
  32. *
  33. * @return
  34. * An associative array with key/value paris compatible with those from the
  35. * hook_field_formatter_info() function of the Drupal Field API.
  36. *
  37. */
  38. public static function info() {
  39. $class = get_called_class();
  40. return array(
  41. 'label' => $class::$default_label,
  42. 'field types' => $class::$field_types,
  43. 'settings' => $class::$default_settings,
  44. );
  45. }
  46. /**
  47. * Provides the field's setting form.
  48. *
  49. * This function corresponds to the hook_field_formatter_settings_form()
  50. * function of the Drupal Field API.
  51. *
  52. * The settings form appears on the 'Manage Display' page of the content
  53. * type administration page. This function provides the form that will
  54. * appear on that page.
  55. *
  56. * To add a validate function, please create a static function in the
  57. * implementing class, and indicate that this function should be used
  58. * in the form array that is returned by this function.
  59. *
  60. * This form will not be displayed if the formatter_settings_summary()
  61. * function does not return anything.
  62. *
  63. * param $field
  64. * The field structure being configured.
  65. * param $instance
  66. * The instance structure being configured.
  67. * param $view_mode
  68. * The view mode being configured.
  69. * param $form
  70. * The (entire) configuration form array, which will usually have no use
  71. * here. Typically for reference only.
  72. * param $form_state
  73. * The form state of the (entire) configuration form.
  74. *
  75. * @return
  76. * A Drupal Form array containing the settings form for this field.
  77. */
  78. public function settingsForm($view_mode, $form, &$form_state) {
  79. }
  80. /**
  81. * Provides the display for a field
  82. *
  83. * This function corresponds to the hook_field_formatter_view()
  84. * function of the Drupal Field API.
  85. *
  86. * This function provides the display for a field when it is viewed on
  87. * the web page. The content returned by the formatter should only include
  88. * what is present in the $items[$delta]['values] array. This way, the
  89. * contents that are displayed on the page, via webservices and downloaded
  90. * into a CSV file will always be identical. The view need not show all
  91. * of the data in the 'values' array.
  92. *
  93. * @param $element
  94. * @param $entity_type
  95. * @param $entity
  96. * @param $langcode
  97. * @param $items
  98. * @param $display
  99. *
  100. * @return
  101. * An element array compatible with that returned by the
  102. * hook_field_formatter_view() function.
  103. */
  104. public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
  105. foreach($items as $delta => $item) {
  106. $element[$delta] = array(
  107. '#type' => 'markup',
  108. '#markup' => $item['value'],
  109. );
  110. }
  111. }
  112. /**
  113. * Provides a summary of the formatter settings.
  114. *
  115. * This function corresponds to the hook_field_formatter_settings_summary()
  116. * function of the Drupal Field API.
  117. *
  118. * On the 'Manage Display' page of the content type administration page,
  119. * fields are allowed to provide a settings form. This settings form can
  120. * be used to allow the site admin to define how the field should be
  121. * formatted. The settings are then available for the formatter()
  122. * function of this class. This function provides a text-based description
  123. * of the settings for the site developer to see. It appears on the manage
  124. * display page inline with the field. A field must always return a
  125. * value in this function if the settings form gear button is to appear.
  126. *
  127. * See the hook_field_formatter_settings_summary() function for more
  128. * information.
  129. *
  130. * @param $field
  131. * @param $instance
  132. * @param $view_mode
  133. *
  134. * @return string
  135. * A string that provides a very brief summary of the field settings
  136. * to the user.
  137. *
  138. */
  139. public function settingsSummary($view_mode) {
  140. }
  141. /**
  142. * When constructing a pager for use by a field, all pagers must have
  143. * a unique ID
  144. */
  145. protected function getPagerElementID() {
  146. return $this->field['id'];
  147. }
  148. /**
  149. * Updates a pager generated by theme('pager') for use with AJAX.
  150. *
  151. * Because fields are meant to be updated by AJAX we need clicks in the pager
  152. * to not reload the entire page but rather to only reload the field.
  153. * Therefore the links in the pager must be adjusted to support this.
  154. *
  155. * @param $pager
  156. * The pager as created by theme('pager')
  157. * @param $entity
  158. * The entity object.
  159. */
  160. protected function ajaxifyPager($pager, $entity) {
  161. $field_id = 'tripal-entity-' . $entity->id . '--' . $this->field['field_name'];
  162. $pager = preg_replace('/href="\/.+\?page=(.+?)"/', 'href="javascript:void(0)" onclick="tripal_navigate_field_pager(\'' . $field_id . '\', $1)"', $pager);
  163. $pager = preg_replace('/href="\/.+"/', 'href="javascript:void(0)" onclick="tripal_navigate_field_pager(\'' . $field_id . '\', 0)"', $pager);
  164. $pager = '<img src="/' . drupal_get_path('module', 'tripal') . '/theme/images/ajax-loader.gif" id="' . $field_id . '-spinner" class="tripal-field-ajax-spinner">' . $pager;
  165. return $pager;
  166. }
  167. }