summaryrefslogtreecommitdiffstats
path: root/content.module
blob: 3b2e70bb668f0754bd24d5240abf9ef71e54ba7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
<?php
// $Id$
/**
 * @file
 * Allows administrators to associate custom fields to content types.
 */

define('CONTENT_DB_STORAGE_PER_FIELD', 0);
define('CONTENT_DB_STORAGE_PER_CONTENT_TYPE', 1);

define('CONTENT_CALLBACK_NONE', 0x0001);
define('CONTENT_CALLBACK_DEFAULT', 0x0002);
define('CONTENT_CALLBACK_CUSTOM', 0x0004);

define('CONTENT_HANDLE_CORE', 0x0001);
define('CONTENT_HANDLE_MODULE', 0x0002);

function content_help($path, $arg) {
  switch ($path) {
    case 'admin/help#content':
      $output = '<p>'. t('The content module, a required component of the Content Construction Kit (CCK), allows administrators to associate custom fields with content types. In Drupal, content types are used to define the characteristics of a post, including the title and description of the fields displayed on its add and edit pages. Using the content module (and the other helper modules included in CCK), custom fields beyond the default "Title" and "Body" may be added. CCK features are accessible through tabs on the <a href="@content-types">content types administration page</a>. (See the <a href="@node-help">node module help page</a> for more information about content types.)', array('@content-types' => url('admin/content/types'), '@node-help' => url('admin/help/node'))) .'</p>';
      $output .= '<p>'. t('When adding a custom field to a content type, you determine its type (whether it will contain text, numbers, or references to other objects) and how it will be displayed (either as a text field or area, a select box, checkbox, radio button, or autocompleting field). A field may have multiple values (i.e., a "person" may have multiple e-mail addresses) or a single value (i.e., an "employee" has a single employee identification number). As you add and edit fields, CCK automatically adjusts the structure of the database as necessary. CCK also provides a number of other features, including intelligent caching for your custom data, an import and export facility for content type definitions, and integration with other contributed modules.') .'</p>';
      $output .= '<p>'. t('Custom field types are provided by a set of optional modules included with CCK (each module provides a different type). The <a href="@modules">modules page</a> allows you to enable or disable CCK components. A default installation of CCK includes:', array('@modules' => url('admin/build/modules'))) .'</p>';
      $output .= '<ul>';
      $output .= '<li>'. t('<em>number</em>, which adds numeric field types, in integer, decimal or floating point form. You may define a set of allowed inputs, or specify an allowable range of values. A variety of common formats for displaying numeric data are available.') .'</li>';
      $output .= '<li>'. t("<em>text</em>, which adds text field types. A text field may contain plain text only, or optionally, may use Drupal's input format filters to securely manage rich text input. Text input fields may be either a single line (text field), multiple lines (text area), or for greater input control, a select box, checkbox, or radio buttons. If desired, CCK can validate the input to a set of allowed values.") .'</li>';
      $output .= '<li>'. t('<em>nodereference</em>, which creates custom references between Drupal nodes. By adding a <em>nodereference</em> field and two different content types, for instance, you can easily create complex parent/child relationships between data (multiple "employee" nodes may contain a <em>nodereference</em> field linking to an "employer" node).') .'</li>';
      $output .= '<li>'. t('<em>userreference</em>, which creates custom references to your sites\' user accounts. By adding a <em>userreference</em> field, you can create complex relationships between your site\'s users and posts. To track user involvement in a post beyond Drupal\'s standard <em>Authored by</em> field, for instance, add a <em>userreference</em> field named "Edited by" to a content type to store a link to an editor\'s user account page.') .'</li>';
      $output .= '<li>'. t('<em>fieldgroup</em>, which creates collapsible fieldsets to hold a group of related fields. A fieldset may either be open or closed by default. The order of your fieldsets, and the order of fields within a fieldset, is managed via a drag-and-drop interface provided by content module.') .'</li>';
      $output .= '</ul>';
      $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@handbook-cck">CCK</a> or the <a href="@project-cck">CCK project page</a>.', array('@handbook-cck' => 'http://drupal.org/handbook/modules/cck', '@project-cck' => 'http://drupal.org/project/cck')) .'</p>';
      return $output;
  }
}

/**
 * Implementation of hook_flush_caches.
 */
function content_flush_caches() {
  return array(content_cache_tablename());
}

/**
 * Implementation of hook_init().
 */
function content_init() {
  drupal_add_css(drupal_get_path('module', 'content') .'/theme/content-module.css');
  if (module_exists('token') && !function_exists('content_token_values')) {
    module_load_include('inc', 'content', 'includes/content.token');
  }
  if (module_exists('diff') && !function_exists('content_diff')) {
    module_load_include('inc', 'content', 'includes/content.diff');
  }
}

/**
 * Implementation of hook_perm().
 */
function content_perm() {
  return array('Use PHP input for field settings (dangerous - grant with care)');
}

/**
 * Implementation of hook_menu_alter().
 */
function content_menu_alter(&$items) {
  // Customize the content types page with our own callback
  $items['admin/content/types']['page callback'] = 'content_types_overview';
  $items['admin/content/types']['file'] = 'content.admin.inc';
  $items['admin/content/types']['file path'] = drupal_get_path('module', 'content') .'/includes';
}

/**
 * Implementation of hook_menu().
 */
function content_menu() {
  $items = array();
  $items['admin/content/types/fields'] = array(
    'title' => 'Fields',
    'page callback' => 'content_fields_list',
    'access arguments' => array('administer content types'),
    'file' => 'includes/content.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  // Callback for AHAH add more buttons.
  $items['content/js_add_more'] = array(
    'page callback' => 'content_add_more_js',
    'access arguments' => array('access content'),
    'file' => 'includes/content.node_form.inc',
    'type' => MENU_CALLBACK,
  );

  // Make sure this doesn't fire until content_types is working,
  // and tables are updated, needed to avoid errors on initial installation.
  if (!defined('MAINTENANCE_MODE') && variable_get('content_schema_version', -1) >= 6007) {
    foreach (node_get_types() as $type) {
      $type_name = $type->type;
      $content_type = content_types($type_name);
      $type_url_str = $content_type['url_str'];
      $items['admin/content/node-type/'. $type_url_str .'/fields'] = array(
        'title' => 'Manage fields',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('content_field_overview_form', $type_name),
        'access arguments' => array('administer content types'),
        'file' => 'includes/content.admin.inc',
        'type' => MENU_LOCAL_TASK,
        'weight' => 1,
      );
      $items['admin/content/node-type/'. $type_url_str .'/display'] = array(
        'title' => 'Display fields',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('content_display_overview_form', $type_name),
        'access arguments' => array('administer content types'),
        'file' => 'includes/content.admin.inc',
        'type' => MENU_LOCAL_TASK,
        'weight' => 2,
      );
      $contexts = content_build_modes('_tabs');
      foreach ($contexts as $key => $tab) {
        $items['admin/content/node-type/'. $type_url_str .'/display/'. $key] = array(
          'title' => $tab['title'],
          'page arguments' => array('content_display_overview_form', $type_name, $key),
          'access arguments' => array('administer content types'),
          'type' => $key == 'basic' ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
          'weight' => $key == 'basic' ? 0 : 1,
        );
      }
      // Cast as an array in case this is called before any fields have
      // been added, like when a new content type is created.
      foreach ((array) $content_type['fields'] as $field) {
        $field_name = $field['field_name'];
        $items['admin/content/node-type/'. $type_url_str .'/fields/'. $field_name] = array(
          'title' => $field['widget']['label'],
          'page callback' => 'drupal_get_form',
          'page arguments' => array('content_field_edit_form', $type_name, $field_name),
          'access arguments' => array('administer content types'),
          'file' => 'includes/content.admin.inc',
          'type' => MENU_LOCAL_TASK,
        );
        $items['admin/content/node-type/'. $type_url_str .'/fields/'. $field_name .'/remove'] = array(
          'title' => 'Remove field',
          'page callback' => 'drupal_get_form',
          'page arguments' => array('content_field_remove_form', $type_name, $field_name),
          'access arguments' => array('administer content types'),
          'file' => 'includes/content.admin.inc',
          'type' => MENU_CALLBACK,
        );
      }
    }
  }
  return $items;
}

/**
 * Hook elements().
 *
 * Used to add multiple value processing, validation, and themes.
 *
 * FAPI callbacks can be declared here, and the element will be
 * passed to those callbacks.
 *
 * Drupal will automatically theme the element using a theme with
 * the same name as the hook_elements key.
 */
function content_elements() {
  return array(
    'content_multiple_values' => array(),
    'content_field' => array(),
  );
}

/**
 * Implementation of hook_theme().
 */
function content_theme() {
  $path = drupal_get_path('module', 'content') .'/theme';
  require_once "./$path/theme.inc";

  return array(
    'content_field' => array(
      'template' => 'content-field',
      'arguments' => array('element' => NULL),
      'path' => $path,
    ),
    'content_overview_links' => array(
      'arguments' => array(),
    ),
    'content_field_overview_form' => array(
      'template' => 'content-admin-field-overview-form',
      'file' => 'theme.inc',
      'path' => $path,
      'arguments' => array('form' => NULL),
    ),
    'content_display_overview_form' => array(
      'template' => 'content-admin-display-overview-form',
      'file' => 'theme.inc',
      'path' => $path,
      'arguments' => array('form' => NULL),
    ),
    'content_exclude' => array(
      'arguments' => array('content' => NULL, 'object' => array(), 'context' => NULL),
    ),
    'content_view_multiple_field' => array(
      'arguments' => array('items' => NULL, 'field' => NULL, 'data' => NULL),
    ),
    'content_multiple_values' => array(
      'arguments' => array('element' => NULL),
    ),
  );
}

/**
 * Implementation of hook_views_api().
 */
function content_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'content') . '/includes/views',
  );
}

/**
 * Implementation of hook_ctools_plugin_directory().
 */
function content_ctools_plugin_directory($module, $plugin) {
  if ($module == 'ctools' && $plugin == 'content_types') {
    return 'includes/panels/' . $plugin;
  }
}

/**
 * Load data for a node type's fields.
 * Implementation of hook_nodeapi 'load' op.
 *
 * When loading one of the content.module nodes, we need to let each field handle
 * its own loading. This can make for a number of queries in some cases, so we
 * cache the loaded object structure and invalidate it during the update process.
 */
function content_load(&$node) {
  $cid = 'content:'. $node->nid .':'. $node->vid;
  if ($cached = cache_get($cid, content_cache_tablename())) {
    foreach ($cached->data as $key => $value) {
      $node->$key = $value;
    }
  }
  else {
    $default_additions = _content_field_invoke_default('load', $node);
    if ($default_additions) {
      foreach ($default_additions as $key => $value) {
        $node->$key = $value;
      }
    }
    $additions = _content_field_invoke('load', $node);
    if ($additions) {
      foreach ($additions as $key => $value) {
        $node->$key = $value;
        $default_additions[$key] = $value;
      }
    }
    cache_set($cid, $default_additions, content_cache_tablename());
  }
}

/**
 * Implementation of hook_nodeapi 'validate' op.
 *
 */
function content_validate(&$node, $form = NULL) {
  _content_field_invoke_default('validate', $node, $form);
  _content_field_invoke('validate', $node, $form);
}

