title.core.inc 5.8 KB

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