title.core.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * @file
  4. * Provide field replacement information.
  5. *
  6. * Provide field replacement information for core entities and type specific
  7. * callbacks.
  8. */
  9. /**
  10. * Implements hook_entity_info().
  11. */
  12. function title_entity_info() {
  13. $info = array();
  14. $general_settings = variable_get('title_general', array('maxlength' => 255));
  15. $field = array(
  16. 'type' => 'text',
  17. 'cardinality' => 1,
  18. 'translatable' => TRUE,
  19. 'settings' => array(
  20. 'max_length' => $general_settings['maxlength'],
  21. ),
  22. );
  23. $instance = array(
  24. 'required' => TRUE,
  25. 'settings' => array(
  26. 'text_processing' => 0,
  27. ),
  28. 'widget' => array(
  29. 'weight' => -5,
  30. ),
  31. 'display' => array(
  32. 'default' => array(
  33. 'type' => 'hidden',
  34. ),
  35. ),
  36. );
  37. $info['node'] = array(
  38. 'field replacement' => array(
  39. 'title' => array(
  40. 'field' => $field,
  41. 'instance' => array(
  42. 'label' => t('Title'),
  43. 'description' => '',
  44. ) + $instance,
  45. 'additional keys' => array(
  46. 'format' => 'format',
  47. ),
  48. ),
  49. ),
  50. 'efq bundle conditions' => TRUE,
  51. );
  52. if (module_exists('taxonomy')) {
  53. $info['taxonomy_term'] = array(
  54. 'field replacement' => array(
  55. 'name' => array(
  56. 'field' => $field,
  57. 'instance' => array(
  58. 'label' => t('Name'),
  59. 'description' => '',
  60. ) + $instance,
  61. 'preprocess_key' => 'term_name',
  62. ),
  63. 'description' => array(
  64. 'field' => array(
  65. 'type' => 'text_with_summary',
  66. ) + $field,
  67. 'instance' => array(
  68. 'required' => FALSE,
  69. 'label' => t('Description'),
  70. 'description' => '',
  71. 'settings' => array(
  72. 'text_processing' => 1,
  73. ),
  74. ) + $instance,
  75. 'callbacks' => array(
  76. 'submit' => 'title_field_term_description_submit',
  77. ),
  78. 'additional keys' => array(
  79. 'format' => 'format',
  80. ),
  81. ),
  82. ),
  83. );
  84. }
  85. if (module_exists('comment')) {
  86. $info['comment'] = array(
  87. 'field replacement' => array(
  88. 'subject' => array(
  89. 'field' => $field,
  90. 'instance' => array(
  91. 'label' => t('Subject'),
  92. 'description' => '',
  93. ) + $instance,
  94. 'preprocess_key' => 'title',
  95. ),
  96. ),
  97. );
  98. }
  99. return $info;
  100. }
  101. /**
  102. * Submit callback for the taxonomy term description.
  103. */
  104. function title_field_term_description_submit($entity_type, $entity, $legacy_field, $info, $langcode, &$values) {
  105. if (!isset($values['description'])) {
  106. $values['description'] = array();
  107. }
  108. foreach (array('value', 'format') as $key) {
  109. if (isset($entity->{$info['field']['field_name']}[$langcode][0][$key])) {
  110. $values['description'][$key] = $entity->{$info['field']['field_name']}[$langcode][0][$key];
  111. }
  112. // If the keys are not defined an empty value has been submitted, hence we
  113. // need to update the term description accordingly.
  114. else {
  115. $values['description'][$key] = ($key == 'value') ? '' : filter_default_format();
  116. }
  117. }
  118. }
  119. /**
  120. * Sync callback for the text field type.
  121. */
  122. function title_field_text_sync_get($entity_type, $entity, $legacy_field, $info, $langcode) {
  123. $value = NULL;
  124. $format = 'plain_text';
  125. $info += array(
  126. 'additional keys' => array(
  127. 'format' => 'format',
  128. ),
  129. );
  130. $format_key = $info['additional keys']['format'];
  131. $field_name = $info['field']['field_name'];
  132. // Return values only if there is any available to process for the current
  133. // language.
  134. if (!empty($entity->{$field_name}[$langcode]) && is_array($entity->{$field_name}[$langcode])) {
  135. $item = $entity->{$field_name}[$langcode][0] + array(
  136. 'value' => NULL,
  137. 'format' => NULL,
  138. );
  139. $value = $item['value'];
  140. $format = $item['format'];
  141. }
  142. return array(
  143. $legacy_field => $value,
  144. $format_key => $format,
  145. );
  146. }
  147. /**
  148. * Sync back callback for the text field type.
  149. */
  150. function title_field_text_sync_set($entity_type, $entity, $legacy_field, $info, $langcode) {
  151. $entity->{$info['field']['field_name']}[$langcode][0]['value'] = $entity->{$legacy_field};
  152. }
  153. /**
  154. * Sync callback for the text with summary field type.
  155. */
  156. function title_field_text_with_summary_sync_get($entity_type, $entity, $legacy_field, $info, $langcode) {
  157. $value = NULL;
  158. $format = NULL;
  159. $info += array(
  160. 'additional keys' => array(
  161. 'format' => 'format',
  162. ),
  163. );
  164. $format_key = $info['additional keys']['format'];
  165. $field_name = $info['field']['field_name'];
  166. // Return values only if there is any available to process for the current
  167. // language.
  168. if (!empty($entity->{$field_name}[$langcode]) && is_array($entity->{$field_name}[$langcode])) {
  169. $item = $entity->{$field_name}[$langcode][0] + array(
  170. 'value' => NULL,
  171. 'format' => NULL,
  172. );
  173. $value = $item['value'];
  174. $format = $item['format'];
  175. }
  176. return array(
  177. $legacy_field => $value,
  178. $format_key => $format,
  179. );
  180. }
  181. /**
  182. * Sync back callback for the text with summary field type.
  183. */
  184. function title_field_text_with_summary_sync_set($entity_type, $entity, $legacy_field, $info, $langcode) {
  185. foreach (array('value' => $legacy_field, 'format' => $info['additional keys']['format']) as $column => $name) {
  186. if (isset($entity->{$name})) {
  187. $entity->{$info['field']['field_name']}[$langcode][0][$column] = $entity->{$name};
  188. }
  189. }
  190. }
  191. /**
  192. * Process variables for page.tpl.php.
  193. */
  194. function title_process_page(&$variables) {
  195. // Ugly but necessary: there is no standardized way to tell if the current
  196. // page is an entity view page. This information should be injected here in
  197. // some form by entity-defining modules.
  198. $entity_types = array(
  199. 'comment' => 1,
  200. 'node' => 1,
  201. 'taxonomy_term' => 2,
  202. );
  203. foreach ($entity_types as $entity_type => $position) {
  204. if ($entity = menu_get_object($entity_type, $position)) {
  205. break;
  206. }
  207. }
  208. if ($entity) {
  209. title_field_replacement_hide_label($entity_type, $entity, $variables, TRUE);
  210. }
  211. }
  212. /**
  213. * Process variables for node.tpl.php.
  214. */
  215. function title_process_node(&$variables) {
  216. title_field_replacement_hide_label('node', $variables['node'], $variables);
  217. }
  218. /**
  219. * Process variables for taxonomy-term.tpl.php.
  220. */
  221. function title_process_taxonomy_term(&$variables) {
  222. title_field_replacement_hide_label('taxonomy_term', $variables['term'], $variables);
  223. }
  224. /**
  225. * Process variables for comment.tpl.php.
  226. */
  227. function title_process_comment(&$variables) {
  228. title_field_replacement_hide_label('comment', $variables['comment'], $variables);
  229. }