/**
 * Implementation of hook_nodeapi 'presave' op.
 *
 */
function content_presave(&$node) {
  _content_field_invoke('presave', $node);
  _content_field_invoke_default('presave', $node);
}

/**
 * Implementation of hook_nodeapi 'insert' op.
 *
 * Insert node type fields.
 */
function content_insert(&$node) {
  _content_field_invoke('insert', $node);
  _content_field_invoke_default('insert', $node);
}

/**
 * Implementation of hook_nodeapi 'update' op.
 *
 * Update node type fields.
 */
function content_update(&$node) {
  _content_field_invoke('update', $node);
  _content_field_invoke_default('update', $node);
  cache_clear_all('content:'. $node->nid .':'. $node->vid, content_cache_tablename());
}

/**
 * Implementation of hook_nodeapi 'delete' op.
 *
 * Delete node type fields.
 */
function content_delete(&$node) {
  _content_field_invoke('delete', $node);
  _content_field_invoke_default('delete', $node);
  cache_clear_all('content:'. $node->nid .':', content_cache_tablename(), TRUE);
}

/**
 * Implementation of hook_nodeapi 'delete_revision' op.
 *
 * Delete node type fields for a revision.
 */
function content_delete_revision(&$node) {
  _content_field_invoke('delete revision', $node);
  _content_field_invoke_default('delete revision', $node);
  cache_clear_all('content:'. $node->nid .':'. $node->vid, content_cache_tablename());
}

/**
 * Implementation of hook_nodeapi 'view' op.
 *
 * Generate field render arrays.
 */
function content_view(&$node, $teaser = FALSE, $page = FALSE) {
  // Let field modules sanitize their data for output.
  _content_field_invoke('sanitize', $node, $teaser, $page);

  // Merge fields.
  $additions = _content_field_invoke_default('view', $node, $teaser, $page);
  $node->content = array_merge((array) $node->content, $additions);
}

/**
 * Render a single field, fully themed with label and multiple values.
 *
 * To be used by third-party code (Views, Panels...) that needs to output
 * an isolated field. Do *not* use inside node templates, use the
 * $FIELD_NAME_rendered variables instead.
 *
 * By default, the field is displayed using the settings defined for the
 * 'full node' or 'teaser' contexts (depending on the value of the $teaser param).
 * Set $node->build_mode to a different value to use a different context.
 *
 * Different settings can be specified by adjusting $field['display_settings'].
 *
 * @param $field
 *   The field definition.
 * @param $node
 *   The node containing the field to display. Can be a 'pseudo-node', containing
 *   at least 'type', 'nid', 'vid', and the field data.
 * @param $teaser
 * @param $page
 *   Similar to hook_nodeapi('view')
 * @return
 *   The themed output for the field.
 */
function content_view_field($field, $node, $teaser = FALSE, $page = FALSE) {
  $output = '';
  if (isset($node->$field['field_name'])) {
    $items = $node->$field['field_name'];

    // Use 'full'/'teaser' if not specified otherwise.
    $node->build_mode = isset($node->build_mode) ? $node->build_mode : NODE_BUILD_NORMAL;

    // One-field equivalent to _content_field_invoke('sanitize').
    $field_types = _content_field_types();
    $module = $field_types[$field['type']]['module'];
    $function = $module .'_field';
    if (function_exists($function)) {
      $function('sanitize', $node, $field, $items, $teaser, $page);
      $node->$field['field_name'] = $items;
    }

    $view = content_field('view', $node, $field, $items, $teaser, $page);
    // content_field('view') adds a wrapper to handle variables and 'excluded'
    // fields for node templates. We bypass it and render the actual field.
    $output = drupal_render($view[$field['field_name']]['field']);
  }
  return $output;
}

/**
 * Implementation of hook_nodeapi 'alter' op.
 *
 * Add back the formatted values in the 'view' element for all fields,
 * so that node templates can use it.
 */
function content_alter(&$node, $teaser = FALSE, $page = FALSE) {
  _content_field_invoke_default('alter', $node, $teaser, $page);
}

/**
 * Implementation of hook_nodeapi 'prepare translation' op.
 *
 * Generate field render arrays.
 */
function content_prepare_translation(&$node) {
  $default_additions = _content_field_invoke_default('prepare translation', $node);
  $additions = _content_field_invoke('prepare translation', $node);
  // Merge module additions after the default ones to enable overriding
  // of field values.
  $node = (object) array_merge((array) $node, $default_additions, $additions);
}

/**
 * Implementation of hook_nodeapi().
 */
function content_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  // Prevent against invalid 'nodes' built by broken 3rd party code.
  if (isset($node->type)) {
    $type = content_types($node->type);
    // Save cycles if the type has no CCK fields.
    if (!empty($type['fields'])) {
      $callback = 'content_'. str_replace(' ', '_', $op);
      if (function_exists($callback)) {
        $callback($node, $a3, $a4);
      }
    }

    // Special case for 'view' op, we want to adjust weights of non-cck fields
    // even if there are no actual fields for this type.
    if ($op == 'view') {
      $node->content['#pre_render'][] = 'content_alter_extra_weights';
      $node->content['#content_extra_fields'] = $type['extra'];
    }
  }
}

/**
 *  Implementation of hook_form_alter().
 */
function content_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
    $type = content_types($form['#node']->type);
    if (!empty($type['fields'])) {
      module_load_include('inc', 'content', 'includes/content.node_form');
      // Merge field widgets.
      $form = array_merge($form, content_form($form, $form_state));
    }
    $form['#pre_render'][] = 'content_alter_extra_weights';
    $form['#content_extra_fields'] = $type['extra'];
  }
}

/**
 * Pre-render callback to adjust weights of non-CCK fields.
 */
function content_alter_extra_weights($elements) {
  if (isset($elements['#content_extra_fields'])) {
    foreach ($elements['#content_extra_fields'] as $key => $value) {
      // Some core 'fields' use a different key in node forms and in 'view'
      // render arrays. Check we're not on a form first.
      if (!isset($elements['#build_id']) && isset($value['view']) && isset($elements[$value['view']])) {
        $elements[$value['view']]['#weight'] = $value['weight'];
      }
      elseif (isset($elements[$key])) {
        $elements[$key]['#weight'] = $value['weight'];
      }
    }
  }
  return $elements;
}

/**
 * Proxy function to call content_add_more_submit(), because it might not be
 * included yet when the form is processed and invokes the callback.
 */
function content_add_more_submit_proxy($form, &$form_state) {
  module_load_include('inc', 'content', 'includes/content.node_form');
  content_add_more_submit($form, $form_state);
}

/**
 * Proxy function to call content_multiple_value_after_build(), because it might
 * not be included yet when the form is processed and invokes the callback.
 */
function content_multiple_value_after_build_proxy($elements, &$form_state) {
  module_load_include('inc', 'content', 'includes/content.node_form');
  return content_multiple_value_after_build($elements, $form_state);
}

/**
 * Theme an individual form element.
 *
 * Combine multiple values into a table with drag-n-drop reordering.
 */
function theme_content_multiple_values($element) {
  $field_name = $element['#field_name'];
  $field = content_fields($field_name);
  $output = '';

  if ($field['multiple'] >= 1) {
    $table_id = $element['#field_name'] .'_values';
    $order_class = $element['#field_name'] .'-delta-order';
    $required = !empty($element['#required']) ? '<span class="form-required" title="'. t('This field is required.') .'">*</span>' : '';

    $header = array(
      array(
        'data' => t('!title: !required', array('!title' => $element['#title'], '!required' => $required)),
        'colspan' => 2
      ),
      array('data' => t('Order'), 'class' => 'content-multiple-weight-header'),
    );
    if ($field['multiple'] == 1) {
      $header[] = array('data' => '<span>'. t('Remove') .'</span>', 'class' => 'content-multiple-remove-header');
    }
    $rows = array();

    // Sort items according to '_weight' (needed when the form comes back after
    // preview or failed validation)
    $items = array();
    foreach (element_children($element) as $key) {
      if ($key !== $element['#field_name'] .'_add_more') {
        $items[$element[$key]['#delta']] = &$element[$key];
      }
    }
    uasort($items, '_content_sort_items_value_helper');

    // Add the items as table rows.
    foreach ($items as $delta => $item) {
      $item['_weight']['#attributes']['class'] = $order_class;
      $delta_element = drupal_render($item['_weight']);
      if ($field['multiple'] == 1) {
        $remove_element = drupal_render($item['_remove']);
      }
      $cells = array(
        array('data' => '', 'class' => 'content-multiple-drag'),
        drupal_render($item),
        array('data' => $delta_element, 'class' => 'delta-order'),
      );
      $row_class = 'draggable';
      if ($field['multiple'] == 1) {
        if (!empty($item['_remove']['#default_value'])) {
          $row_class .= ' content-multiple-removed-row';
        }
        $cells[] = array('data' => $remove_element, 'class' => 'content-multiple-remove-cell');
      }
      $rows[] = array('data' => $cells, 'class' => $row_class);
    }

    $output .= theme('table', $header, $rows, array('id' => $table_id, 'class' => 'content-multiple-table'));
    $output .= $element['#description'] ? '<div class="description">'. $element['#description'] .'</div>' : '';
    $output .= drupal_render($element[$element['#field_name'] .'_add_more']);

    drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);
    drupal_add_js(drupal_get_path('module', 'content') .'/js/content.node_form.js');
  }
  else {
    foreach (element_children($element) as $key) {
      $output .= drupal_render($element[$key]);
    }
  }

  return $output;
}

/**
 * Modules notify Content module when uninstalled, disabled, etc.
 *
 * @param string $op
 *   the module operation: uninstall, install, enable, disable
 * @param string $module
 *   the name of the affected module.
 * @TODO
 *   figure out exactly what needs to be done by content module when
 *   field modules are installed, uninstalled, enabled or disabled.
 */
function content_notify($op, $module) {
  switch ($op) {
    case 'install':
      content_clear_type_cache();
      break;
    case 'uninstall':
      module_load_include('inc', 'content', 'includes/content.crud');
      content_module_delete($module);
      break;
    case 'enable':
      content_associate_fields($module);
      content_clear_type_cache();
      break;
    case 'disable':
      // When CCK modules are disabled before content module's update is run
      // to add the active column, we can't do this.
      if (variable_get('content_schema_version', -1) < 6007) {
        return FALSE;
      }
      db_query("UPDATE {". content_field_tablename() ."} SET active=0 WHERE module='%s'", $module);
      db_query("UPDATE {". content_instance_tablename() ."} SET widget_active=0 WHERE widget_module='%s'", $module);
      content_clear_type_cache(TRUE);
      break;
  }
}

/**
 * Allows a module to update the database for fields and columns it controls.
 *
 * @param string $module
 *   The name of the module to update on.
 */
