field_ui.module 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 fields'),
  103. );
  104. // Add the "administer fields" permission on top of the access
  105. // restriction because the field UI should only be accessible to
  106. // trusted users.
  107. if ($access['access callback'] != 'user_access' || $access['access arguments'] != array('administer fields')) {
  108. $access = array(
  109. 'access callback' => 'field_ui_admin_access',
  110. 'access arguments' => array($access['access callback'], $access['access arguments']),
  111. );
  112. }
  113. $items["$path/fields"] = array(
  114. 'title' => 'Manage fields',
  115. 'page callback' => 'drupal_get_form',
  116. 'page arguments' => array('field_ui_field_overview_form', $entity_type, $bundle_arg),
  117. 'type' => MENU_LOCAL_TASK,
  118. 'weight' => 1,
  119. 'file' => 'field_ui.admin.inc',
  120. ) + $access;
  121. $items["$path/fields/%field_ui_menu"] = array(
  122. 'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
  123. 'title callback' => 'field_ui_menu_title',
  124. 'title arguments' => array($field_position),
  125. 'page callback' => 'drupal_get_form',
  126. 'page arguments' => array('field_ui_field_edit_form', $field_position),
  127. 'file' => 'field_ui.admin.inc',
  128. ) + $access;
  129. $items["$path/fields/%field_ui_menu/edit"] = array(
  130. 'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
  131. 'title' => 'Edit',
  132. 'page callback' => 'drupal_get_form',
  133. 'page arguments' => array('field_ui_field_edit_form', $field_position),
  134. 'type' => MENU_DEFAULT_LOCAL_TASK,
  135. 'file' => 'field_ui.admin.inc',
  136. ) + $access;
  137. $items["$path/fields/%field_ui_menu/field-settings"] = array(
  138. 'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
  139. 'title' => 'Field settings',
  140. 'page callback' => 'drupal_get_form',
  141. 'page arguments' => array('field_ui_field_settings_form', $field_position),
  142. 'type' => MENU_LOCAL_TASK,
  143. 'file' => 'field_ui.admin.inc',
  144. ) + $access;
  145. $items["$path/fields/%field_ui_menu/widget-type"] = array(
  146. 'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
  147. 'title' => 'Widget type',
  148. 'page callback' => 'drupal_get_form',
  149. 'page arguments' => array('field_ui_widget_type_form', $field_position),
  150. 'type' => MENU_LOCAL_TASK,
  151. 'file' => 'field_ui.admin.inc',
  152. ) + $access;
  153. $items["$path/fields/%field_ui_menu/delete"] = array(
  154. 'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
  155. 'title' => 'Delete',
  156. 'page callback' => 'drupal_get_form',
  157. 'page arguments' => array('field_ui_field_delete_form', $field_position),
  158. 'type' => MENU_LOCAL_TASK,
  159. 'weight' => 10,
  160. 'file' => 'field_ui.admin.inc',
  161. ) + $access;
  162. // 'Manage display' tab.
  163. $items["$path/display"] = array(
  164. 'title' => 'Manage display',
  165. 'page callback' => 'drupal_get_form',
  166. 'page arguments' => array('field_ui_display_overview_form', $entity_type, $bundle_arg, 'default'),
  167. 'type' => MENU_LOCAL_TASK,
  168. 'weight' => 2,
  169. 'file' => 'field_ui.admin.inc',
  170. ) + $access;
  171. // View modes secondary tabs.
  172. // The same base $path for the menu item (with a placeholder) can be
  173. // used for all bundles of a given entity type; but depending on
  174. // administrator settings, each bundle has a different set of view
  175. // modes available for customisation. So we define menu items for all
  176. // view modes, and use an access callback to determine which ones are
  177. // actually visible for a given bundle.
  178. $weight = 0;
  179. $view_modes = array('default' => array('label' => t('Default'))) + $entity_info['view modes'];
  180. foreach ($view_modes as $view_mode => $view_mode_info) {
  181. $items["$path/display/$view_mode"] = array(
  182. 'title' => $view_mode_info['label'],
  183. 'page arguments' => array('field_ui_display_overview_form', $entity_type, $bundle_arg, $view_mode),
  184. // The access callback needs to check both the current 'custom
  185. // display' setting for the view mode, and the overall access
  186. // rules for the bundle admin pages.
  187. 'access callback' => '_field_ui_view_mode_menu_access',
  188. 'access arguments' => array_merge(array($entity_type, $bundle_arg, $view_mode, $access['access callback']), $access['access arguments']),
  189. 'type' => ($view_mode == 'default' ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK),
  190. 'weight' => ($view_mode == 'default' ? -10 : $weight++),
  191. 'file' => 'field_ui.admin.inc',
  192. );
  193. }
  194. }
  195. }
  196. }
  197. }
  198. return $items;
  199. }
  200. /**
  201. * Menu loader; Load a field instance based on field and bundle name.
  202. *
  203. * @param $field_name
  204. * The name of the field, as contained in the path.
  205. * @param $entity_type
  206. * The name of the entity.
  207. * @param $bundle_name
  208. * The name of the bundle, as contained in the path.
  209. * @param $bundle_pos
  210. * The position of $bundle_name in $map.
  211. * @param $map
  212. * The translated menu router path argument map.
  213. *
  214. * @return
  215. * The field instance array.
  216. *
  217. * @ingroup field
  218. */
  219. function field_ui_menu_load($field_name, $entity_type, $bundle_name, $bundle_pos, $map) {
  220. // Extract the actual bundle name from the translated argument map.
  221. // The menu router path to manage fields of an entity can be shared among
  222. // multiple bundles. For example:
  223. // - admin/structure/types/manage/%node_type/fields/%field_ui_menu
  224. // - admin/structure/types/manage/%comment_node_type/fields/%field_ui_menu
  225. // The menu system will automatically load the correct bundle depending on the
  226. // actual path arguments, but this menu loader function only receives the node
  227. // type string as $bundle_name, which is not the bundle name for comments.
  228. // We therefore leverage the dynamically translated $map provided by the menu
  229. // system to retrieve the actual bundle and bundle name for the current path.
  230. if ($bundle_pos > 0) {
  231. $bundle = $map[$bundle_pos];
  232. $bundle_name = field_extract_bundle($entity_type, $bundle);
  233. }
  234. // Check whether the field exists at all.
  235. if ($field = field_info_field($field_name)) {
  236. // Only return the field if a field instance exists for the given entity
  237. // type and bundle.
  238. if ($instance = field_info_instance($entity_type, $field_name, $bundle_name)) {
  239. return $instance;
  240. }
  241. }
  242. return FALSE;
  243. }
  244. /**
  245. * Menu title callback.
  246. */
  247. function field_ui_menu_title($instance) {
  248. return $instance['label'];
  249. }
  250. /**
  251. * Menu access callback for the 'view mode display settings' pages.
  252. */
  253. function _field_ui_view_mode_menu_access($entity_type, $bundle, $view_mode, $access_callback) {
  254. // It's good practice to call func_get_args() at the beginning of a function
  255. // to avoid problems with function parameters being modified later. The
  256. // behavior of func_get_args() changed in PHP7.
  257. // @see https://www.php.net/manual/en/migration70.incompatible.php#migration70.incompatible.other.func-parameter-modified
  258. $all_args = func_get_args();
  259. // First, determine visibility according to the 'use custom display'
  260. // setting for the view mode.
  261. $bundle = field_extract_bundle($entity_type, $bundle);
  262. $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
  263. $visibility = ($view_mode == 'default') || !empty($view_mode_settings[$view_mode]['custom_settings']);
  264. // Then, determine access according to the $access parameter. This duplicates
  265. // part of _menu_check_access().
  266. if ($visibility) {
  267. // Grab the variable 'access arguments' part.
  268. $args = array_slice($all_args, 4);
  269. $callback = empty($access_callback) ? 0 : trim($access_callback);
  270. if (is_numeric($callback)) {
  271. return (bool) $callback;
  272. }
  273. else {
  274. // As call_user_func_array() is quite slow and user_access is a very
  275. // common callback, it is worth making a special case for it.
  276. if ($access_callback == 'user_access') {
  277. return (count($args) == 1) ? user_access($args[0]) : user_access($args[0], $args[1]);
  278. }
  279. elseif (function_exists($access_callback)) {
  280. return call_user_func_array($access_callback, $args);
  281. }
  282. }
  283. }
  284. }
  285. /**
  286. * Implements hook_theme().
  287. */
  288. function field_ui_theme() {
  289. return array(
  290. 'field_ui_table' => array(
  291. 'render element' => 'elements',
  292. ),
  293. );
  294. }
  295. /**
  296. * Implements hook_element_info().
  297. */
  298. function field_ui_element_info() {
  299. return array(
  300. 'field_ui_table' => array(
  301. '#theme' => 'field_ui_table',
  302. '#pre_render' => array('field_ui_table_pre_render'),
  303. '#regions' => array('' => array()),
  304. ),
  305. );
  306. }
  307. /**
  308. * Implements hook_field_attach_create_bundle().
  309. */
  310. function field_ui_field_attach_create_bundle($entity_type, $bundle) {
  311. // When a new bundle is created, the menu needs to be rebuilt to add our
  312. // menu item tabs.
  313. variable_set('menu_rebuild_needed', TRUE);
  314. }
  315. /**
  316. * Determines the administration path for a bundle.
  317. */
  318. function _field_ui_bundle_admin_path($entity_type, $bundle_name) {
  319. $bundles = field_info_bundles($entity_type);
  320. $bundle_info = $bundles[$bundle_name];
  321. if (isset($bundle_info['admin'])) {
  322. return isset($bundle_info['admin']['real path']) ? $bundle_info['admin']['real path'] : $bundle_info['admin']['path'];
  323. }
  324. }
  325. /**
  326. * Identifies inactive fields within a bundle.
  327. */
  328. function field_ui_inactive_instances($entity_type, $bundle_name = NULL) {
  329. $params = array('entity_type' => $entity_type);
  330. if (empty($bundle_name)) {
  331. $active = field_info_instances($entity_type);
  332. $inactive = array();
  333. }
  334. else {
  335. // Restrict to the specified bundle. For consistency with the case where
  336. // $bundle_name is NULL, the $active and $inactive arrays are keyed by
  337. // bundle name first.
  338. $params['bundle'] = $bundle_name;
  339. $active = array($bundle_name => field_info_instances($entity_type, $bundle_name));
  340. $inactive = array($bundle_name => array());
  341. }
  342. // Iterate on existing definitions, and spot those that do not appear in the
  343. // $active list collected earlier.
  344. $all_instances = field_read_instances($params, array('include_inactive' => TRUE));
  345. foreach ($all_instances as $instance) {
  346. if (!isset($active[$instance['bundle']][$instance['field_name']])) {
  347. $inactive[$instance['bundle']][$instance['field_name']] = $instance;
  348. }
  349. }
  350. if (!empty($bundle_name)) {
  351. return $inactive[$bundle_name];
  352. }
  353. return $inactive;
  354. }
  355. /**
  356. * Implements hook_form_FORM_ID_alter().
  357. *
  358. * Adds a button 'Save and add fields' to the 'Create content type' form.
  359. *
  360. * @see node_type_form()
  361. * @see field_ui_form_node_type_form_submit()
  362. */
  363. function field_ui_form_node_type_form_alter(&$form, $form_state) {
  364. // We want to display the button only on add page.
  365. if (empty($form['#node_type']->type)) {
  366. $form['actions']['save_continue'] = array(
  367. '#type' => 'submit',
  368. '#value' => t('Save and add fields'),
  369. '#weight' => 45,
  370. );
  371. $form['#submit'][] = 'field_ui_form_node_type_form_submit';
  372. }
  373. }
  374. /**
  375. * Form submission handler for the 'Save and add fields' button.
  376. *
  377. * @see field_ui_form_node_type_form_alter()
  378. */
  379. function field_ui_form_node_type_form_submit($form, &$form_state) {
  380. if ($form_state['triggering_element']['#parents'][0] === 'save_continue') {
  381. $form_state['redirect'] = _field_ui_bundle_admin_path('node', $form_state['values']['type']) .'/fields';
  382. }
  383. }
  384. /**
  385. * Access callback to determine if a user is allowed to use the field UI.
  386. *
  387. * Only grant access if the user has both the "administer fields" permission and
  388. * is granted access by the entity specific restrictions.
  389. */
  390. function field_ui_admin_access($access_callback, $access_arguments) {
  391. return user_access('administer fields') && call_user_func_array($access_callback, $access_arguments);
  392. }