field_ui.module 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /**
  3. * @file
  4. * Allows administrators to attach custom fields to fieldable types.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function field_ui_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#field_ui':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $output .= '<p>' . t('The Field UI module provides an administrative user interface (UI) for attaching and managing fields. Fields can be defined at the content-type level for content items and comments, at the vocabulary level for taxonomy terms, and at the site level for user accounts. Other modules may also enable fields to be defined for their data. Field types (text, image, number, etc.) are defined by modules, and collected and managed by the <a href="@field">Field module</a>. For more information, see the online handbook entry for <a href="@field_ui" target="_blank">Field UI module</a>.', array('@field' => url('admin/help/field'), '@field_ui' => 'http://drupal.org/documentation/modules/field-ui')) . '</p>';
  15. $output .= '<h3>' . t('Uses') . '</h3>';
  16. $output .= '<dl>';
  17. $output .= '<dt>' . t('Planning fields') . '</dt>';
  18. $output .= '<dd>' . t('There are several decisions you will need to make before defining a field for content, comments, etc.:') . '<dl>';
  19. $output .= '<dt>' . t('What the field will be called') . '</dt>';
  20. $output .= '<dd>' . t('A field has a <em>label</em> (the name displayed in the user interface) and a <em>machine name</em> (the name used internally). The label can be changed after you create the field, if needed, but the machine name cannot be changed after you have created the field.') . '</li>';
  21. $output .= '<dt>' . t('What type of data the field will store') . '</dt>';
  22. $output .= '<dd>' . t('Each field can store one type of data (text, number, file, etc.). When you define a field, you choose a particular <em>field type</em>, which corresponds to the type of data you want to store. The field type cannot be changed after you have created the field.') . '</dd>';
  23. $output .= '<dt>' . t('How the data will be input and displayed') . '</dt>';
  24. $output .= '<dd>' . t('Each field type has one or more available <em>widgets</em> associated with it; each widget provides a mechanism for data input when you are editing (text box, select list, file upload, etc.). Each field type also has one or more display options, which determine how the field is displayed to site visitors. The widget and display options can be changed after you have created the field.') . '</dd>';
  25. $output .= '<dt>' . t('How many values the field will store') . '</dt>';
  26. $output .= '<dd>' . t('You can store one value, a specific maximum number of values, or an unlimited number of values in each field. For example, an employee identification number field might store a single number, whereas a phone number field might store multiple phone numbers. This setting can be changed after you have created the field, but if you reduce the maximum number of values, you may lose information.') . '</dd>';
  27. $output .= '</dl>';
  28. $output .= '<dt>' . t('Reusing fields') . '</dt>';
  29. $output .= '<dd>' . t('Once you have defined a field, you can reuse it. For example, if you define a custom image field for one content type, and you need to have an image field with the same parameters on another content type, you can add the same field to the second content type, in the <em>Add existing field</em> area of the user interface. You could also add this field to a taxonomy vocabulary, comments, user accounts, etc.') . '</dd>';
  30. $output .= '<dd>' . t('Some settings of a reused field are unique to each use of the field; others are shared across all places you use the field. For example, the label of a text field is unique to each use, while the setting for the number of values is shared.') . '</dd>';
  31. $output .= '<dd>' . t('There are two main reasons for reusing fields. First, reusing fields can save you time over defining new fields. Second, reusing fields also allows you to display, filter, group, and sort content together by field across content types. For example, the contributed Views module allows you to create lists and tables of content. So if you use the same field on multiple content types, you can create a View containing all of those content types together displaying that field, sorted by that field, and/or filtered by that field.') . '</dd>';
  32. $output .= '<dt>' . t('Fields on content items') . '</dt>';
  33. $output .= '<dd>' . t('Fields on content items are defined at the content-type level, on the <em>Manage fields</em> tab of the content type edit page (which you can reach from the <a href="@types">Content types page</a>). When you define a field for a content type, each content item of that type will have that field added to it. Some fields, such as the Title and Body, are provided for you when you create a content type, or are provided on content types created by your installation profile.', array('@types' => url('admin/structure/types'))) . '</dd>';
  34. $output .= '<dt>' . t('Fields on taxonomy terms') . '</dt>';
  35. $output .= '<dd>' . t('Fields on taxonomy terms are defined at the taxonomy vocabulary level, on the <em>Manage fields</em> tab of the vocabulary edit page (which you can reach from the <a href="@taxonomy">Taxonomy page</a>). When you define a field for a vocabulary, each term in that vocabulary will have that field added to it. For example, you could define an image field for a vocabulary to store an icon with each term.', array('@taxonomy' => url('admin/structure/taxonomy'))) . '</dd>';
  36. $output .= '<dt>' . t('Fields on user accounts') . '</dt>';
  37. $output .= '<dd>' . t('Fields on user accounts are defined on a site-wide basis on the <a href="@fields">Manage fields tab</a> of the <a href="@accounts">Account settings</a> page. When you define a field for user accounts, each user account will have that field added to it. For example, you could add a long text field to allow users to include a biography.', array('@fields' => url('admin/config/people/accounts/fields'), '@accounts' => url('admin/config/people/accounts'))) . '</dd>';
  38. $output .= '<dt>' . t('Fields on comments') . '</dt>';
  39. $output .= '<dd>' . t('Fields on comments are defined at the content-type level, on the <em>Comment fields</em> tab of the content type edit page (which you can reach from the <a href="@types">Content types page</a>). When you add a field for comments, each comment on a content item of that type will have that field added to it. For example, you could add a website field to the comments on forum posts, to allow forum commenters to add a link to their website.', array('@types' => url('admin/structure/types'))) . '</dd>';
  40. $output .= '</dl>';
  41. return $output;
  42. case 'admin/reports/fields':
  43. return '<p>' . t('This list shows all fields currently in use for easy reference.') . '</p>';
  44. }
  45. }
  46. /**
  47. * Implements hook_field_attach_rename_bundle().
  48. */
  49. function field_ui_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  50. // The Field UI relies on entity_get_info() to build menu items for entity
  51. // field administration pages. Clear the entity info cache and ensure that
  52. // the menu is rebuilt.
  53. entity_info_cache_clear();
  54. menu_rebuild();
  55. }
  56. /**
  57. * Implements hook_menu().
  58. */
  59. function field_ui_menu() {
  60. $items['admin/reports/fields'] = array(
  61. 'title' => 'Field list',
  62. 'description' => 'Overview of fields on all entity types.',
  63. 'page callback' => 'field_ui_fields_list',
  64. 'access arguments' => array('administer content types'),
  65. 'type' => MENU_NORMAL_ITEM,
  66. 'file' => 'field_ui.admin.inc',
  67. );
  68. // Ensure the following is not executed until field_bundles is working and
  69. // tables are updated. Needed to avoid errors on initial installation.
  70. if (defined('MAINTENANCE_MODE')) {
  71. return $items;
  72. }
  73. // Create tabs for all possible bundles.
  74. foreach (entity_get_info() as $entity_type => $entity_info) {
  75. if ($entity_info['fieldable']) {
  76. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  77. if (isset($bundle_info['admin'])) {
  78. // Extract path information from the bundle.
  79. $path = $bundle_info['admin']['path'];
  80. // Different bundles can appear on the same path (e.g. %node_type and
  81. // %comment_node_type). To allow field_ui_menu_load() to extract the
  82. // actual bundle object from the translated menu router path
  83. // arguments, we need to identify the argument position of the bundle
  84. // name string ('bundle argument') and pass that position to the menu
  85. // loader. The position needs to be casted into a string; otherwise it
  86. // would be replaced with the bundle name string.
  87. if (isset($bundle_info['admin']['bundle argument'])) {
  88. $bundle_arg = $bundle_info['admin']['bundle argument'];
  89. $bundle_pos = (string) $bundle_arg;
  90. }
  91. else {
  92. $bundle_arg = $bundle_name;
  93. $bundle_pos = '0';
  94. }
  95. // This is the position of the %field_ui_menu placeholder in the
  96. // items below.
  97. $field_position = count(explode('/', $path)) + 1;
  98. // Extract access information, providing defaults.
  99. $access = array_intersect_key($bundle_info['admin'], drupal_map_assoc(array('access callback', 'access arguments')));
  100. $access += array(
  101. 'access callback' => 'user_access',
  102. 'access arguments' => array('administer site configuration'),
  103. );
  104. $items["$path/fields"] = array(
  105. 'title' => 'Manage fields',
  106. 'page callback' => 'drupal_get_form',
  107. 'page arguments' => array('field_ui_field_overview_form', $entity_type, $bundle_arg),
  108. 'type' => MENU_LOCAL_TASK,
  109. 'weight' => 1,
  110. 'file' => 'field_ui.admin.inc',
  111. ) + $access;
  112. $items["$path/fields/%field_ui_menu"] = array(
  113. 'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
  114. 'title callback' => 'field_ui_menu_title',
  115. 'title arguments' => array($field_position),
  116. 'page callback' => 'drupal_get_form',
  117. 'page arguments' => array('field_ui_field_edit_form', $field_position),
  118. 'file' => 'field_ui.admin.inc',
  119. ) + $access;
  120. $items["$path/fields/%field_ui_menu/edit"] = array(
  121. 'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
  122. 'title' => 'Edit',
  123. 'page callback' => 'drupal_get_form',
  124. 'page arguments' => array('field_ui_field_edit_form', $field_position),
  125. 'type' => MENU_DEFAULT_LOCAL_TASK,
  126. 'file' => 'field_ui.admin.inc',
  127. ) + $access;
  128. $items["$path/fields/%field_ui_menu/field-settings"] = array(
  129. 'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
  130. 'title' => 'Field settings',
  131. 'page callback' => 'drupal_get_form',
  132. 'page arguments' => array('field_ui_field_settings_form', $field_position),
  133. 'type' => MENU_LOCAL_TASK,
  134. 'file' => 'field_ui.admin.inc',
  135. ) + $access;
  136. $items["$path/fields/%field_ui_menu/widget-type"] = array(
  137. 'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
  138. 'title' => 'Widget type',
  139. 'page callback' => 'drupal_get_form',
  140. 'page arguments' => array('field_ui_widget_type_form', $field_position),
  141. 'type' => MENU_LOCAL_TASK,
  142. 'file' => 'field_ui.admin.inc',
  143. ) + $access;
  144. $items["$path/fields/%field_ui_menu/delete"] = array(
  145. 'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
  146. 'title' => 'Delete',
  147. 'page callback' => 'drupal_get_form',
  148. 'page arguments' => array('field_ui_field_delete_form', $field_position),
  149. 'type' => MENU_LOCAL_TASK,
  150. 'weight' => 10,
  151. 'file' => 'field_ui.admin.inc',
  152. ) + $access;
  153. // 'Manage display' tab.
  154. $items["$path/display"] = array(
  155. 'title' => 'Manage display',
  156. 'page callback' => 'drupal_get_form',
  157. 'page arguments' => array('field_ui_display_overview_form', $entity_type, $bundle_arg, 'default'),
  158. 'type' => MENU_LOCAL_TASK,
  159. 'weight' => 2,
  160. 'file' => 'field_ui.admin.inc',
  161. ) + $access;
  162. // View modes secondary tabs.
  163. // The same base $path for the menu item (with a placeholder) can be
  164. // used for all bundles of a given entity type; but depending on
  165. // administrator settings, each bundle has a different set of view
  166. // modes available for customisation. So we define menu items for all
  167. // view modes, and use an access callback to determine which ones are
  168. // actually visible for a given bundle.
  169. $weight = 0;
  170. $view_modes = array('default' => array('label' => t('Default'))) + $entity_info['view modes'];
  171. foreach ($view_modes as $view_mode => $view_mode_info) {
  172. $items["$path/display/$view_mode"] = array(
  173. 'title' => $view_mode_info['label'],
  174. 'page arguments' => array('field_ui_display_overview_form', $entity_type, $bundle_arg, $view_mode),
  175. // The access callback needs to check both the current 'custom
  176. // display' setting for the view mode, and the overall access
  177. // rules for the bundle admin pages.
  178. 'access callback' => '_field_ui_view_mode_menu_access',
  179. 'access arguments' => array_merge(array($entity_type, $bundle_arg, $view_mode, $access['access callback']), $access['access arguments']),
  180. 'type' => ($view_mode == 'default' ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK),
  181. 'weight' => ($view_mode == 'default' ? -10 : $weight++),
  182. 'file' => 'field_ui.admin.inc',
  183. );
  184. }
  185. }
  186. }
  187. }
  188. }
  189. return $items;
  190. }
  191. /**
  192. * Menu loader; Load a field instance based on field and bundle name.
  193. *
  194. * @param $field_name
  195. * The name of the field, as contained in the path.
  196. * @param $entity_type
  197. * The name of the entity.
  198. * @param $bundle_name
  199. * The name of the bundle, as contained in the path.
  200. * @param $bundle_pos
  201. * The position of $bundle_name in $map.
  202. * @param $map
  203. * The translated menu router path argument map.
  204. *
  205. * @return
  206. * The field instance array.
  207. *
  208. * @ingroup field
  209. */
  210. function field_ui_menu_load($field_name, $entity_type, $bundle_name, $bundle_pos, $map) {
  211. // Extract the actual bundle name from the translated argument map.
  212. // The menu router path to manage fields of an entity can be shared among
  213. // multiple bundles. For example:
  214. // - admin/structure/types/manage/%node_type/fields/%field_ui_menu
  215. // - admin/structure/types/manage/%comment_node_type/fields/%field_ui_menu
  216. // The menu system will automatically load the correct bundle depending on the
  217. // actual path arguments, but this menu loader function only receives the node
  218. // type string as $bundle_name, which is not the bundle name for comments.
  219. // We therefore leverage the dynamically translated $map provided by the menu
  220. // system to retrieve the actual bundle and bundle name for the current path.
  221. if ($bundle_pos > 0) {
  222. $bundle = $map[$bundle_pos];
  223. $bundle_name = field_extract_bundle($entity_type, $bundle);
  224. }
  225. // Check whether the field exists at all.
  226. if ($field = field_info_field($field_name)) {
  227. // Only return the field if a field instance exists for the given entity
  228. // type and bundle.
  229. if ($instance = field_info_instance($entity_type, $field_name, $bundle_name)) {
  230. return $instance;
  231. }
  232. }
  233. return FALSE;
  234. }
  235. /**
  236. * Menu title callback.
  237. */
  238. function field_ui_menu_title($instance) {
  239. return $instance['label'];
  240. }
  241. /**
  242. * Menu access callback for the 'view mode display settings' pages.
  243. */
  244. function _field_ui_view_mode_menu_access($entity_type, $bundle, $view_mode, $access_callback) {
  245. // First, determine visibility according to the 'use custom display'
  246. // setting for the view mode.
  247. $bundle = field_extract_bundle($entity_type, $bundle);
  248. $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
  249. $visibility = ($view_mode == 'default') || !empty($view_mode_settings[$view_mode]['custom_settings']);
  250. // Then, determine access according to the $access parameter. This duplicates
  251. // part of _menu_check_access().
  252. if ($visibility) {
  253. // Grab the variable 'access arguments' part.
  254. $all_args = func_get_args();
  255. $args = array_slice($all_args, 4);
  256. $callback = empty($access_callback) ? 0 : trim($access_callback);
  257. if (is_numeric($callback)) {
  258. return (bool) $callback;
  259. }
  260. else {
  261. // As call_user_func_array() is quite slow and user_access is a very
  262. // common callback, it is worth making a special case for it.
  263. if ($access_callback == 'user_access') {
  264. return (count($args) == 1) ? user_access($args[0]) : user_access($args[0], $args[1]);
  265. }
  266. elseif (function_exists($access_callback)) {
  267. return call_user_func_array($access_callback, $args);
  268. }
  269. }
  270. }
  271. }
  272. /**
  273. * Implements hook_theme().
  274. */
  275. function field_ui_theme() {
  276. return array(
  277. 'field_ui_table' => array(
  278. 'render element' => 'elements',
  279. ),
  280. );
  281. }
  282. /**
  283. * Implements hook_element_info().
  284. */
  285. function field_ui_element_info() {
  286. return array(
  287. 'field_ui_table' => array(
  288. '#theme' => 'field_ui_table',
  289. '#pre_render' => array('field_ui_table_pre_render'),
  290. '#regions' => array('' => array()),
  291. ),
  292. );
  293. }
  294. /**
  295. * Implements hook_field_attach_create_bundle().
  296. */
  297. function field_ui_field_attach_create_bundle($entity_type, $bundle) {
  298. // When a new bundle is created, the menu needs to be rebuilt to add our
  299. // menu item tabs.
  300. variable_set('menu_rebuild_needed', TRUE);
  301. }
  302. /**
  303. * Determines the administration path for a bundle.
  304. */
  305. function _field_ui_bundle_admin_path($entity_type, $bundle_name) {
  306. $bundles = field_info_bundles($entity_type);
  307. $bundle_info = $bundles[$bundle_name];
  308. if (isset($bundle_info['admin'])) {
  309. return isset($bundle_info['admin']['real path']) ? $bundle_info['admin']['real path'] : $bundle_info['admin']['path'];
  310. }
  311. }
  312. /**
  313. * Identifies inactive fields within a bundle.
  314. */
  315. function field_ui_inactive_instances($entity_type, $bundle_name = NULL) {
  316. $params = array('entity_type' => $entity_type);
  317. if (empty($bundle_name)) {
  318. $active = field_info_instances($entity_type);
  319. $inactive = array();
  320. }
  321. else {
  322. // Restrict to the specified bundle. For consistency with the case where
  323. // $bundle_name is NULL, the $active and $inactive arrays are keyed by
  324. // bundle name first.
  325. $params['bundle'] = $bundle_name;
  326. $active = array($bundle_name => field_info_instances($entity_type, $bundle_name));
  327. $inactive = array($bundle_name => array());
  328. }
  329. // Iterate on existing definitions, and spot those that do not appear in the
  330. // $active list collected earlier.
  331. $all_instances = field_read_instances($params, array('include_inactive' => TRUE));
  332. foreach ($all_instances as $instance) {
  333. if (!isset($active[$instance['bundle']][$instance['field_name']])) {
  334. $inactive[$instance['bundle']][$instance['field_name']] = $instance;
  335. }
  336. }
  337. if (!empty($bundle_name)) {
  338. return $inactive[$bundle_name];
  339. }
  340. return $inactive;
  341. }
  342. /**
  343. * Implements hook_form_FORM_ID_alter().
  344. *
  345. * Adds a button 'Save and add fields' to the 'Create content type' form.
  346. *
  347. * @see node_type_form()
  348. * @see field_ui_form_node_type_form_submit()
  349. */
  350. function field_ui_form_node_type_form_alter(&$form, $form_state) {
  351. // We want to display the button only on add page.
  352. if (empty($form['#node_type']->type)) {
  353. $form['actions']['save_continue'] = array(
  354. '#type' => 'submit',
  355. '#value' => t('Save and add fields'),
  356. '#weight' => 45,
  357. );
  358. $form['#submit'][] = 'field_ui_form_node_type_form_submit';
  359. }
  360. }
  361. /**
  362. * Form submission handler for the 'Save and add fields' button.
  363. *
  364. * @see field_ui_form_node_type_form_alter()
  365. */
  366. function field_ui_form_node_type_form_submit($form, &$form_state) {
  367. if ($form_state['triggering_element']['#parents'][0] === 'save_continue') {
  368. $form_state['redirect'] = _field_ui_bundle_admin_path('node', $form_state['values']['type']) .'/fields';
  369. }
  370. }