function content_associate_fields($module) {
  // When CCK modules are enabled before content module's update is run,
  // to add module and active columns, we can't do this.
  if (variable_get('content_schema_version', -1) < 6007) {
    return FALSE;
  }
  $module_fields = module_invoke($module, 'field_info');
  if ($module_fields) {
    foreach ($module_fields as $name => $field_info) {
      watchdog('content', 'Updating field type %type with module %module.', array('%type' => $name, '%module' => $module));
      db_query("UPDATE {". content_field_tablename() ."} SET module = '%s', active = %d WHERE type = '%s'", $module, 1, $name);
    }
  }
  $module_widgets = module_invoke($module, 'widget_info');
  if ($module_widgets) {
    foreach ($module_widgets as $name => $widget_info) {
      watchdog('content', 'Updating widget type %type with module %module.', array('%type' => $name, '%module' => $module));
      db_query("UPDATE {". content_instance_tablename() ."} SET widget_module = '%s', widget_active = %d WHERE widget_type = '%s'", $module, 1, $name);
    }
  }
  // This is called from updates and installs, so get the install-safe
  // version of a fields array.
  $fields_set = array();
  module_load_include('install', 'content');
  $types = content_types_install();
  foreach ($types as $type_name => $fields) {
    foreach ($fields as $field) {
      if ($field['module'] == $module && !in_array($field['field_name'], $fields_set)) {
        $columns = (array) module_invoke($field['module'], 'field_settings', 'database columns', $field);
        db_query("UPDATE {". content_field_tablename() ."} SET db_columns = '%s' WHERE field_name = '%s'", serialize($columns), $field['field_name']);
        $fields_set[] = $field['field_name'];
      }
    }
  }
}

/**
 * Implementation of hook_field(). Handles common field housekeeping.
 *
 * This implementation is special, as content.module does not define any field
 * types. Instead, this function gets called after the type-specific hook, and
 * takes care of default stuff common to all field types.
 *
 * Db-storage ops ('load', 'insert', 'update', 'delete', 'delete revisions')
 * are not executed field by field, and are thus handled separately in
 * content_storage.
 *
 * The 'view' operation constructs the $node in a way that you can use
 * drupal_render() to display the formatted output for an individual field.
 * i.e. print drupal_render($node->countent['field_foo']);
 *
 * The code now supports both single value formatters, which theme an
 * individual item value as has been done in previous version of CCK,
 * and multiple value formatters, which theme all values for the field
 * in a single theme. The multiple value formatters could be used, for
 * instance, to plot field values on a single map or display them
 * in a graph. Single value formatters are the default, multiple value
 * formatters can be designated as such in formatter_info().
 *
 * The node array will look like:
 *   $node->content['field_foo']['wrapper'] = array(
 *     '#type' => 'content_field',
 *     '#title' => 'label'
 *     '#field_name' => 'field_name',
 *     '#node' => $node,
 *     // Value of the $teaser param of hook_nodeapi('view').
 *     '#teaser' => $teaser,
 *     // Value of the $page param of hook_nodeapi('view').
 *     '#page' => $page,
 *     // The curent rendering context ('teaser', 'full', NODE_BUILD_SEARCH_INDEX...).
 *     '#context' => $context,
 *     'items' =>
 *       0 => array(
 *         '#item' => $items[0],
 *         // Only for 'single-value' formatters
 *         '#theme' => $theme,
 *         '#field_name' => 'field_name',
 *         '#type_name' => $node->type,
 *         '#formatter' => $formatter_name,
 *         '#node' => $node,
 *         '#delta' => 0,
 *       ),
 *       1 => array(
 *         '#item' => $items[1],
 *         // Only for 'single-value' formatters
 *         '#theme' => $theme,
 *         '#field_name' => 'field_name',
 *         '#type_name' => $node->type,
 *         '#formatter' => $formatter_name,
 *         '#node' => $node,
 *         '#delta' => 1,
 *       ),
 *       // Only for 'multiple-value' formatters
 *       '#theme' => $theme,
 *       '#field_name' => 'field_name',
 *       '#type_name' => $node->type,
 *       '#formatter' => $formatter_name,
 *     ),
 *   );
 */
function content_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      // If the field is configured for multiple values and these are handled
      // by content module, we need to filter out items flagged for removal and
      // count non-empty items to enforce field requirement settings.
      if ($field['multiple'] >= 1 && content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) {
        module_load_include('inc', 'content', 'includes/content.node_form');
        // Note that the $teaser argument for nodeapi('validate') is the $form.
        content_multiple_value_nodeapi_validate($node, $field, $items, $teaser);
      }
      break;

    case 'presave':
      if (!empty($node->devel_generate)) {
        include_once('./'. drupal_get_path('module', 'content') .'/includes/content.devel.inc');
        content_generate_fields($node, $field);
        $items = $node->{$field['field_name']};
      }

      // Manual node_save calls might not have all fields filled in.
      // On node insert, we need to make sure all tables get at least an empty
      // record, or subsequent edits, using drupal_write_record() in update mode,
      // won't insert any data.
      // Missing fields on node update are handled in content_storage().
      if (empty($items) && !isset($node->nid)) {
        foreach (array_keys($field['columns']) as $column) {
          $items[0][$column] = NULL;
        }
        $node->$field['field_name'] = $items;
      }

      // If there was an AHAH add more button in this field, don't save it.
      // TODO: is it still needed ?
      unset($items[$field['field_name'] .'_add_more']);

      if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) {
        // Reorder items to account for drag-n-drop reordering.
        $items = _content_sort_items($field, $items);
      }

      // Filter out items flagged for removal.
      $items = content_set_empty($field, $items);

      break;

    case 'view':
      $addition = array();

      // Previewed nodes bypass the 'presave' op, so we need to do some massaging.
      if ($node->build_mode == NODE_BUILD_PREVIEW) {
        if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) {
          // Reorder items to account for drag-n-drop reordering.
          $items = _content_sort_items($field, $items);
        }

        // Filter out items flagged for removal.
        $items = content_set_empty($field, $items);
      }

      // NODE_BUILD_NORMAL is 0, and ('whatever' == 0) is TRUE, so we need a ===.
      if ($node->build_mode === NODE_BUILD_NORMAL || $node->build_mode == NODE_BUILD_PREVIEW) {
        $context = $teaser ? 'teaser' : 'full';
      }
      else {
        $context = $node->build_mode;
      }
      // The field may be missing info for $contexts added by modules
      // enabled after the field was last edited.
      $formatter_name = isset($field['display_settings'][$context]) && isset($field['display_settings'][$context]['format']) ? $field['display_settings'][$context]['format'] : 'default';
      if ($formatter = _content_get_formatter($formatter_name, $field['type'])) {
        $theme = $formatter['module'] .'_formatter_'. $formatter_name;
        $single = (content_handle('formatter', 'multiple values', $formatter) == CONTENT_HANDLE_CORE);

        $label_display = isset($field['display_settings']['label']['format']) ? $field['display_settings']['label']['format'] : 'above';
        // Do not include field labels when indexing content.
        if ($context == NODE_BUILD_SEARCH_INDEX) {
          $label_display = 'hidden';
        }

        $element = array(
          '#type' => 'content_field',
          '#title' => check_plain(t($field['widget']['label'])),
          '#field_name' => $field['field_name'],
          '#access' => $formatter_name != 'hidden' && content_access('view', $field, NULL, $node),
          '#label_display' => $label_display,
          '#node' => $node,
          '#teaser' => $teaser,
          '#page' => $page,
          '#context' => $context,
          '#single' => $single,
          'items' => array(),
        );

        // Fill-in items.
        foreach (array_keys($items) as $weight => $delta) {
          $element['items'][$delta] = array(
            '#item' => $items[$delta],
            '#weight' => $weight,
          );
        }

        // Append formatter information either on each item ('single-value' formatter)
        // or at the upper 'items' level ('multiple-value' formatter)
        $format_info = array(
          '#theme' => $theme,
          '#field_name' => $field['field_name'],
          '#type_name' => $node->type,
          '#formatter' => $formatter_name,
          '#node' => $node,
        );
        if ($single) {
          foreach ($items as $delta => $item) {
            $element['items'][$delta] += $format_info;
            $element['items'][$delta]['#item']['#delta'] = $delta;
          }
        }
        else {
          $element['items'] += $format_info;
        }

        // The wrapper lets us get the themed output for the whole field
        // to populate the $FIELD_NAME_rendered variable for node templates,
        // and hide it from the $content variable if needed.
        // See 'preprocess_node' op and theme_content_field_wrapper()?
        $wrapper = array(
          'field' => $element,
          '#weight' => $field['widget']['weight'],
          '#post_render' => array('content_field_wrapper_post_render'),
          '#field_name' => $field['field_name'],
          '#type_name' => $node->type,
          '#context' => $context,
        );

        $addition = array($field['field_name'] => $wrapper);
      }
      return $addition;

    case 'alter':
      // Add back the formatted values in the 'view' element,
      // so that tokens and node templates can use it.
      // Note: Doing this in 'preprocess_node' breaks token integration.

      // The location of the field's rendered output depends on whether the
      // field is in a fieldgroup or not.
      $wrapper = NULL;
      if (isset($node->content[$field['field_name']])) {
        $wrapper = $node->content[$field['field_name']];
      }
      elseif (module_exists('fieldgroup') && ($group_name = fieldgroup_get_group($node->type, $field['field_name'])) && isset($node->content[$group_name]['group'][$field['field_name']])) {
        $wrapper = $node->content[$group_name]['group'][$field['field_name']];
      }

      if ($wrapper) {
        $element = $wrapper['field'];
        // '#single' is not set if the field is hidden or inaccessible.
        if (isset($element['#single'])) {
          if (!empty($element['#single'])) {
            // Single value formatter.
            foreach (element_children($element['items']) as $delta) {
              // '#chilren' is not set if the field is empty.
              $items[$delta]['view'] = isset($element['items'][$delta]['#children']) ? $element['items'][$delta]['#children'] : '';
            }
          }
          elseif (isset($element['items']['#children']))  {
            // Multiple values formatter.
            $items[0]['view'] = $element['items']['#children'];
          }
        }
        else {
          // Hidden or inaccessible field.
          $items[0]['view'] = '';
        }
      }
      break;

    case 'preprocess_node':
      // Add $FIELD_NAME_rendered variables.
      $addition = array();

      // The location of the field's rendered output depends on whether the
      // field is in a fieldgroup or not.
      $wrapper = NULL;
      if (isset($node->content[$field['field_name']])) {
        $wrapper = $node->content[$field['field_name']];
      }
      elseif (module_exists('fieldgroup') && ($group_name = fieldgroup_get_group($node->type, $field['field_name'])) && isset($node->content[$group_name]['group'][$field['field_name']])) {
        $wrapper = $node->content[$group_name]['group'][$field['field_name']];
      }

      if ($wrapper) {
        // '#chilren' is not set if the field is empty.
        $addition[$field['field_name'] .'_rendered'] = isset($wrapper['#children']) ? $wrapper['#children'] : '';
      }

      return $addition;

    case 'prepare translation':
      $addition = array();
      if (isset($node->translation_source->$field['field_name'])) {
        $addition[$field['field_name']] = $node->translation_source->$field['field_name'];
      }
      return $addition;
  }
}

/**
 * Helper function to filter out items flagged for removal.
 *
 * On order to keep marker rows in the database, the function ensures
 * that the right number of 'all columns NULL' values is kept.
 *
 * @param array $field
 * @param array $items
 * @return array
 *   returns filtered and adjusted item array
 */
