name.install 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * @file
  4. * Standard installation functions for name.
  5. */
  6. /**
  7. * Implements hook_field_schema().
  8. *
  9. * There is no path backwards in Drupal 7 when it comes to dropping columns or
  10. * reducing the size of the columns (via coresponence mid 2010). So as a varchar
  11. * is just the data + a terminator byte, all fields get all columns with vc(255).
  12. */
  13. function name_field_schema($field) {
  14. module_load_include('module', 'name');
  15. $columns = array();
  16. foreach (_name_translations() as $key => $title) {
  17. $columns[$key] = array(
  18. 'type' => 'varchar',
  19. 'length' => 255,
  20. 'not null' => FALSE,
  21. );
  22. }
  23. return array(
  24. 'columns' => $columns,
  25. 'indexes' => array(
  26. 'given' => array('given'),
  27. 'family' => array('family'),
  28. ),
  29. );
  30. }
  31. /**
  32. * Implements hook_schema().
  33. */
  34. function name_schema() {
  35. $schema = array();
  36. $schema['name_custom_format'] = array(
  37. 'fields' => array(
  38. 'ncfid' => array(
  39. 'description' => 'The primary identifier for a custom format.',
  40. 'type' => 'serial',
  41. 'unsigned' => TRUE,
  42. 'not null' => TRUE,
  43. ),
  44. 'name' => array(
  45. 'description' => 'The name to identify the custom format to a user.',
  46. 'type' => 'varchar',
  47. 'length' => 255,
  48. 'not null' => TRUE,
  49. ),
  50. 'machine_name' => array(
  51. 'description' => 'The machine name to identify the custom format to the system.',
  52. 'type' => 'varchar',
  53. 'length' => 255,
  54. 'not null' => TRUE,
  55. ),
  56. 'format' => array(
  57. 'description' => 'The format string to apply to names.',
  58. 'type' => 'varchar',
  59. 'length' => 255,
  60. 'not null' => TRUE,
  61. ),
  62. ),
  63. 'primary key' => array('ncfid'),
  64. );
  65. return $schema;
  66. }
  67. /**
  68. * Implements hook_install().
  69. *
  70. * Inserts some common name formats into the database.
  71. */
  72. function name_install() {
  73. name_install_default_formats();
  74. }
  75. /**
  76. * Implements hook_uninstall().
  77. */
  78. function name_uninstall() {
  79. variable_del('name_settings');
  80. variable_del('name_user_preferred');
  81. // There is no UI for this setting.
  82. variable_del('name_example_names');
  83. variable_del('name_inline_styles');
  84. variable_del('name_inline_styles_rtl');
  85. variable_del('name_element_wrapper');
  86. variable_del('name_component_required_marker');
  87. }
  88. /**
  89. * This adds the formats that would otherwise be inserted during installation.
  90. */
  91. function name_update_6000() {
  92. $ret = array();
  93. // Create the table.
  94. $schema = array(
  95. 'fields' => array(
  96. 'ncfid' => array(
  97. 'description' => t('The primary identifier for a custom format.'),
  98. 'type' => 'serial',
  99. 'unsigned' => TRUE,
  100. 'not null' => TRUE,
  101. ),
  102. 'name' => array(
  103. 'description' => t('The name to identify the custom format to a user.'),
  104. 'type' => 'varchar',
  105. 'length' => 255,
  106. 'not null' => TRUE,
  107. ),
  108. 'machine_name' => array(
  109. 'description' => t('The machine name to identify the custom format to the system.'),
  110. 'type' => 'varchar',
  111. 'length' => 255,
  112. 'not null' => TRUE,
  113. ),
  114. 'format' => array(
  115. 'description' => t('The format string to apply to names.'),
  116. 'type' => 'varchar',
  117. 'length' => 255,
  118. 'not null' => TRUE,
  119. ),
  120. ),
  121. 'primary key' => array('ncfid'),
  122. );
  123. db_create_table('name_custom_format', $schema);
  124. // Install some default values.
  125. $t = get_t();
  126. name_install_default_formats();
  127. // Clear the caches.
  128. cache_clear_all('*', 'cache', TRUE);
  129. cache_clear_all('*', 'cache_content', TRUE);
  130. if (module_exists('views')) {
  131. // Needed because this can be called from .install files.
  132. module_load_include('module', 'views');
  133. views_invalidate_cache();
  134. }
  135. return t('Created name formats table {name_custom_format}.') . '<br/>'
  136. . $t('The default set of custom name format strings was generated and saved. These are listed !here.',
  137. array('!here' => l($t('here', 'admin/config/content/name'))));
  138. }
  139. /**
  140. * Move the user field selected realname into a variable to avoid doing lookups
  141. * on all user fields / instances during hook_user_load().
  142. */
  143. function name_update_7000() {
  144. foreach (field_info_instances('user', 'user') as $instance) {
  145. $field = field_info_field($instance['field_name']);
  146. if ($field['type'] == 'name' && !empty($instance['settings']['override_username'])) {
  147. variable_set('name_user_preferred', $instance['field_name']);
  148. break;
  149. }
  150. }
  151. }
  152. /**
  153. * Updates the field formatters to the new singular formatter type.
  154. */
  155. function name_update_7002() {
  156. foreach (field_info_fields() as $field_name => $field) {
  157. if ($field['type'] == 'name' && !empty($field['bundles'])) {
  158. foreach ($field['bundles'] as $entity_type => $bundles) {
  159. foreach ($bundles as $bundle) {
  160. $changed = FALSE;
  161. $instance = field_info_instance($entity_type, $field_name, $bundle);
  162. if (!empty($instance['display'])) {
  163. foreach ($instance['display'] as $view_mode => &$display) {
  164. list($name, $output, $format) = explode('_', $display['type'] . '__');
  165. if ($name == 'name') {
  166. switch ($output) {
  167. case 'default':
  168. case 'plain':
  169. case 'raw':
  170. $display['type'] = 'name_formatter';
  171. $display['settings']['output'] = $output;
  172. if (empty($format)) {
  173. $format = 'default';
  174. }
  175. $display['settings']['format'] = $format;
  176. // Ensure that the display settings for the two new fields
  177. // are set while we are updating the other display settings.
  178. $defaults = array(
  179. 'markup' => 0,
  180. 'multiple' => 'default',
  181. 'multiple_delimiter' => ', ',
  182. // And or symbol.
  183. 'multiple_and' => 'text',
  184. // contextual, always, never.
  185. 'multiple_delimiter_precedes_last' => 'never',
  186. 'multiple_el_al_min' => 3,
  187. 'multiple_el_al_first' => 1,
  188. );
  189. foreach ($defaults as $key => $value) {
  190. if (!isset($display['settings'][$key])) {
  191. $display['settings'][$key] = $value;
  192. }
  193. }
  194. $changed = TRUE;
  195. break;
  196. default:
  197. // Assuming a third party formatter here.
  198. }
  199. }
  200. }
  201. }
  202. if ($changed) {
  203. field_update_instance($instance);
  204. }
  205. }
  206. }
  207. }
  208. }
  209. // The new formatter settings will not save unless we clear the caches.
  210. field_cache_clear();
  211. cache_clear_all();
  212. }
  213. /**
  214. * Inserts some common formats.
  215. */
  216. function name_install_default_formats() {
  217. $t = get_t();
  218. $formats = array(
  219. array(
  220. 'format' => '((((t+ig)+im)+if)+is)+jc',
  221. 'name' => $t('Full'),
  222. 'machine_name' => 'full',
  223. ),
  224. array(
  225. 'format' => 'g',
  226. 'name' => $t('Given'),
  227. 'machine_name' => 'given',
  228. ),
  229. array(
  230. 'format' => 'f',
  231. 'name' => $t('Family'),
  232. 'machine_name' => 'family',
  233. ),
  234. array(
  235. 'format' => 't+if',
  236. 'name' => $t('Title Family'),
  237. 'machine_name' => 'formal',
  238. ),
  239. array(
  240. 'format' => 'g+if',
  241. 'name' => $t('Given Family'),
  242. 'machine_name' => 'short_full',
  243. ),
  244. );
  245. foreach ($formats as $format) {
  246. db_insert('name_custom_format')
  247. ->fields(array(
  248. 'name' => $format['name'],
  249. 'machine_name' => $format['machine_name'],
  250. 'format' => $format['format'],
  251. ))
  252. ->execute();
  253. }
  254. }