TripalFieldFormatter.inc

File

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