function content_set_empty($field, $items) {
  // Prepare an empty item.
  $empty = array();
  foreach (array_keys($field['columns']) as $column) {
    $empty[$column] = NULL;
  }

  // Filter out items flagged for removal.
  $filtered = array();
  $function = $field['module'] .'_content_is_empty';
  foreach ((array) $items as $delta => $item) {
    if (empty($item['_remove'])) {
      $filtered[] = ($function($item, $field) ? $empty : $item);
    }
  }

  // Make sure we store the right number of 'empty' values.
  $pad = $field['multiple'] > 1 ? $field['multiple'] : 1;
  $filtered = array_pad($filtered, $pad, $empty);

  return $filtered;
}

/**
 * Helper function to sort items in a field according to
 * user drag-n-drop reordering.
 */
function _content_sort_items($field, $items) {
  if ($field['multiple'] >= 1 && isset($items[0]['_weight'])) {
    usort($items, '_content_sort_items_helper');
    foreach ($items as $delta => $item) {
      if (is_array($item) && isset($item['_weight'])) {
        unset($items[$delta]['_weight']);
      }
    }
  }
  return $items;
}

/**
 * Sort function for items order.
 * (copied form element_sort(), which acts on #weight keys)
 */
function _content_sort_items_helper($a, $b) {
  $a_weight = (is_array($a) && isset($a['_weight'])) ? $a['_weight'] : 0;
  $b_weight = (is_array($b) && isset($b['_weight'])) ? $b['_weight'] : 0;
  if ($a_weight == $b_weight) {
    return 0;
  }
  return ($a_weight < $b_weight) ? -1 : 1;
}

/**
 * Same as above, using ['_weight']['#value']
 */
function _content_sort_items_value_helper($a, $b) {
  $a_weight = (is_array($a) && isset($a['_weight']['#value'])) ? $a['_weight']['#value'] : 0;
  $b_weight = (is_array($b) && isset($b['_weight']['#value'])) ? $b['_weight']['#value'] : 0;
  if ($a_weight == $b_weight) {
    return 0;
  }
  return ($a_weight < $b_weight) ? -1 : 1;
}

/**
 * Handle storage ops for _content_field_invoke_default().
 */
function content_storage($op, $node) {
  // Don't try this before content module's update is run to add
  // the active and module columns.
  if (variable_get('content_schema_version', -1) < 6007) {
    return FALSE;
  }

  $type_name = $node->type;
  $type = content_types($type_name);

  switch ($op) {
    case 'load':
      // OPTIMIZE: load all non multiple fields in a single JOIN query ?
      // warning: 61-join limit in MySQL ?
      $additions = array();
      // For each table used by this content type,
      foreach ($type['tables'] as $table) {
        $schema = drupal_get_schema($table);
        // The per-type table might not have any fields actually stored in it.
        if (!$schema['content fields']) {
          continue;
        }
        $query = 'SELECT * FROM {'. $table .'} WHERE vid = %d';

        // If we're loading a table for a multiple field,
        // we fetch all rows (values) ordered by delta,
        // else we only fetch one row.
        $result = isset($schema['fields']['delta']) ? db_query($query .' ORDER BY delta', $node->vid) : db_query_range($query, $node->vid, 0, 1);

        // For each table row, populate the fields.
        while ($row = db_fetch_array($result)) {
          // For each field stored in the table, add the field item.
          foreach ($schema['content fields'] as $field_name) {
            $item = array();
            $field = content_fields($field_name, $type_name);
            $db_info = content_database_info($field);
            // For each column declared by the field, populate the item.
            foreach ($db_info['columns'] as $column => $attributes) {
              $item[$column] = $row[$attributes['column']];
            }

            // Add the item to the field values for the node.
            if (!isset($additions[$field_name])) {
              $additions[$field_name] = array();
            }

            // Preserve deltas when loading items from database.
            if (isset($row['delta'])) {
              // Make sure multiple value fields have consecutive deltas.
              if ($row['delta'] > 0 && !isset($additions[$field_name][$row['delta']-1])) {
                $empty = array();
                foreach (array_keys($db_info['columns']) as $column) {
                  $empty[$column] = NULL;
                }
                $next_delta = !empty($additions[$field_name]) ? (max(array_keys($additions[$field_name])) + 1) : 0;
                for ($delta = $next_delta; $delta < $row['delta']; $delta++) {
                  if (!isset($additions[$field_name][$delta])) {
                    $additions[$field_name][$delta] = $empty;
                  }
                }
              }
              $additions[$field_name][$row['delta']] = $item;
            }
            else {
              $additions[$field_name][] = $item;
            }
          }
        }
      }
      return $additions;

    case 'insert':
    case 'update':
      foreach ($type['tables'] as $table) {
        $schema = drupal_get_schema($table);
        $record = array();
        foreach ($schema['content fields'] as $field_name) {
          if (isset($node->$field_name)) {
            $field = content_fields($field_name, $type_name);
            // Multiple fields need specific handling, we'll deal with them later on.
            if ($field['multiple']) {
              continue;
            }
            $db_info = content_database_info($field);
            foreach ($db_info['columns'] as $column => $attributes) {
              $record[$attributes['column']] = $node->{$field_name}[0][$column];
            }
          }
        }
        // $record might be empty because
        // - the table stores a multiple field :
        //   we do nothing, this is handled later on
        // - this is the per-type table and no field is actually stored in it :
        //   we still store the nid and vid
        if (count($record) || empty($schema['content fields'])) {
          $record['nid'] = $node->nid;
          $record['vid'] = $node->vid;
          // Can't rely on the insert/update op of the node to decide if this
          // is an insert or an update, a node or revision may have existed
          // before any fields were created, so there may not be an entry here.

          // TODO - should we auto create an entry for all existing nodes when
          // fields are added to content types -- either a NULL value
          // or the default value? May need to offer the user an option of
          // how to handle that.
          if (db_result(db_query("SELECT COUNT(*) FROM {". $table ."} WHERE vid = %d", $node->vid))) {
            content_write_record($table, $record, array('vid'));
          }
          else {
            content_write_record($table, $record);
          }
        }
      }

      // Handle multiple fields.
      foreach ($type['fields'] as $field) {
        if ($field['multiple'] && isset($node->$field['field_name'])) {
          $db_info = content_database_info($field);
          // Delete and insert, rather than update, in case a value was added.
          if ($op == 'update') {
            db_query('DELETE FROM {'. $db_info['table'] .'} WHERE vid = %d', $node->vid);
          }
          // Collect records for non-empty items.
          $function = $field['module'] .'_content_is_empty';
          $records = array();
          foreach ($node->$field['field_name'] as $delta => $item) {
            if (!$function($item, $field)) {
              $record = array();
              foreach ($db_info['columns'] as $column => $attributes) {
                $record[$attributes['column']] = $item[$column];
              }
              $record['nid'] = $node->nid;
              $record['vid'] = $node->vid;
              $record['delta'] = $delta;
              $records[] = $record;
            }
          }
          // If there was no non-empty item, insert delta 0 with NULL values.
          if (empty($records)) {
            $record = array();
            foreach ($db_info['columns'] as $column => $attributes) {
              $record[$attributes['column']] = NULL;
            }
            $record['nid'] = $node->nid;
            $record['vid'] = $node->vid;
            $record['delta'] = 0;
            $records[] = $record;
          }
          // Insert the collected records for this field into database.
          foreach ($records as $record) {
            content_write_record($db_info['table'], $record);
          }
        }
      }
      break;

    case 'delete':
      foreach ($type['tables'] as $table) {
        db_query('DELETE FROM {'. $table .'} WHERE nid = %d', $node->nid);
      }
      break;

    case 'delete revision':
      foreach ($type['tables'] as $table) {
        db_query('DELETE FROM {'. $table .'} WHERE vid = %d', $node->vid);
      }
      break;
  }
}

/**
 * Save a record to the database based upon the schema.
 *
 * Directly copied from core's drupal_write_record, which can't update a
 * column to NULL. See http://drupal.org/node/227677 and
 * http://drupal.org/node/226264 for more details about that problem.
 *
 * TODO - get rid of this function and change references back to
 * drupal_write_record() if the patch gets into core. Will need a method
 * of protecting people on older versions, though.
 *
 * Default values are filled in for missing items, and 'serial' (auto increment)
 * types are filled in with IDs.
 *
 * @param $table
 *   The name of the table; this must exist in schema API.
 * @param $object
 *   The object to write. This is a reference, as defaults according to
 *   the schema may be filled in on the object, as well as ID on the serial
 *   type(s). Both array an object types may be passed.
 * @param $update
 *   If this is an update, specify the primary keys' field names. It is the
 *   caller's responsibility to know if a record for this object already
 *   exists in the database. If there is only 1 key, you may pass a simple string.
 * @return
 *   Failure to write a record will return FALSE. Otherwise SAVED_NEW or
 *   SAVED_UPDATED is returned depending on the operation performed. The
 *   $object parameter contains values for any serial fields defined by
 *   the $table. For example, $object->nid will be populated after inserting
 *   a new node.
 */
function content_write_record($table, &$object, $update = array()) {
  // Standardize $update to an array.
  if (is_string($update)) {
    $update = array($update);
  }

  // Convert to an object if needed.
  if (is_array($object)) {
    $object = (object) $object;
    $array = TRUE;
  }
  else {
    $array = FALSE;
  }

  $schema = drupal_get_schema($table);
  if (empty($schema)) {
    return FALSE;
  }

  $fields = $defs = $values = $serials = $placeholders = array();

  // Go through our schema, build SQL, and when inserting, fill in defaults for
  // fields that are not set.
  foreach ($schema['fields'] as $field => $info) {
    // Special case -- skip serial types if we are updating.
    if ($info['type'] == 'serial' && count($update)) {
      continue;
    }

    // For inserts, populate defaults from Schema if not already provided
    if (!isset($object->$field) && !count($update) && isset($info['default'])) {
      $object->$field = $info['default'];
    }

    // Track serial fields so we can helpfully populate them after the query.
    if ($info['type'] == 'serial') {
      $serials[] = $field;
      // Ignore values for serials when inserting data. Unsupported.
      unset($object->$field);
    }

    // Build arrays for the fields, placeholders, and values in our query.
    if (isset($object->$field) || array_key_exists($field, $object)) {
      $fields[] = $field;
      if (isset($object->$field)) {
        $placeholders[] = db_type_placeholder($info['type']);

        if (empty($info['serialize'])) {
          $values[] = $object->$field;
        }
        else {
          $values[] = serialize($object->$field);
        }
      }
      else {
        $placeholders[] = 'NULL';
      }
    }
  }

  // Build the SQL.
  $query = '';
  if (!count($update)) {
    $query = "INSERT INTO {". $table ."} (". implode(', ', $fields) .') VALUES ('. implode(', ', $placeholders) .')';
    $return = SAVED_NEW;
  }
  else {
    $query = '';
    foreach ($fields as $id => $field) {
      if ($query) {
        $query .= ', ';
      }
      $query .= $field .' = '. $placeholders[$id];
    }

    foreach ($update as $key) {
      $conditions[] = "$key = ". db_type_placeholder($schema['fields'][$key]['type']);
      $values[] = $object->$key;
    }

    $query = "UPDATE {". $table ."} SET $query WHERE ". implode(' AND ', $conditions);
    $return = SAVED_UPDATED;
  }

  // Execute the SQL.
  if (db_query($query, $values)) {
    if ($serials) {
      // Get last insert ids and fill them in.
      foreach ($serials as $field) {
        $object->$field = db_last_insert_id($table, $field);
      }
    }

    // If we began with an array, convert back so we don't surprise the caller.
    if ($array) {
      $object = (array) $object;
    }

    return $return;
  }

  return FALSE;
}

/**
 * Invoke a field hook.
 *
 * For each operation, both this function and _content_field_invoke_default() are
 * called so that the default database handling can occur.
 */
function _content_field_invoke($op, &$node, $teaser = NULL, $page = NULL) {
  $type_name = is_string($node) ? $node : (is_array($node) ? $node['type'] : $node->type);
  $type = content_types($type_name);
  $field_types = _content_field_types();

  $return = array();
  foreach ($type['fields'] as $field) {
    $items = isset($node->$field['field_name']) ? $node->$field['field_name'] : array();

    // Make sure AHAH 'add more' button isn't sent to the fields for processing.
    unset($items[$field['field_name'] .'_add_more']);

    $module = $field_types[$field['type']]['module'];
    $function = $module .'_field';
    if (function_exists($function)) {
      $result = $function($op, $node, $field, $items, $teaser, $page);
      if (is_array($result)) {
        $return = array_merge($return, $result);
      }
      else if (isset($result)) {
        $return[] = $result;
      }
    }
    // test for values in $items in case modules added items on insert
    if (isset($node->$field['field_name']) || count($items)) {
      $node->$field['field_name'] = $items;
    }
  }
  return $return;
}

/**
 * Invoke content.module's version of a field hook.
 */
function _content_field_invoke_default($op, &$node, $teaser = NULL, $page = NULL) {
  $type_name = is_string($node) ? $node : (is_array($node) ? $node['type'] : $node->type);
  $type = content_types($type_name);
  $field_types = _content_field_types();

  $return = array();
  // The operations involving database queries are better off handled by table
  // rather than by field.
  if (in_array($op, array('load', 'insert', 'update', 'delete', 'delete revision'))) {
    return content_storage($op, $node);
  }
  else {
    foreach ($type['fields'] as $field) {
      $items = isset($node->$field['field_name']) ? $node->$field['field_name'] : array();
      $result = content_field($op, $node, $field, $items, $teaser, $page);
      if (is_array($result)) {
        $return = array_merge($return, $result);
      }
      else if (isset($result)) {
        $return[] = $result;
      }
      if (isset($node->$field['field_name'])) {
        $node->$field['field_name'] = $items;
      }
    }
  }
  return $return;
}

/**
 * Return a list of all content types.
 *
 * @param $content_type_name
 *   If set, return information on just this type.
 *
 * Do some type checking and set up empty arrays for missing
 * info to avoid foreach errors elsewhere in the code.
 */
function content_types($type_name = NULL) {
  // handle type name with either an underscore or a dash
  $type_name = !empty($type_name) ? str_replace('-', '_', $type_name) : NULL;

  $info = _content_type_info();
  if (isset($info['content types'])) {
    if (!isset($type_name)) {
      return $info['content types'];
    }
    if (isset($info['content types'][$type_name])) {
      return $info['content types'][$type_name];
    }
  }
  return array('tables' => array(), 'fields' => array(), 'extra' => array());
}

/**
 * Return a list of all fields.
 *
 * @param $field_name
 *   If not empty, return information on just this field.
 * @param $content_type_name
 *   If not empty, return information of the field within the context of this content
 *   type.
 *
 * Be sure to check empty() instead of isset() on field_name and
 * content_type_name to avoid bad results when the value is set
 * but empty, as sometimes happens in the formatter.
 */
function content_fields($field_name = NULL, $content_type_name = NULL) {
  $info = _content_type_info();
  if (isset($info['fields'])) {
    if (empty($field_name)) {
      return $info['fields'];
    }
    if (isset($info['fields'][$field_name])) {
      if (empty($content_type_name)) {
        return $info['fields'][$field_name];
      }
      if (isset($info['content types'][$content_type_name]['fields'][$field_name])) {
        return $info['content types'][$content_type_name]['fields'][$field_name];
      }
    }
  }
}

/**
 * Return a list of field types.
 */
function _content_field_types() {
  $info = _content_type_info();
  return isset($info['field types']) ? $info['field types'] : array();
}

/**
 * Return a list of widget types.
 */
function _content_widget_types() {
  $info = _content_type_info();
  return isset($info['widget types']) ? $info['widget types'] : array();
}

/**
 * Return the formatter description corresponding to a formatter name,
 * defaulting to 'default' if none is found.
 */
function _content_get_formatter($formatter_name, $field_type) {
  $field_types = _content_field_types();
  $formatters = $field_types[$field_type]['formatters'];

  if (!isset($formatters[$formatter_name]) && $formatter_name != 'hidden') {
    // This might happen when the selected formatter has been renamed in the
    // module, or if the module has been disabled since then.
    $formatter_name = 'default';
  }

  return isset($formatters[$formatter_name]) ? $formatters[$formatter_name] : FALSE;
}

/**
 * Collate all information on content types, fields, and related structures.
 *
 * @param $reset
 *   If TRUE, clear the cache and fetch the information from the database again.
 */
function _content_type_info($reset = FALSE) {
  global $language;
  static $info;

  if ($reset || !isset($info)) {
    // Make sure this function doesn't run until the tables have been created,
    // For instance: when first enabled and called from content_menu(),
    // or when uninstalled and some subsequent field module uninstall
    // attempts to refresh the data.

    // Don't try this before content module's update is run
    // to add module and active columns to the table.
    if (variable_get('content_schema_version', -1) < 6007) {
      return array();
    }

    if (!$reset && $cached = cache_get('content_type_info:'. $language->language, content_cache_tablename())) {
      $info = $cached->data;
    }
    else {
      $info = array(
        'field types' => array(),
        'widget types' => array(),
        'fields' => array(),
        'content types' => array(),
      );

      // Populate field types.
      foreach (module_list() as $module) {
        $module_field_types = module_invoke($module, 'field_info');
        if ($module_field_types) {
          foreach ($module_field_types as $name => $field_info) {
            // Truncate names to match the value that is stored in the database.
            $db_name = substr($name, 0, 32);
            $info['field types'][$db_name] = $field_info;
            $info['field types'][$db_name]['module'] = $module;
            $info['field types'][$db_name]['formatters'] = array();
          }
        }
      }

      // Populate widget types and formatters for known field types.
      foreach (module_list() as $module) {
        if ($module_widgets = module_invoke($module, 'widget_info')) {
          foreach ($module_widgets as $name => $widget_info) {
            // Truncate names to match the value that is stored in the database.
            $db_name = substr($name, 0, 32);
            $info['widget types'][$db_name] = $widget_info;
            $info['widget types'][$db_name]['module'] = $module;
            // Replace field types with db_compatible version of known field types.
            $info['widget types'][$db_name]['field types'] = array();
            foreach ($widget_info['field types'] as $field_type) {
              $field_type_db_name = substr($field_type, 0, 32);
              if (isset($info['field types'][$field_type_db_name])) {
                $info['widget types'][$db_name]['field types'][] = $field_type_db_name;
              }
            }
          }
        }

        if ($module_formatters = module_invoke($module, 'field_formatter_info')) {
          foreach ($module_formatters as $name => $formatter_info) {
            foreach ($formatter_info['field types'] as $field_type) {
              // Truncate names to match the value that is stored in the database.
              $db_name = substr($field_type, 0, 32);
              if (isset($info['field types'][$db_name])) {
                $info['field types'][$db_name]['formatters'][$name] = $formatter_info;
                $info['field types'][$db_name]['formatters'][$name]['module'] = $module;
              }
            }
          }
        }
      }

      // Populate actual field instances.
      module_load_include('inc', 'content', 'includes/content.crud');
      foreach (node_get_types('types', NULL, TRUE) as $type_name => $data) {
        $type = (array) $data;
        $type['url_str'] = str_replace('_', '-', $type['type']);
        $type['fields'] = array();
        $type['tables'] = array();
        if ($fields = content_field_instance_read(array('type_name' => $type_name))) {
          foreach ($fields as $field) {
            $db_info = content_database_info($field);
            $type['tables'][$db_info['table']] = $db_info['table'];

            // Allow external modules to translate field strings.
            $field_strings = array(
              'widget_label' => $field['widget']['label'],
              'widget_description' => $field['widget']['description'],
            );
            drupal_alter('content_field_strings', $field_strings, $field['type_name'], $field['field_name']);
            $field['widget']['label'] = $field_strings['widget_label'];
            $field['widget']['description'] = $field_strings['widget_description'];

            $type['fields'][$field['field_name']] = $field;
            // This means that content_fields($field_name) (no type name)
            // returns the last instance loaded.
            $info['fields'][$field['field_name']] = $field;
          }
          // Make sure the per-type table is added, even if no field is actually
          // stored in it.
          $table = _content_tablename($type['type'], CONTENT_DB_STORAGE_PER_CONTENT_TYPE);
          $type['tables'][$table] = $table;
        }

        // Gather information about non-CCK 'fields'.
        $extra = module_invoke_all('content_extra_fields', $type_name);
        drupal_alter('content_extra_fields', $extra, $type_name);
        // Add saved weights.
        foreach (variable_get('content_extra_weights_'. $type_name, array()) as $key => $value) {
          // Some stored entries might not exist anymore, for instance if uploads
          // have been disabled, or vocabularies removed...
          if (isset($extra[$key])) {
            $extra[$key]['weight'] = $value;
          }
        }
        $type['extra'] = $extra;

        $info['content types'][$type_name] = $type;
      }

      cache_set('content_type_info:'. $language->language, $info, content_cache_tablename());
    }
  }
  return $info;
}

/**
 *  Implementation of hook_node_type()
 *  React to change in node types
 */
function content_node_type($op, $info) {
  switch ($op) {
    case 'insert':
      module_load_include('inc', 'content', 'includes/content.crud');
      content_type_create($info);
      break;
    case 'update':
      module_load_include('inc', 'content', 'includes/content.crud');
      content_type_update($info);
      break;
    case 'delete':
      module_load_include('inc', 'content', 'includes/content.crud');
      content_type_delete($info);
      break;
  }
}

/**
 * Clear the cache of content_types; called in several places when content
 * information is changed.
 */
function content_clear_type_cache($rebuild_schema = FALSE) {
  cache_clear_all('*', content_cache_tablename(), TRUE);
  _content_type_info(TRUE);

  // Refresh the schema to pick up new information.
  if ($rebuild_schema) {
    $schema = drupal_get_schema(NULL, TRUE);
  }

  if (module_exists('views')) {
    // Needed because this can be called from .install files
    module_load_include('module', 'views');
    views_invalidate_cache();
  }
}

/**
 * Retrieve the database storage location(s) for a field.
 *
 * TODO: add a word about why it's not included in the global _content_type_info array.
 *
 * @param $field
 *   The field whose database information is requested.
 * @return
 *   An array with the keys:
 *     "table": The name of the database table where the field data is stored.
 *     "columns": An array of columns stored for this field. Each is a collection
 *       of information returned from hook_field_settings('database columns'),
 *       with the addition of a "column" attribute which holds the name of the
 *       database column that stores the data.
 */
function content_database_info($field) {
  $db_info = array();
  if ($field['db_storage'] == CONTENT_DB_STORAGE_PER_FIELD) {
    $db_info['table'] = _content_tablename($field['field_name'], CONTENT_DB_STORAGE_PER_FIELD);
  }
  else {
    $db_info['table'] = _content_tablename($field['type_name'], CONTENT_DB_STORAGE_PER_CONTENT_TYPE);
  }

  $db_info['columns'] = (array) $field['columns'];
  // Generate column names for this field from generic column names.
  foreach ($db_info['columns'] as $column_name => $attributes) {
    $db_info['columns'][$column_name]['column'] = $field['field_name'] .'_'. $column_name;
  }

  return $db_info;
}

/**
 * Helper function for identifying the storage type for a field.
 */
function content_storage_type($field) {
  if ($field['multiple'] > 0) {
    return CONTENT_DB_STORAGE_PER_FIELD;
  }
  else {
    module_load_include('inc', 'content', 'includes/content.crud');
    $instances = content_field_instance_read(array('field_name' => $field['field_name']));
    if (count($instances) > 1) {
      return CONTENT_DB_STORAGE_PER_FIELD;
    }
  }
  return CONTENT_DB_STORAGE_PER_CONTENT_TYPE;
}

/**
 * Manipulate a 2D array to reverse rows and columns.
 *
 * The default data storage for fields is delta first, column names second.
 * This is sometimes inconvenient for field modules, so this function can be
 * used to present the data in an alternate format.
 *
 * @param $array
 *   The array to be transposed. It must be at least two-dimensional, and
 *   the subarrays must all have the same keys or behavior is undefined.
 * @return
 *   The transposed array.
 */
function content_transpose_array_rows_cols($array) {
  $result = array();
  if (is_array($array)) {
    foreach ($array as $key1 => $value1) {
      if (is_array($value1)) {
        foreach ($value1 as $key2 => $value2) {
          if (!isset($result[$key2])) {
            $result[$key2] = array();
          }
          $result[$key2][$key1] = $value2;
        }
      }
    }
  }
  return $result;
}

/**
 * Helper function to flatten an array of allowed values.
 *
 * @param $array
 *   A single or multidimensional array.
 * @return
 *   A flattened array.
 */
function content_array_flatten($array) {
  $result = array();
  if (is_array($array)) {
    foreach ($array as $key => $value) {
      if (is_array($value)) {
        $result += content_array_flatten($value);
      }
      else {
        $result[$key] = $value;
      }
    }
  }
  return $result;
}

/**
 *  Create an array of the allowed values for this field.
 *
 *  Used by number and text fields, expects to find either
 *  PHP code that will return the correct value, or a string
 *  with keys and labels separated with '|' and with each
 *  new value on its own line.
 *
 * @param $field
 *   The field whose allowed values are requested.
 * @param $flatten
 *   Optional. Use TRUE to return a flattened array (default).
 *   FALSE can be used to support optgroups for select widgets
 *   when allowed values list is generated using PHP code.
 */
function content_allowed_values($field, $flatten = TRUE) {
  static $allowed_values;

  $cid = $field['field_name'] .':'. ($flatten ? '1' : '0');
  if (isset($allowed_values[$cid])) {
    return $allowed_values[$cid];
  }

  $allowed_values[$cid] = array();

  if (isset($field['allowed_values_php'])) {
    ob_start();
    $result = eval($field['allowed_values_php']);
    if (is_array($result)) {
      if ($flatten) {
        $result = content_array_flatten($result);
      }
      $allowed_values[$cid] = $result;
    }
    ob_end_clean();
  }

  if (empty($allowed_values[$cid]) && isset($field['allowed_values'])) {
    $list = explode("\n", $field['allowed_values']);
    $list = array_map('trim', $list);
    $list = array_filter($list, 'strlen');
    foreach ($list as $opt) {
      // Sanitize the user input with a permissive filter.
      $opt = content_filter_xss($opt);
      if (strpos($opt, '|') !== FALSE) {
        list($key, $value) = explode('|', $opt);
        $allowed_values[$cid][$key] = (isset($value) && $value !=='') ? $value : $key;
      }
      else {
        $allowed_values[$cid][$opt] = $opt;
      }
    }
    // Allow external modules to translate allowed values list.
    drupal_alter('content_allowed_values', $allowed_values[$cid], $field);
  }
  return $allowed_values[$cid];
}

/**
 * Filter out HTML from allowed values array while leaving entities unencoded.
 *
 * @see content_allowed_values()
 * @see optionwidgets_select_process()
 * @see content_handler_filter_many_to_one::allowed_values()
 */
function content_allowed_values_filter_html(&$options) {
  foreach ($options as $key => $opt) {
    if (is_array($opt)) {
      content_allowed_values_filter_html($options[$key]);
    }
    else {
      $options[$key] = html_entity_decode(strip_tags($opt), ENT_QUOTES);
    }
  }
}

/**
 * Like filter_xss_admin(), but with a shorter list of allowed tags.
 *
 * Used for items entered by administrators, like field descriptions,
 * allowed values, where some (mainly inline) mark-up may be desired
 * (so check_plain() is not acceptable).
 */
function content_filter_xss($string) {
  return filter_xss($string, _content_filter_xss_allowed_tags());
}

/**
 * List of tags allowed by content_filter_xss().
 */
function _content_filter_xss_allowed_tags() {
  return array('a', 'b', 'big',  'code', 'del', 'em', 'i', 'ins',  'pre', 'q', 'small', 'span', 'strong', 'sub', 'sup', 'tt', 'ol', 'ul', 'li', 'p', 'br', 'img');
}

/**
 * Human-readable list of allowed tags, for display in help texts.
 */
function _content_filter_xss_display_allowed_tags() {
  return '<'. implode('> <', _content_filter_xss_allowed_tags()) .'>';
}

/**
 * Format a field item for display.
 *
 * Used to display a field's values outside the context of the $node, as
 * when fields are displayed in Views, or to display a field in a template
 * using a different formatter than the one set up on the Display Fields tab
 * for the node's context.
 *
 * @param $field
 *   Either a field array or the name of the field.
 * @param $item
 *   The field item(s) to be formatted (such as $node->field_foo[0],
 *   or $node->field_foo if the formatter handles multiple values itself)
 * @param $formatter_name
 *   The name of the formatter to use.
 * @param $node
 *   Optionally, the containing node object for context purposes and
 *   field-instance options.
 *
 * @return
 *   A string containing the contents of the field item(s) sanitized for display.
 *   It will have been passed through the necessary check_plain() or check_markup()
 *   functions as necessary.
 */
function content_format($field, $item, $formatter_name = 'default', $node = NULL) {
  if (!is_array($field)) {
    $field = content_fields($field);
  }

  if (content_access('view', $field, NULL, $node) && $formatter = _content_get_formatter($formatter_name, $field['type'])) {
    $theme = $formatter['module'] .'_formatter_'. $formatter_name;

    $element = array(
      '#theme' => $theme,
      '#field_name' => $field['field_name'],
      '#type_name' => isset($node->type) ? $node->type :'',
      '#formatter' => $formatter_name,
      '#node' => $node,
      '#delta' => isset($item['#delta']) ? $item['#delta'] : NULL,
    );

    if (content_handle('formatter', 'multiple values', $formatter) == CONTENT_HANDLE_CORE) {
      // Single value formatter.

      // hook_field('sanitize') expects an array of items, so we build one.
      $items = array($item);
      $function = $field['module'] .'_field';
      if (function_exists($function)) {
        $function('sanitize', $node, $field, $items, FALSE, FALSE);
      }

      $element['#item'] = $items[0];
    }
    else {
      // Multiple values formatter.
      $items = $item;
      $function = $field['module'] .'_field';
      if (function_exists($function)) {
        $function('sanitize', $node, $field, $items, FALSE, FALSE);
      }

      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#item' => $item,
          '#weight' => $delta,
        );
      }
    }

    return theme($theme, $element);
  }
}

/**
 * Registry of available node build modes.
 *
 * @param $selector
 *   Determines what information should be returned.
 * @return
 *   Depending on the value of the $selector parameter:
 *   - NULL: a flat list of all available build modes.
 *   The other two options are mainly used internally by CCK's UI:
 *   - '_tabs': the list of tabs to be shown on the 'Display fields' screens.
 *   - a string tab id: the build modes in this tab.
 */
function content_build_modes($selector = NULL) {
  static $info;

  if (!isset($info)) {
    $data = array();
    foreach (module_implements('content_build_modes') as $module) {
      $function = $module .'_content_build_modes';
      $data = array_merge($data, (array) $function());
    }
    $flat = array();
    foreach ($data as $tab) {
      // Use the + operator to preserve numeric indexes (core build modes).
      $flat += (array) $tab['build modes'];
    }
    $info = array('tabs' => $data, 'build modes' => $flat);
  }

  if ($selector === '_tabs') {
    return $info['tabs'];
  }
  elseif (isset($selector) && isset($info['tabs'][$selector])) {
    return isset($info['tabs'][$selector]) ? $info['tabs'][$selector]['build modes'] : array();
  }
  else {
    return $info['build modes'];
  }
}

/**
 * Implementations of hook_content_build_modes
 * on behalf of core modules.
 *
 * @return
 * An array describing the build modes used by the module.
 * They are grouped by secondary tabs on CCK's 'Display fields' screens.
 *
 * Expected format:
 * array(
 *   // The first level keys (tab1_url, tab2_url) will be used to generate
 *   // the url of the tab: admin/content/node-type/[type_name]/display/[tab1_url]
 *   // A module can add its render modes to a tab defined by another module.
 *   // In this case, there's no need to provide a 'title' for this tab.
 *   'tab1_url' => array(
 *     'title' => t('The human-readable title of the tab'),
 *     'build modes' => array(
 *       // The keys of the 'context' array are the values used in $node->build_mode.
 *       'mymodule_mode1' => array(
 *         'title' => t('The human-readable name of the build mode'),
 *        // The 'views style' property determines if the render mode should be
 *        // available as an option in Views' 'node' row style (not implemented yet).
 *        'views style' => TRUE,
 *       ),
 *       'mymodule_mode2' => array(
 *         'title' => t('Mode 2'),
 *         'views style' => TRUE,
 *       ),
 *     ),
 *   ),
 *   'tab2_url' => array(
 *     // ...
 *   ),
 * );
 */
function node_content_build_modes() {
  return array(
    'basic' => array(
      'title' => t('Basic'),
      'build modes' => array(
        'teaser' => array(
          'title' => t('Teaser'),
          'views style' => TRUE,
        ),
        'full' => array(
          'title' => t('Full node'),
          'views style' => TRUE,
        ),
      ),
    ),
    'rss' => array(
      'title' => t('RSS'),
      'build modes' => array(
        NODE_BUILD_RSS => array(
          'title' => t('RSS'),
          'views style' => FALSE,
        ),
      ),
    ),
  );
}
function search_content_build_modes() {
  return array(
    'search' => array(
      'title' => t('Search'),
      'build modes' => array(
        NODE_BUILD_SEARCH_INDEX => array(
          'title' => t('Search Index'),
          'views style' => FALSE,
        ),
        NODE_BUILD_SEARCH_RESULT => array(
          'title' => t('Search Result'),
          'views style' => FALSE,
        ),
      ),
    ),
  );
}
function book_content_build_modes() {
  return array(
    'print' => array(
      'title' => t('Print'),
      'build modes' => array(
        NODE_BUILD_PRINT => array(
          'title' => t('Print'),
          'views style' => TRUE,
        ),
      ),
    ),
  );
}

/**
 * Generate a table name for a field or a content type.
 *
 * @param $name
 *   The name of the content type or content field
 * @param $storage
 *   CONTENT_DB_STORAGE_PER_FIELD or CONTENT_DB_STORAGE_PER_CONTENT_TYPE
 * @return
 *   A string containing the generated name for the database table
 */
function _content_tablename($name, $storage, $version = NULL) {
  if (is_null($version)) {
    $version = variable_get('content_schema_version', 0);
  }

  if ($version < 1003) {
    $version = 0;
  }
  else {
    $version = 1003;
  }

  $name = str_replace('-', '_', $name);
  switch ("$version-$storage") {
    case '0-'. CONTENT_DB_STORAGE_PER_CONTENT_TYPE :
      return "node_$name";
    case '0-'. CONTENT_DB_STORAGE_PER_FIELD :
      return "node_data_$name";
    case '1003-'. CONTENT_DB_STORAGE_PER_CONTENT_TYPE :
      return "content_type_$name";
    case '1003-'. CONTENT_DB_STORAGE_PER_FIELD :
      return "content_$name";
  }
}

/**
 * Generate table name for the content field table.
 *
 * Needed because the table name changes depending on version.
 * Using 'content_node_field' instead of 'content_field'
 * to avoid conflicts with field tables that will be prefixed
 * with 'content_field'.
 */
function content_field_tablename($version = NULL) {
  if (is_null($version)) {
    $version = variable_get('content_schema_version', 0);
  }
  return $version < 6001 ? 'node_field' : 'content_node_field';
}

/**
 * Generate table name for the content field instance table.
 *
 * Needed because the table name changes depending on version.
 */
function content_instance_tablename($version = NULL) {
  if (is_null($version)) {
    $version = variable_get('content_schema_version', 0);
  }
  return $version < 6001 ? 'node_field_instance' : 'content_node_field_instance';
}

/**
 * Generate table name for the content cache table.
 *
 * Needed because the table name changes depending on version. Because of
 * a new database column, the content_cache table will be unusable until
 * update 6000 runs, so the cache table will be used instead.
 */
function content_cache_tablename() {
  if (variable_get('content_schema_version', -1) < 6000) {
    return 'cache';
  }
  else {
    return 'cache_content';
  }
}

/**
 * A basic schema used by all field and type tables.
 *
 * This will only add the columns relevant for the specified field.
 * Leave $field['columns'] empty to get only the base schema,
 * otherwise the function will return the whole thing.
 */
function content_table_schema($field = NULL) {
  $schema = array(
    'fields' => array(
      'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
      'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0)
    ),
    'primary key' => array('vid'),
    'indexes' => array(
      'nid'    => array('nid'),
    ),
  );

  // Add delta column if needed.
  if (!empty($field['multiple'])) {
    $schema['fields']['delta'] = array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0);
    $schema['primary key'][] = 'delta';
  }
  $schema['content fields'] = array();

  // Add field columns column if needed.
  // This function is called from install files where it is not safe
  // to use content_fields() or content_database_info(), so we
  // just used the column values stored in the $field.
  // We also need the schema to include fields from disabled modules
  // or there will be no way to delete those fields.

  if (!empty($field['columns'])) {
    foreach ($field['columns'] as $column => $attributes) {
      $column_name =  $field['field_name'] .'_'. $column;
      if (isset($attributes['index']) && $attributes['index']) {
        $schema['indexes'][$column_name] = array($column_name);
        unset($attributes['index']);
      }
      unset($attributes['column']);
      unset($attributes['sortable']);
      $schema['fields'][$column_name] = $attributes;
    }
    $schema['content fields'][] = $field['field_name'];
  }
  return $schema;
}

/**
 * Checks if an index exists.
 *
 * @todo: May we remove this funcion when implemented by Drupal core itself?
 * @link http://drupal.org/node/360854
 * @link http://dev.mysql.com/doc/refman/5.0/en/extended-show.html
 *
 * @param $table
 *   Name of the table.
 * @param $name
 *   Name of the index.
 * @return
 *   TRUE if the table exists. Otherwise FALSE.
 */
function content_db_index_exists($table, $name) {
  global $db_type;
  if ($db_type == 'mysql' || $db_type == 'mysqli') {
    if (version_compare(db_version(), '5.0.3') < 0) {
      // Earlier versions of MySQL don't support a WHERE clause for SHOW.
      $result = db_query('SHOW INDEX FROM {'. $table .'}');
      while ($row = db_fetch_array($result)) {
        if ($row['Key_name'] == $name) {
          return TRUE;
        }
      }
      return FALSE;
    }
    return (bool)db_result(db_query("SHOW INDEX FROM {". $table ."} WHERE key_name = '$name'"));
  }
  elseif ($db_type == 'pgsql') {
    // Note that the index names in Schema API for PostgreSQL are prefixed by
    // the table name and suffixed by '_idx'.
    return (bool)db_result(db_query("SELECT COUNT(indexname) FROM pg_indexes WHERE indexname = '{". $table ."}_{$name}_idx'"));
  }
  return FALSE;
}

/**
 *  Helper function for determining the behavior of a field or a widget
 *  with respect to a given operation. (currently used for field 'view',
 *  and widget 'default values' and 'multiple values')
 *
 *  @param $entity
 *    'field' or 'widget'
 *  @param $op
 *    the name of the operation ('view', 'default value'...)
 *  @param $field
 *    The field array, including widget info.
 *  @return
 *    CONTENT_CALLBACK_NONE    - do nothing for this operation
 *    CONTENT_CALLBACK_CUSTOM  - use the module's callback function.
 *    CONTENT_CALLBACK_DEFAULT - use content module default behavior
 *
 */
function content_callback($entity, $op, $field) {
  switch ($entity) {
    case 'field':
      $info = module_invoke($field['module'], "field_info");
      return isset($info[$field['type']]['callbacks'][$op]) ? $info[$field['type']]['callbacks'][$op] : CONTENT_CALLBACK_DEFAULT;

    case 'widget':
      $info = module_invoke($field['widget']['module'], "widget_info");
      return isset($info[$field['widget']['type']]['callbacks'][$op]) ? $info[$field['widget']['type']]['callbacks'][$op] : CONTENT_CALLBACK_DEFAULT;
  }
}

/**
 *  Helper function for determining the handling of a field, widget or
 *  formatter with respect to a given operation.
 *
 *  Currently used for widgets and formatters 'multiple values'.
 *
 *  @param $entity
 *    'field', 'widget' or 'formatter'
 *  @param $op
 *    the name of the operation ('default values'...)
 *  @param $object
 *    - if $entity is 'field' or 'widget': the field array,
 *      including widget info.
 *    - if $entity is 'formater': the formatter array.
 *  @return
 *    CONTENT_HANDLE_CORE    - the content module handles this operation.
 *    CONTENT_HANDLE_MODULE  - the implementing module handles this operation.
 */
function content_handle($entity, $op, $object) {
  switch ($entity) {
    case 'field':
      $info = module_invoke($object['module'], "field_info");
      return isset($info[$object['type']][$op]) ? $info[$object['type']][$op] : CONTENT_HANDLE_CORE;

    case 'widget':
      $info = module_invoke($object['widget']['module'], "widget_info");
      return isset($info[$object['widget']['type']][$op]) ? $info[$object['widget']['type']][$op] : CONTENT_HANDLE_CORE;

    case 'formatter':
      // Much simpler, formatters arrays *are* the 'formatter_info' itself.
      // We let content_handle deal with them only for code consistency.
      return isset($object[$op]) ? $object[$op] : CONTENT_HANDLE_CORE;
  }
}

/**
 *  Helper function to return the correct default value for a field.
 *
 *  @param $node
 *    The node.
 *  @param $field
 *    The field array.
 *  @param $items
 *    The value of the field in the node.
 *  @return
 *    The default value for that field.
 */
function content_default_value(&$form, &$form_state, $field, $delta) {
  $widget_types = _content_widget_types();
  $module = $widget_types[$field['widget']['type']]['module'];

  $default_value = array();
  if (!empty($field['widget']['default_value_php'])) {
    ob_start();
    $result = eval($field['widget']['default_value_php']);
    ob_end_clean();
    if (is_array($result)) {
      $default_value = $result;
    }
  }
  elseif (!empty($field['widget']['default_value'])) {
    $default_value = $field['widget']['default_value'];
  }
  return (array) $default_value;
}

/**
 * Determine whether the user has access to a given field.
 *
 * @param $op
 *   The operation to be performed. Possible values:
 *   - "edit"
 *   - "view"
 * @param $field
 *   The field on which the operation is to be performed.
 * @param $account
 *   (optional) The account to check, if not given use currently logged in user.
 * @param $node
 *   (optional) The node on which the operation is to be performed.
 * @return
 *   TRUE if the operation is allowed;
 *   FALSE if the operation is denied.
 */
function content_access($op, $field, $account = NULL, $node = NULL) {
  global $user;

  if (is_null($account)) {
    $account = $user;
  }
  // Check for valid field data.
  if (!isset($field['field_name'])) {
    return FALSE;
  }
  $access = module_invoke_all('field_access', $op, $field, $account, $node);
  foreach ($access as $value) {
    if ($value === FALSE) {
      return FALSE;
    }
  }
  return TRUE;
}

 /**
 * Hide specified fields from the $content variable in node templates.
 */
function content_field_wrapper_post_render($content, $element) {
  $field = content_fields($element['#field_name'], $element['#type_name']);
  if (theme('content_exclude', $content, $field, $element['#context'])) {
    return '';
  }
  return $content;
}


/**
 * 'Theme' function for a field's addition to $content.
 *
 * Adapts the all-inclusive $content variable in node templates to allow
 * some field content to be excluded. This is a theme function, so it can be
 * overridden in different themes to produce different results.
 *
 * The html for individual fields and groups are available in the
 * $FIELD_NAME_rendered and $GROUP_NAME_rendered variables.
 *
 * This allows more flexibility in node templates : you can use custom markup
 * around a few specific fields, and print the rest of the node with $content.
 *
 * @param $content
 *    The themed content for this field or group.
 *
 * @param $object
 *    The field or group array for this item.
 *    $object['#type_name'] holds the content type.
 *    $object['#field_name'] holds the field name (if a field).
 *    $object['#group_name'] holds the group name (if a group).
 *    $object['display_settings'] holds the display settings
 *    for all contexts, in an array like:
 *      $object['display_settings'] => array(
 *        'full' => array(
 *          'format' => 'default',
 *          'exclude' => 0,
 *         ),
 *        'teaser' => array(
 *          'format' => 'default',
 *          'exclude' => 1,
 *         ),
 *      );
 *
 * @param $context
 *    The context for which the node is being rendered.
 *    Can be one of the following values :
 *    - 'teaser'
 *    - 'full'
 *    - NODE_BUILD_SEARCH_INDEX
 *    - NODE_BUILD_SEARCH_RESULT
 *    - NODE_BUILD_RSS
 *    - NODE_BUILD_PRINT
 *    - ... any other custom build mode exposed by 3rd party modules using
 *      hook_content_build_modes().
 *
 * @return
 *   Whether or not content is to be added to $content in this context.
 *   Uses the value of the 'Exclude' checkbox for this field
 *   as set on the Manage fields screen.
 */
function theme_content_exclude($content, $object, $context) {
  // The field may be missing info for $contexts added by modules
  // enabled after the field was last edited.
  if (empty($object['display_settings'])
    || empty($object['display_settings'][$context])
    || !is_array($object['display_settings'][$context])
    || empty($object['display_settings'][$context]['exclude'])) {
    return FALSE;
  }
  else {
    return TRUE;
  }
}

/**
 * Theme preprocess function for field.tpl.php.
 *
 * The $variables array contains the following arguments:
 * - $node
 * - $field
 * - $items
 * - $teaser
 * - $page
 *
 * @see field.tpl.php
 *
 * TODO : this should live in theme/theme.inc, but then the preprocessor
 * doesn't get called when the theme overrides the template. Bug in theme layer ?
 */
function content_preprocess_content_field(&$variables) {
  $element = $variables['element'];
  $field = content_fields($element['#field_name'], $element['#node']->type);

  $variables['node'] = $element['#node'];
  $variables['field'] = $field;
  $variables['items'] = array();

  if ($element['#single']) {
    // Single value formatter.
    foreach (element_children($element['items']) as $delta) {
      $variables['items'][$delta] = $element['items'][$delta]['#item'];
      // Use isset() to avoid undefined index message on #children when field values are empty.
      $variables['items'][$delta]['view'] = isset($element['items'][$delta]['#children']) ? $element['items'][$delta]['#children'] : '';
    }
  }
  else  {
    // Multiple values formatter.
    // We display the 'all items' output as $items[0], as if it was the
    // output of a single valued field.
    // Raw values are still exposed for all items.
    foreach (element_children($element['items']) as $delta) {
      $variables['items'][$delta] = $element['items'][$delta]['#item'];
    }
    $variables['items'][0]['view'] = $element['items']['#children'];
  }

  $variables['teaser'] = $element['#teaser'];
  $variables['page'] = $element['#page'];

  $field_empty = TRUE;

  foreach ($variables['items'] as $delta => $item) {
    if (!isset($item['view']) || (empty($item['view']) && (string)$item['view'] !== '0')) {
      $variables['items'][$delta]['empty'] = TRUE;
    }
    else {
      $field_empty = FALSE;
      $variables['items'][$delta]['empty'] = FALSE;
    }
  }

  $additions = array(
    'field_type' => $field['type'],
    'field_name' => $field['field_name'],
    'field_type_css' => strtr($field['type'], '_', '-'),
    'field_name_css' => strtr($field['field_name'], '_', '-'),
    'label' => check_plain(t($field['widget']['label'])),
    'label_display' => $element['#label_display'],
    'field_empty' => $field_empty,
    'template_files' => array(
      'content-field',
      'content-field-'. $element['#field_name'],
      'content-field-'. $element['#node']->type,
      'content-field-'. $element['#field_name'] .'-'. $element['#node']->type,
    ),
  );
  $variables = array_merge($variables, $additions);
}

/**
 * Theme preprocess function for node.
 *
 * - Adds $FIELD_NAME_rendered variables
 *   containing the themed output for the whole field.
 * - Adds the formatted values in the 'view' key of the items.
 */
function content_preprocess_node(&$vars) {
  $additions = _content_field_invoke_default('preprocess_node', $vars['node']);
  $vars = array_merge($vars, $additions);
}

/**
 * Debugging using hook_content_fieldapi.
 *
 * @TODO remove later
 *
 * @param $op
 * @param $field
 */
function content_content_fieldapi($op, $field) {
  if (module_exists('devel')) {
    //dsm($op);
    //dsm($field);
  }
}

/**
 * Implementation of hook_content_extra_fields.
 *
 * Informations for non-CCK 'node fields' defined in core.
 */
function content_content_extra_fields($type_name) {
  $type = node_get_types('type', $type_name);
  $extra = array();

  if ($type->has_title) {
    $extra['title'] = array(
      'label' => $type->title_label,
      'description' => t('Node module form.'),
      'weight' => -5
    );
  }
  if ($type->has_body) {
    $extra['body_field'] = array(
      'label' => $type->body_label,
      'description' => t('Node module form.'),
      'weight' => 0,
      'view' => 'body'
    );
  }
  $extra['revision_information'] = array(
    'label' => t('Revision information'),
    'description' => t('Node module form.'),
    'weight' => 20
  );
  $extra['author'] = array(
    'label' => t('Authoring information'),
    'description' => t('Node module form.'),
    'weight' => 20,
  );
  $extra['options'] = array(
    'label' => t('Publishing options'),
    'description' => t('Node module form.'),
    'weight' => 25,
   );
  if (module_exists('comment')) {
    $extra['comment_settings'] = array(
      'label' => t('Comment settings'),
      'description' => t('Comment module form.'),
      'weight' => 30
    );
  }
  if (module_exists('locale') && variable_get("language_content_type_$type_name", 0)) {
    $extra['language'] = array(
      'label' => t('Language'),
      'description' => t('Locale module form.'),
      'weight' => 0
    );
  }
  if (module_exists('translation') && translation_supported_type($type_name)) {
    $extra['translation'] = array(
      'label' => t('Translation settings'),
      'description' => t('Translation module form.'),
      'weight' => 30
    );
  }
  if (module_exists('menu')) {
    $extra['menu'] = array(
      'label' => t('Menu settings'),
      'description' => t('Menu module form.'),
      'weight' => -2
    );
  }
  if (module_exists('taxonomy') && taxonomy_get_vocabularies($type_name)) {
    $extra['taxonomy'] = array(
      'label' => t('Taxonomy'),
      'description' => t('Taxonomy module form.'),
      'weight' => -3
    );
  }
  if (module_exists('book')) {
    $extra['book'] = array(
      'label' => t('Book'),
      'description' => t('Book module form.'),
      'weight' => 10
    );
  }
  if (module_exists('path')) {
    $extra['path'] = array(
      'label' => t('Path settings'),
      'description' => t('Path module form.'),
      'weight' => 30
    );
  }
  if ($type_name == 'poll' && module_exists('poll')) {
    $extra['title'] = array(
      'label' => t('Poll title'),
      'description' => t('Poll module title.'),
      'weight' => -5
    );
    $extra['choice_wrapper'] = array(
      'label' => t('Poll choices'),
      'description' => t('Poll module choices.'),
      'weight' => -4
    );
    $extra['settings'] = array(
      'label' => t('Poll settings'),
      'description' => t('Poll module settings.'),
      'weight' => -3
    );
  }
  if (module_exists('upload') && variable_get("upload_$type_name", TRUE)) {
    $extra['attachments'] = array(
      'label' => t('File attachments'),
      'description' => t('Upload module form.'),
      'weight' => 30,
      'view' => 'files'
    );
  }

  return $extra;
}

/**
 * Retrieve the user-defined weight for non-CCK node 'fields'.
 *
 * CCK's 'Manage fields' page lets users reorder node fields, including non-CCK
 * items (body, taxonomy, other hook_nodeapi-added elements by contrib modules...).
 * Contrib modules that want to have their 'fields' supported need to expose
 * them with hook_content_extra_fields, and use this function to retrieve the
 * user-defined weight.
 *
 * @param $type_name
 *   The content type name.
 * @param $pseudo_field_name
 *   The name of the 'field'.
 * @return
 *   The weight for the 'field', respecting the user settings stored
 *   by content.module.
 */
function content_extra_field_weight($type_name, $pseudo_field_name) {
  $type = content_types($type_name);

  // If we don't have the requested item, this may be because the cached
  // information for 'extra' fields hasn't been refreshed yet.
  if (!isset($type['extra'][$pseudo_field_name])) {
    content_clear_type_cache();
    $type = content_types($type_name);
  }

  if (isset($type['extra'][$pseudo_field_name])) {
    return $type['extra'][$pseudo_field_name]['weight'];
  }
}

/**
 * Find max delta value actually in use for a field.
 *
 * Helper function to do things like tell when we should prevent a
 * change in multiple value settings that would result in data loss,
 * or know if content actually exists for a field.
 *
 * @param $field_name
 *   The field name to examine.
 * @param $type_name
 *   If provided, search only for existing data in that type,
 *   otherwise search for all instances of field data in all types.
 * @return
 *   NULL if field is not in use, or the maximum delta value in use.
 *
 * TODO
 * Go back to the field settings validation and use this function
 * to prevent (or confirm) changes in multiple values that
 * would destroy data.
 *
 * Fields with only NULL data will show up as being in use.
 * Do we want to eliminate them from the results?
 */
function content_max_delta($field_name, $type_name = NULL) {
  $fields = content_fields();
  $field = $fields[$field_name];

  // Non-multiple value fields don't use the delta column,
  // but could exist in multiple databases. If any value
  // exists in any examined table, the max delta will be zero.
  if (empty($field['multiple'])) {
    $content_types = content_types();
    foreach ($content_types as $content_type) {
      if (empty($type_name) || $content_type['type'] == $type_name) {
        foreach ($content_type['fields'] as $field) {
          $db_info = content_database_info($field);
          if (db_result(db_query("SELECT COUNT(*) FROM {". $db_info['table'] ."}")) >= 1) {
            return 0;
          }
        }
      }
    }
  }
  // Multiple value fields always share the same table and use the delta.
  // If we want to find delta values for a particular type, we join
  // in the node table to limit the type.
  else {
    $db_info = content_database_info($field);
    if (!empty($type_name)) {
      $delta = db_result(db_query("SELECT MAX(delta) FROM {". $db_info['table'] ."} f LEFT JOIN {node} n ON f.vid = n.vid WHERE n.type = '%s'", $type_name));
    }
    else {
      $delta = db_result(db_query("SELECT MAX(delta) FROM {". $db_info['table'] ."}"));
    }
    if ($delta >= 0) {
      return $delta;
    }
  }
  // If we got this far, there is no data for this field.
  return NULL;
}

/**
 * Helper function to identify inactive fields.
 */
function content_inactive_fields($type_name = NULL) {
  module_load_include('inc', 'content', 'includes/content.crud');
  if (!empty($type_name)) {
    $param = array('type_name' => $type_name);
    $inactive = array($type_name => array());
  }
  else {
    $param = array();
    $inactive = array();
  }
  $all = content_field_instance_read($param, TRUE);
  $active = array_keys(content_fields());
  foreach ($all as $field) {
    if (!in_array($field['field_name'], $active)) {
      $inactive[$field['type_name']][$field['field_name']] = content_field_instance_expand($field);
    }
  }
  if (!empty($type_name)) {
    return $inactive[$type_name];
  }
  return $inactive;
}

/**
 * Helper function to identify inactive instances.
 * This will be the same results as content_inactive_fields(),
 * EXCEPT that his function will return inactive instances even 
 * if the fields have other (shared) instances that are still active.
 */
function content_inactive_instances($type_name = NULL) {
  module_load_include('inc', 'content', 'includes/content.crud');
  if (!empty($type_name)) {
    $param = array('type_name' => $type_name);
    $inactive = array($type_name => array());
  }
  else {
    $param = array();
    $inactive = array();
  }
  $all = content_field_instance_read($param, TRUE);
  foreach ($all as $field) {
    $inactive[$field['type_name']][$field['field_name']] = content_field_instance_expand($field);
  }
  if (!empty($type_name)) {
    return $inactive[$type_name];
  }
  return $inactive;
}