field_ui.admin.inc 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  1. <?php
  2. /**
  3. * @file
  4. * Administrative interface for custom field type creation.
  5. */
  6. /**
  7. * Menu callback; lists all defined fields for quick reference.
  8. */
  9. function field_ui_fields_list() {
  10. $instances = field_info_instances();
  11. $field_types = field_info_field_types();
  12. $bundles = field_info_bundles();
  13. $modules = system_rebuild_module_data();
  14. $header = array(t('Field name'), t('Field type'), t('Used in'));
  15. $rows = array();
  16. foreach ($instances as $entity_type => $type_bundles) {
  17. foreach ($type_bundles as $bundle => $bundle_instances) {
  18. foreach ($bundle_instances as $field_name => $instance) {
  19. $field = field_info_field($field_name);
  20. // Initialize the row if we encounter the field for the first time.
  21. if (!isset($rows[$field_name])) {
  22. $rows[$field_name]['class'] = $field['locked'] ? array('menu-disabled') : array('');
  23. $rows[$field_name]['data'][0] = $field['locked'] ? t('@field_name (Locked)', array('@field_name' => $field_name)) : $field_name;
  24. $module_name = $field_types[$field['type']]['module'];
  25. $rows[$field_name]['data'][1] = $field_types[$field['type']]['label'] . ' ' . t('(module: !module)', array('!module' => $modules[$module_name]->info['name']));
  26. }
  27. // Add the current instance.
  28. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  29. $rows[$field_name]['data'][2][] = $admin_path ? l($bundles[$entity_type][$bundle]['label'], $admin_path . '/fields') : $bundles[$entity_type][$bundle]['label'];
  30. }
  31. }
  32. }
  33. foreach ($rows as $field_name => $cell) {
  34. $rows[$field_name]['data'][2] = implode(', ', $cell['data'][2]);
  35. }
  36. if (empty($rows)) {
  37. $output = t('No fields have been defined yet.');
  38. }
  39. else {
  40. // Sort rows by field name.
  41. ksort($rows);
  42. $output = theme('table', array('header' => $header, 'rows' => $rows));
  43. }
  44. return $output;
  45. }
  46. /**
  47. * Displays a message listing the inactive fields of a given bundle.
  48. */
  49. function field_ui_inactive_message($entity_type, $bundle) {
  50. $inactive_instances = field_ui_inactive_instances($entity_type, $bundle);
  51. if (!empty($inactive_instances)) {
  52. $field_types = field_info_field_types();
  53. $widget_types = field_info_widget_types();
  54. foreach ($inactive_instances as $field_name => $instance) {
  55. $list[] = t('%field (@field_name) field requires the %widget_type widget provided by %widget_module module', array(
  56. '%field' => $instance['label'],
  57. '@field_name' => $instance['field_name'],
  58. '%widget_type' => isset($widget_types[$instance['widget']['type']]) ? $widget_types[$instance['widget']['type']]['label'] : $instance['widget']['type'],
  59. '%widget_module' => $instance['widget']['module'],
  60. ));
  61. }
  62. drupal_set_message(t('Inactive fields are not shown unless their providing modules are enabled. The following fields are not enabled: !list', array('!list' => theme('item_list', array('items' => $list)))), 'error');
  63. }
  64. }
  65. /**
  66. * Determines the rendering order of an array representing a tree.
  67. *
  68. * Callback for array_reduce() within field_ui_table_pre_render().
  69. */
  70. function _field_ui_reduce_order($array, $a) {
  71. $array = !isset($array) ? array() : $array;
  72. if ($a['name']) {
  73. $array[] = $a['name'];
  74. }
  75. if (!empty($a['children'])) {
  76. uasort($a['children'], 'drupal_sort_weight');
  77. $array = array_merge($array, array_reduce($a['children'], '_field_ui_reduce_order'));
  78. }
  79. return $array;
  80. }
  81. /**
  82. * Returns the region to which a row in the 'Manage fields' screen belongs.
  83. *
  84. * This function is used as a #region_callback in
  85. * field_ui_field_overview_form(). It is called during
  86. * field_ui_table_pre_render().
  87. */
  88. function field_ui_field_overview_row_region($row) {
  89. switch ($row['#row_type']) {
  90. case 'field':
  91. case 'extra_field':
  92. return 'main';
  93. case 'add_new_field':
  94. // If no input in 'label', assume the row has not been dragged out of the
  95. // 'add new' section.
  96. return (!empty($row['label']['#value']) ? 'main' : 'add_new');
  97. }
  98. }
  99. /**
  100. * Returns the region to which a row in the 'Manage display' screen belongs.
  101. *
  102. * This function is used as a #region_callback in
  103. * field_ui_field_overview_form(), and is called during
  104. * field_ui_table_pre_render().
  105. */
  106. function field_ui_display_overview_row_region($row) {
  107. switch ($row['#row_type']) {
  108. case 'field':
  109. case 'extra_field':
  110. return ($row['format']['type']['#value'] == 'hidden' ? 'hidden' : 'visible');
  111. }
  112. }
  113. /**
  114. * Pre-render callback for field_ui_table elements.
  115. */
  116. function field_ui_table_pre_render($elements) {
  117. $js_settings = array();
  118. // For each region, build the tree structure from the weight and parenting
  119. // data contained in the flat form structure, to determine row order and
  120. // indentation.
  121. $regions = $elements['#regions'];
  122. $tree = array('' => array('name' => '', 'children' => array()));
  123. $trees = array_fill_keys(array_keys($regions), $tree);
  124. $parents = array();
  125. $list = drupal_map_assoc(element_children($elements));
  126. // Iterate on rows until we can build a known tree path for all of them.
  127. while ($list) {
  128. foreach ($list as $name) {
  129. $row = &$elements[$name];
  130. $parent = $row['parent_wrapper']['parent']['#value'];
  131. // Proceed if parent is known.
  132. if (empty($parent) || isset($parents[$parent])) {
  133. // Grab parent, and remove the row from the next iteration.
  134. $parents[$name] = $parent ? array_merge($parents[$parent], array($parent)) : array();
  135. unset($list[$name]);
  136. // Determine the region for the row.
  137. $function = $row['#region_callback'];
  138. $region_name = $function($row);
  139. // Add the element in the tree.
  140. $target = &$trees[$region_name][''];
  141. foreach ($parents[$name] as $key) {
  142. $target = &$target['children'][$key];
  143. }
  144. $target['children'][$name] = array('name' => $name, 'weight' => $row['weight']['#value']);
  145. // Add tabledrag indentation to the first row cell.
  146. if ($depth = count($parents[$name])) {
  147. $children = element_children($row);
  148. $cell = current($children);
  149. $row[$cell]['#prefix'] = theme('indentation', array('size' => $depth)) . (isset($row[$cell]['#prefix']) ? $row[$cell]['#prefix'] : '');
  150. }
  151. // Add row id and associate JS settings.
  152. $id = drupal_html_class($name);
  153. $row['#attributes']['id'] = $id;
  154. if (isset($row['#js_settings'])) {
  155. $row['#js_settings'] += array(
  156. 'rowHandler' => $row['#row_type'],
  157. 'name' => $name,
  158. 'region' => $region_name,
  159. );
  160. $js_settings[$id] = $row['#js_settings'];
  161. }
  162. }
  163. }
  164. }
  165. // Determine rendering order from the tree structure.
  166. foreach ($regions as $region_name => $region) {
  167. $elements['#regions'][$region_name]['rows_order'] = array_reduce($trees[$region_name], '_field_ui_reduce_order');
  168. }
  169. $elements['#attached']['js'][] = array(
  170. 'type' => 'setting',
  171. 'data' => array('fieldUIRowsData' => $js_settings),
  172. );
  173. return $elements;
  174. }
  175. /**
  176. * Returns HTML for Field UI overview tables.
  177. *
  178. * @param $variables
  179. * An associative array containing:
  180. * - elements: An associative array containing a Form API structure to be
  181. * rendered as a table.
  182. *
  183. * @ingroup themeable
  184. */
  185. function theme_field_ui_table($variables) {
  186. $elements = $variables['elements'];
  187. $table = array();
  188. $js_settings = array();
  189. // Add table headers and attributes.
  190. foreach (array('header', 'attributes') as $key) {
  191. if (isset($elements["#$key"])) {
  192. $table[$key] = $elements["#$key"];
  193. }
  194. }
  195. // Determine the colspan to use for region rows, by checking the number of
  196. // columns in the headers.
  197. $columns_count = 0;
  198. foreach ($table['header'] as $header) {
  199. $columns_count += (is_array($header) && isset($header['colspan']) ? $header['colspan'] : 1);
  200. }
  201. // Render rows, region by region.
  202. foreach ($elements['#regions'] as $region_name => $region) {
  203. $region_name_class = drupal_html_class($region_name);
  204. // Add region rows.
  205. if (isset($region['title'])) {
  206. $table['rows'][] = array(
  207. 'class' => array('region-title', 'region-' . $region_name_class . '-title'),
  208. 'no_striping' => TRUE,
  209. 'data' => array(
  210. array('data' => $region['title'], 'colspan' => $columns_count),
  211. ),
  212. );
  213. }
  214. if (isset($region['message'])) {
  215. $class = (empty($region['rows_order']) ? 'region-empty' : 'region-populated');
  216. $table['rows'][] = array(
  217. 'class' => array('region-message', 'region-' . $region_name_class . '-message', $class),
  218. 'no_striping' => TRUE,
  219. 'data' => array(
  220. array('data' => $region['message'], 'colspan' => $columns_count),
  221. ),
  222. );
  223. }
  224. // Add form rows, in the order determined at pre-render time.
  225. foreach ($region['rows_order'] as $name) {
  226. $element = $elements[$name];
  227. $row = array('data' => array());
  228. if (isset($element['#attributes'])) {
  229. $row += $element['#attributes'];
  230. }
  231. // Render children as table cells.
  232. foreach (element_children($element) as $cell_key) {
  233. $child = &$element[$cell_key];
  234. // Do not render a cell for children of #type 'value'.
  235. if (!(isset($child['#type']) && $child['#type'] == 'value')) {
  236. $cell = array('data' => drupal_render($child));
  237. if (isset($child['#cell_attributes'])) {
  238. $cell += $child['#cell_attributes'];
  239. }
  240. $row['data'][] = $cell;
  241. }
  242. }
  243. $table['rows'][] = $row;
  244. }
  245. }
  246. return theme('table', $table);
  247. }
  248. /**
  249. * Form constructor for the 'Manage fields' form of a bundle.
  250. *
  251. * Allows fields and pseudo-fields to be re-ordered.
  252. *
  253. * @see field_ui_field_overview_form_validate()
  254. * @see field_ui_field_overview_form_submit()
  255. * @ingroup forms
  256. */
  257. function field_ui_field_overview_form($form, &$form_state, $entity_type, $bundle) {
  258. $bundle = field_extract_bundle($entity_type, $bundle);
  259. field_ui_inactive_message($entity_type, $bundle);
  260. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  261. // When displaying the form, make sure the list of fields is up-to-date.
  262. if (empty($form_state['post'])) {
  263. field_info_cache_clear();
  264. }
  265. // Gather bundle information.
  266. $instances = field_info_instances($entity_type, $bundle);
  267. $field_types = field_info_field_types();
  268. $widget_types = field_info_widget_types();
  269. $extra_fields = field_info_extra_fields($entity_type, $bundle, 'form');
  270. $form += array(
  271. '#entity_type' => $entity_type,
  272. '#bundle' => $bundle,
  273. '#fields' => array_keys($instances),
  274. '#extra' => array_keys($extra_fields),
  275. );
  276. $table = array(
  277. '#type' => 'field_ui_table',
  278. '#tree' => TRUE,
  279. '#header' => array(
  280. t('Label'),
  281. t('Weight'),
  282. t('Parent'),
  283. t('Machine name'),
  284. t('Field type'),
  285. t('Widget'),
  286. array('data' => t('Operations'), 'colspan' => 2),
  287. ),
  288. '#parent_options' => array(),
  289. '#regions' => array(
  290. 'main' => array('message' => t('No fields are present yet.')),
  291. 'add_new' => array('title' => '&nbsp;'),
  292. ),
  293. '#attributes' => array(
  294. 'class' => array('field-ui-overview'),
  295. 'id' => 'field-overview',
  296. ),
  297. );
  298. // Fields.
  299. foreach ($instances as $name => $instance) {
  300. $field = field_info_field($instance['field_name']);
  301. $admin_field_path = $admin_path . '/fields/' . $instance['field_name'];
  302. $table[$name] = array(
  303. '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
  304. '#row_type' => 'field',
  305. '#region_callback' => 'field_ui_field_overview_row_region',
  306. 'label' => array(
  307. '#markup' => check_plain($instance['label']),
  308. ),
  309. 'weight' => array(
  310. '#type' => 'textfield',
  311. '#title' => t('Weight for @title', array('@title' => $instance['label'])),
  312. '#title_display' => 'invisible',
  313. '#default_value' => $instance['widget']['weight'],
  314. '#size' => 3,
  315. '#attributes' => array('class' => array('field-weight')),
  316. ),
  317. 'parent_wrapper' => array(
  318. 'parent' => array(
  319. '#type' => 'select',
  320. '#title' => t('Parent for @title', array('@title' => $instance['label'])),
  321. '#title_display' => 'invisible',
  322. '#options' => $table['#parent_options'],
  323. '#empty_value' => '',
  324. '#attributes' => array('class' => array('field-parent')),
  325. '#parents' => array('fields', $name, 'parent'),
  326. ),
  327. 'hidden_name' => array(
  328. '#type' => 'hidden',
  329. '#default_value' => $name,
  330. '#attributes' => array('class' => array('field-name')),
  331. ),
  332. ),
  333. 'field_name' => array(
  334. '#markup' => $instance['field_name'],
  335. ),
  336. 'type' => array(
  337. '#type' => 'link',
  338. '#title' => t($field_types[$field['type']]['label']),
  339. '#href' => $admin_field_path . '/field-settings',
  340. '#options' => array('attributes' => array('title' => t('Edit field settings.'))),
  341. ),
  342. 'widget_type' => array(
  343. '#type' => 'link',
  344. '#title' => t($widget_types[$instance['widget']['type']]['label']),
  345. '#href' => $admin_field_path . '/widget-type',
  346. '#options' => array('attributes' => array('title' => t('Change widget type.'))),
  347. ),
  348. 'edit' => array(
  349. '#type' => 'link',
  350. '#title' => t('edit'),
  351. '#href' => $admin_field_path,
  352. '#options' => array('attributes' => array('title' => t('Edit instance settings.'))),
  353. ),
  354. 'delete' => array(
  355. '#type' => 'link',
  356. '#title' => t('delete'),
  357. '#href' => $admin_field_path . '/delete',
  358. '#options' => array('attributes' => array('title' => t('Delete instance.'))),
  359. ),
  360. );
  361. if (!empty($instance['locked'])) {
  362. $table[$name]['edit'] = array('#value' => t('Locked'));
  363. $table[$name]['delete'] = array();
  364. $table[$name]['#attributes']['class'][] = 'menu-disabled';
  365. }
  366. }
  367. // Non-field elements.
  368. foreach ($extra_fields as $name => $extra_field) {
  369. $table[$name] = array(
  370. '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
  371. '#row_type' => 'extra_field',
  372. '#region_callback' => 'field_ui_field_overview_row_region',
  373. 'label' => array(
  374. '#markup' => check_plain($extra_field['label']),
  375. ),
  376. 'weight' => array(
  377. '#type' => 'textfield',
  378. '#default_value' => $extra_field['weight'],
  379. '#size' => 3,
  380. '#attributes' => array('class' => array('field-weight')),
  381. '#title_display' => 'invisible',
  382. '#title' => t('Weight for @title', array('@title' => $extra_field['label'])),
  383. ),
  384. 'parent_wrapper' => array(
  385. 'parent' => array(
  386. '#type' => 'select',
  387. '#title' => t('Parent for @title', array('@title' => $extra_field['label'])),
  388. '#title_display' => 'invisible',
  389. '#options' => $table['#parent_options'],
  390. '#empty_value' => '',
  391. '#attributes' => array('class' => array('field-parent')),
  392. '#parents' => array('fields', $name, 'parent'),
  393. ),
  394. 'hidden_name' => array(
  395. '#type' => 'hidden',
  396. '#default_value' => $name,
  397. '#attributes' => array('class' => array('field-name')),
  398. ),
  399. ),
  400. 'field_name' => array(
  401. '#markup' => $name,
  402. ),
  403. 'type' => array(
  404. '#markup' => isset($extra_field['description']) ? $extra_field['description'] : '',
  405. '#cell_attributes' => array('colspan' => 2),
  406. ),
  407. 'edit' => array(
  408. '#markup' => isset($extra_field['edit']) ? $extra_field['edit'] : '',
  409. ),
  410. 'delete' => array(
  411. '#markup' => isset($extra_field['delete']) ? $extra_field['delete'] : '',
  412. ),
  413. );
  414. }
  415. // Additional row: add new field.
  416. $max_weight = field_info_max_weight($entity_type, $bundle, 'form');
  417. $field_type_options = field_ui_field_type_options();
  418. $widget_type_options = field_ui_widget_type_options(NULL, TRUE);
  419. if ($field_type_options && $widget_type_options) {
  420. $name = '_add_new_field';
  421. $table[$name] = array(
  422. '#attributes' => array('class' => array('draggable', 'tabledrag-leaf', 'add-new')),
  423. '#row_type' => 'add_new_field',
  424. '#region_callback' => 'field_ui_field_overview_row_region',
  425. 'label' => array(
  426. '#type' => 'textfield',
  427. '#title' => t('New field label'),
  428. '#title_display' => 'invisible',
  429. '#size' => 15,
  430. '#description' => t('Label'),
  431. '#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . t('Add new field') .'</div>',
  432. '#suffix' => '</div>',
  433. ),
  434. 'weight' => array(
  435. '#type' => 'textfield',
  436. '#default_value' => $max_weight + 1,
  437. '#size' => 3,
  438. '#title_display' => 'invisible',
  439. '#title' => t('Weight for new field'),
  440. '#attributes' => array('class' => array('field-weight')),
  441. '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
  442. ),
  443. 'parent_wrapper' => array(
  444. 'parent' => array(
  445. '#type' => 'select',
  446. '#title' => t('Parent for new field'),
  447. '#title_display' => 'invisible',
  448. '#options' => $table['#parent_options'],
  449. '#empty_value' => '',
  450. '#attributes' => array('class' => array('field-parent')),
  451. '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
  452. '#parents' => array('fields', $name, 'parent'),
  453. ),
  454. 'hidden_name' => array(
  455. '#type' => 'hidden',
  456. '#default_value' => $name,
  457. '#attributes' => array('class' => array('field-name')),
  458. ),
  459. ),
  460. 'field_name' => array(
  461. '#type' => 'machine_name',
  462. '#title' => t('New field name'),
  463. '#title_display' => 'invisible',
  464. // This field should stay LTR even for RTL languages.
  465. '#field_prefix' => '<span dir="ltr">field_',
  466. '#field_suffix' => '</span>&lrm;',
  467. '#size' => 15,
  468. '#description' => t('A unique machine-readable name containing letters, numbers, and underscores.'),
  469. // 32 characters minus the 'field_' prefix.
  470. '#maxlength' => 26,
  471. '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
  472. '#machine_name' => array(
  473. 'source' => array('fields', $name, 'label'),
  474. 'exists' => '_field_ui_field_name_exists',
  475. 'standalone' => TRUE,
  476. 'label' => '',
  477. ),
  478. '#required' => FALSE,
  479. ),
  480. 'type' => array(
  481. '#type' => 'select',
  482. '#title' => t('Type of new field'),
  483. '#title_display' => 'invisible',
  484. '#options' => $field_type_options,
  485. '#empty_option' => t('- Select a field type -'),
  486. '#description' => t('Type of data to store.'),
  487. '#attributes' => array('class' => array('field-type-select')),
  488. '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
  489. ),
  490. 'widget_type' => array(
  491. '#type' => 'select',
  492. '#title' => t('Widget for new field'),
  493. '#title_display' => 'invisible',
  494. '#options' => $widget_type_options,
  495. '#empty_option' => t('- Select a widget -'),
  496. '#description' => t('Form element to edit the data.'),
  497. '#attributes' => array('class' => array('widget-type-select')),
  498. '#cell_attributes' => array('colspan' => 3),
  499. '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
  500. ),
  501. // Place the 'translatable' property as an explicit value so that contrib
  502. // modules can form_alter() the value for newly created fields.
  503. 'translatable' => array(
  504. '#type' => 'value',
  505. '#value' => FALSE,
  506. ),
  507. );
  508. }
  509. // Additional row: add existing field.
  510. $existing_fields = field_ui_existing_field_options($entity_type, $bundle);
  511. if ($existing_fields && $widget_type_options) {
  512. // Build list of options.
  513. $existing_field_options = array();
  514. foreach ($existing_fields as $field_name => $info) {
  515. $text = t('@type: @field (@label)', array(
  516. '@type' => $info['type_label'],
  517. '@label' => $info['label'],
  518. '@field' => $info['field'],
  519. ));
  520. $existing_field_options[$field_name] = truncate_utf8($text, 80, FALSE, TRUE);
  521. }
  522. asort($existing_field_options);
  523. $name = '_add_existing_field';
  524. $table[$name] = array(
  525. '#attributes' => array('class' => array('draggable', 'tabledrag-leaf', 'add-new')),
  526. '#row_type' => 'add_new_field',
  527. '#region_callback' => 'field_ui_field_overview_row_region',
  528. 'label' => array(
  529. '#type' => 'textfield',
  530. '#title' => t('Existing field label'),
  531. '#title_display' => 'invisible',
  532. '#size' => 15,
  533. '#description' => t('Label'),
  534. '#attributes' => array('class' => array('label-textfield')),
  535. '#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . t('Add existing field') .'</div>',
  536. '#suffix' => '</div>',
  537. ),
  538. 'weight' => array(
  539. '#type' => 'textfield',
  540. '#default_value' => $max_weight + 2,
  541. '#size' => 3,
  542. '#title_display' => 'invisible',
  543. '#title' => t('Weight for added field'),
  544. '#attributes' => array('class' => array('field-weight')),
  545. '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
  546. ),
  547. 'parent_wrapper' => array(
  548. 'parent' => array(
  549. '#type' => 'select',
  550. '#title' => t('Parent for existing field'),
  551. '#title_display' => 'invisible',
  552. '#options' => $table['#parent_options'],
  553. '#empty_value' => '',
  554. '#attributes' => array('class' => array('field-parent')),
  555. '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
  556. '#parents' => array('fields', $name, 'parent'),
  557. ),
  558. 'hidden_name' => array(
  559. '#type' => 'hidden',
  560. '#default_value' => $name,
  561. '#attributes' => array('class' => array('field-name')),
  562. ),
  563. ),
  564. 'field_name' => array(
  565. '#type' => 'select',
  566. '#title' => t('Existing field to share'),
  567. '#title_display' => 'invisible',
  568. '#options' => $existing_field_options,
  569. '#empty_option' => t('- Select an existing field -'),
  570. '#description' => t('Field to share'),
  571. '#attributes' => array('class' => array('field-select')),
  572. '#cell_attributes' => array('colspan' => 2),
  573. '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
  574. ),
  575. 'widget_type' => array(
  576. '#type' => 'select',
  577. '#title' => t('Widget for existing field'),
  578. '#title_display' => 'invisible',
  579. '#options' => $widget_type_options,
  580. '#empty_option' => t('- Select a widget -'),
  581. '#description' => t('Form element to edit the data.'),
  582. '#attributes' => array('class' => array('widget-type-select')),
  583. '#cell_attributes' => array('colspan' => 3),
  584. '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
  585. ),
  586. );
  587. }
  588. $form['fields'] = $table;
  589. $form['actions'] = array('#type' => 'actions');
  590. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  591. $form['#attached']['css'][] = drupal_get_path('module', 'field_ui') . '/field_ui.css';
  592. $form['#attached']['js'][] = drupal_get_path('module', 'field_ui') . '/field_ui.js';
  593. // Add settings for the update selects behavior.
  594. $js_fields = array();
  595. foreach ($existing_fields as $field_name => $info) {
  596. $js_fields[$field_name] = array('label' => $info['label'], 'type' => $info['type'], 'widget' => $info['widget_type']);
  597. }
  598. $form['#attached']['js'][] = array(
  599. 'type' => 'setting',
  600. 'data' => array('fields' => $js_fields, 'fieldWidgetTypes' => field_ui_widget_type_options()),
  601. );
  602. // Add tabledrag behavior.
  603. $form['#attached']['drupal_add_tabledrag'][] = array('field-overview', 'order', 'sibling', 'field-weight');
  604. $form['#attached']['drupal_add_tabledrag'][] = array('field-overview', 'match', 'parent', 'field-parent', 'field-parent', 'field-name');
  605. return $form;
  606. }
  607. /**
  608. * Form validation handler for field_ui_field_overview_form().
  609. *
  610. * @see field_ui_field_overview_form_submit()
  611. */
  612. function field_ui_field_overview_form_validate($form, &$form_state) {
  613. _field_ui_field_overview_form_validate_add_new($form, $form_state);
  614. _field_ui_field_overview_form_validate_add_existing($form, $form_state);
  615. }
  616. /**
  617. * Validates the 'add new field' row of field_ui_field_overview_form().
  618. *
  619. * @see field_ui_field_overview_form_validate()
  620. */
  621. function _field_ui_field_overview_form_validate_add_new($form, &$form_state) {
  622. $field = $form_state['values']['fields']['_add_new_field'];
  623. // Validate if any information was provided in the 'add new field' row.
  624. if (array_filter(array($field['label'], $field['field_name'], $field['type'], $field['widget_type']))) {
  625. // Missing label.
  626. if (!$field['label']) {
  627. form_set_error('fields][_add_new_field][label', t('Add new field: you need to provide a label.'));
  628. }
  629. // Missing field name.
  630. if (!$field['field_name']) {
  631. form_set_error('fields][_add_new_field][field_name', t('Add new field: you need to provide a field name.'));
  632. }
  633. // Field name validation.
  634. else {
  635. $field_name = $field['field_name'];
  636. // Add the 'field_' prefix.
  637. $field_name = 'field_' . $field_name;
  638. form_set_value($form['fields']['_add_new_field']['field_name'], $field_name, $form_state);
  639. }
  640. // Missing field type.
  641. if (!$field['type']) {
  642. form_set_error('fields][_add_new_field][type', t('Add new field: you need to select a field type.'));
  643. }
  644. // Missing widget type.
  645. if (!$field['widget_type']) {
  646. form_set_error('fields][_add_new_field][widget_type', t('Add new field: you need to select a widget.'));
  647. }
  648. // Wrong widget type.
  649. elseif ($field['type']) {
  650. $widget_types = field_ui_widget_type_options($field['type']);
  651. if (!isset($widget_types[$field['widget_type']])) {
  652. form_set_error('fields][_add_new_field][widget_type', t('Add new field: invalid widget.'));
  653. }
  654. }
  655. }
  656. }
  657. /**
  658. * Render API callback: Checks if a field machine name is taken.
  659. *
  660. * @param $value
  661. * The machine name, not prefixed with 'field_'.
  662. *
  663. * @return
  664. * Whether or not the field machine name is taken.
  665. */
  666. function _field_ui_field_name_exists($value) {
  667. // Prefix with 'field_'.
  668. $field_name = 'field_' . $value;
  669. // We need to check inactive fields as well, so we can't use
  670. // field_info_fields().
  671. return (bool) field_read_fields(array('field_name' => $field_name), array('include_inactive' => TRUE));
  672. }
  673. /**
  674. * Validates the 'add existing field' row of field_ui_field_overview_form().
  675. *
  676. * @see field_ui_field_overview_form_validate()
  677. */
  678. function _field_ui_field_overview_form_validate_add_existing($form, &$form_state) {
  679. // The form element might be absent if no existing fields can be added to
  680. // this bundle.
  681. if (isset($form_state['values']['fields']['_add_existing_field'])) {
  682. $field = $form_state['values']['fields']['_add_existing_field'];
  683. // Validate if any information was provided in the 'add existing field' row.
  684. if (array_filter(array($field['label'], $field['field_name'], $field['widget_type']))) {
  685. // Missing label.
  686. if (!$field['label']) {
  687. form_set_error('fields][_add_existing_field][label', t('Add existing field: you need to provide a label.'));
  688. }
  689. // Missing existing field name.
  690. if (!$field['field_name']) {
  691. form_set_error('fields][_add_existing_field][field_name', t('Add existing field: you need to select a field.'));
  692. }
  693. // Missing widget type.
  694. if (!$field['widget_type']) {
  695. form_set_error('fields][_add_existing_field][widget_type', t('Add existing field: you need to select a widget.'));
  696. }
  697. // Wrong widget type.
  698. elseif ($field['field_name'] && ($existing_field = field_info_field($field['field_name']))) {
  699. $widget_types = field_ui_widget_type_options($existing_field['type']);
  700. if (!isset($widget_types[$field['widget_type']])) {
  701. form_set_error('fields][_add_existing_field][widget_type', t('Add existing field: invalid widget.'));
  702. }
  703. }
  704. }
  705. }
  706. }
  707. /**
  708. * Form submission handler for field_ui_field_overview_form().
  709. *
  710. * @see field_ui_field_overview_form_validate()
  711. */
  712. function field_ui_field_overview_form_submit($form, &$form_state) {
  713. $form_values = $form_state['values']['fields'];
  714. $entity_type = $form['#entity_type'];
  715. $bundle = $form['#bundle'];
  716. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  717. $bundle_settings = field_bundle_settings($entity_type, $bundle);
  718. // Update field weights.
  719. foreach ($form_values as $key => $values) {
  720. if (in_array($key, $form['#fields'])) {
  721. $instance = field_read_instance($entity_type, $key, $bundle);
  722. $instance['widget']['weight'] = $values['weight'];
  723. field_update_instance($instance);
  724. }
  725. elseif (in_array($key, $form['#extra'])) {
  726. $bundle_settings['extra_fields']['form'][$key]['weight'] = $values['weight'];
  727. }
  728. }
  729. field_bundle_settings($entity_type, $bundle, $bundle_settings);
  730. $destinations = array();
  731. // Create new field.
  732. $field = array();
  733. if (!empty($form_values['_add_new_field']['field_name'])) {
  734. $values = $form_values['_add_new_field'];
  735. $field = array(
  736. 'field_name' => $values['field_name'],
  737. 'type' => $values['type'],
  738. 'translatable' => $values['translatable'],
  739. );
  740. $instance = array(
  741. 'field_name' => $field['field_name'],
  742. 'entity_type' => $entity_type,
  743. 'bundle' => $bundle,
  744. 'label' => $values['label'],
  745. 'widget' => array(
  746. 'type' => $values['widget_type'],
  747. 'weight' => $values['weight'],
  748. ),
  749. );
  750. // Create the field and instance.
  751. try {
  752. field_create_field($field);
  753. field_create_instance($instance);
  754. $destinations[] = $admin_path . '/fields/' . $field['field_name'] . '/field-settings';
  755. $destinations[] = $admin_path . '/fields/' . $field['field_name'];
  756. // Store new field information for any additional submit handlers.
  757. $form_state['fields_added']['_add_new_field'] = $field['field_name'];
  758. }
  759. catch (Exception $e) {
  760. drupal_set_message(t('There was a problem creating field %label: !message', array('%label' => $instance['label'], '!message' => $e->getMessage())), 'error');
  761. }
  762. }
  763. // Add existing field.
  764. if (!empty($form_values['_add_existing_field']['field_name'])) {
  765. $values = $form_values['_add_existing_field'];
  766. $field = field_info_field($values['field_name']);
  767. if (!empty($field['locked'])) {
  768. drupal_set_message(t('The field %label cannot be added because it is locked.', array('%label' => $values['label'])), 'error');
  769. }
  770. else {
  771. $instance = array(
  772. 'field_name' => $field['field_name'],
  773. 'entity_type' => $entity_type,
  774. 'bundle' => $bundle,
  775. 'label' => $values['label'],
  776. 'widget' => array(
  777. 'type' => $values['widget_type'],
  778. 'weight' => $values['weight'],
  779. ),
  780. );
  781. try {
  782. field_create_instance($instance);
  783. $destinations[] = $admin_path . '/fields/' . $instance['field_name'] . '/edit';
  784. // Store new field information for any additional submit handlers.
  785. $form_state['fields_added']['_add_existing_field'] = $instance['field_name'];
  786. }
  787. catch (Exception $e) {
  788. drupal_set_message(t('There was a problem creating field instance %label: @message.', array('%label' => $instance['label'], '@message' => $e->getMessage())), 'error');
  789. }
  790. }
  791. }
  792. if ($destinations) {
  793. $destination = drupal_get_destination();
  794. $destinations[] = $destination['destination'];
  795. unset($_GET['destination']);
  796. $form_state['redirect'] = field_ui_get_destinations($destinations);
  797. }
  798. else {
  799. drupal_set_message(t('Your settings have been saved.'));
  800. }
  801. }
  802. /**
  803. * Form constructor for the field display settings for a given view mode.
  804. *
  805. * @see field_ui_display_overview_multistep_submit()
  806. * @see field_ui_display_overview_form_submit()
  807. * @ingroup forms
  808. */
  809. function field_ui_display_overview_form($form, &$form_state, $entity_type, $bundle, $view_mode) {
  810. $bundle = field_extract_bundle($entity_type, $bundle);
  811. field_ui_inactive_message($entity_type, $bundle);
  812. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  813. // Gather type information.
  814. $instances = field_info_instances($entity_type, $bundle);
  815. $field_types = field_info_field_types();
  816. $extra_fields = field_info_extra_fields($entity_type, $bundle, 'display');
  817. $form_state += array(
  818. 'formatter_settings_edit' => NULL,
  819. );
  820. $form += array(
  821. '#entity_type' => $entity_type,
  822. '#bundle' => $bundle,
  823. '#view_mode' => $view_mode,
  824. '#fields' => array_keys($instances),
  825. '#extra' => array_keys($extra_fields),
  826. );
  827. if (empty($instances) && empty($extra_fields)) {
  828. drupal_set_message(t('There are no fields yet added. You can add new fields on the <a href="@link">Manage fields</a> page.', array('@link' => url($admin_path . '/fields'))), 'warning');
  829. return $form;
  830. }
  831. $table = array(
  832. '#type' => 'field_ui_table',
  833. '#tree' => TRUE,
  834. '#header' => array(
  835. t('Field'),
  836. t('Weight'),
  837. t('Parent'),
  838. t('Label'),
  839. array('data' => t('Format'), 'colspan' => 3),
  840. ),
  841. '#regions' => array(
  842. 'visible' => array('message' => t('No field is displayed.')),
  843. 'hidden' => array('title' => t('Hidden'), 'message' => t('No field is hidden.')),
  844. ),
  845. '#parent_options' => array(),
  846. '#attributes' => array(
  847. 'class' => array('field-ui-overview'),
  848. 'id' => 'field-display-overview',
  849. ),
  850. // Add Ajax wrapper.
  851. '#prefix' => '<div id="field-display-overview-wrapper">',
  852. '#suffix' => '</div>',
  853. );
  854. $field_label_options = array(
  855. 'above' => t('Above'),
  856. 'inline' => t('Inline'),
  857. 'hidden' => '<' . t('Hidden') . '>',
  858. );
  859. $extra_visibility_options = array(
  860. 'visible' => t('Visible'),
  861. 'hidden' => t('Hidden'),
  862. );
  863. // Field rows.
  864. foreach ($instances as $name => $instance) {
  865. $field = field_info_field($instance['field_name']);
  866. $display = $instance['display'][$view_mode];
  867. $table[$name] = array(
  868. '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
  869. '#row_type' => 'field',
  870. '#region_callback' => 'field_ui_display_overview_row_region',
  871. '#js_settings' => array(
  872. 'rowHandler' => 'field',
  873. 'defaultFormatter' => $field_types[$field['type']]['default_formatter'],
  874. ),
  875. 'human_name' => array(
  876. '#markup' => check_plain($instance['label']),
  877. ),
  878. 'weight' => array(
  879. '#type' => 'textfield',
  880. '#title' => t('Weight for @title', array('@title' => $instance['label'])),
  881. '#title_display' => 'invisible',
  882. '#default_value' => $display['weight'],
  883. '#size' => 3,
  884. '#attributes' => array('class' => array('field-weight')),
  885. ),
  886. 'parent_wrapper' => array(
  887. 'parent' => array(
  888. '#type' => 'select',
  889. '#title' => t('Label display for @title', array('@title' => $instance['label'])),
  890. '#title_display' => 'invisible',
  891. '#options' => $table['#parent_options'],
  892. '#empty_value' => '',
  893. '#attributes' => array('class' => array('field-parent')),
  894. '#parents' => array('fields', $name, 'parent'),
  895. ),
  896. 'hidden_name' => array(
  897. '#type' => 'hidden',
  898. '#default_value' => $name,
  899. '#attributes' => array('class' => array('field-name')),
  900. ),
  901. ),
  902. 'label' => array(
  903. '#type' => 'select',
  904. '#title' => t('Label display for @title', array('@title' => $instance['label'])),
  905. '#title_display' => 'invisible',
  906. '#options' => $field_label_options,
  907. '#default_value' => $display['label'],
  908. ),
  909. );
  910. $formatter_options = field_ui_formatter_options($field['type']);
  911. $formatter_options['hidden'] = '<' . t('Hidden') . '>';
  912. $table[$name]['format'] = array(
  913. 'type' => array(
  914. '#type' => 'select',
  915. '#title' => t('Formatter for @title', array('@title' => $instance['label'])),
  916. '#title_display' => 'invisible',
  917. '#options' => $formatter_options,
  918. '#default_value' => $display['type'],
  919. '#parents' => array('fields', $name, 'type'),
  920. '#attributes' => array('class' => array('field-formatter-type')),
  921. ),
  922. 'settings_edit_form' => array(),
  923. );
  924. // Formatter settings.
  925. // Check the currently selected formatter, and merge persisted values for
  926. // formatter settings.
  927. if (isset($form_state['values']['fields'][$name]['type'])) {
  928. $formatter_type = $form_state['values']['fields'][$name]['type'];
  929. }
  930. else {
  931. $formatter_type = $display['type'];
  932. }
  933. if (isset($form_state['formatter_settings'][$name])) {
  934. $settings = $form_state['formatter_settings'][$name];
  935. }
  936. else {
  937. $settings = $display['settings'];
  938. }
  939. $settings += field_info_formatter_settings($formatter_type);
  940. $instance['display'][$view_mode]['type'] = $formatter_type;
  941. $formatter = field_info_formatter_types($formatter_type);
  942. $instance['display'][$view_mode]['module'] = $formatter['module'];
  943. $instance['display'][$view_mode]['settings'] = $settings;
  944. // Base button element for the various formatter settings actions.
  945. $base_button = array(
  946. '#submit' => array('field_ui_display_overview_multistep_submit'),
  947. '#ajax' => array(
  948. 'callback' => 'field_ui_display_overview_multistep_js',
  949. 'wrapper' => 'field-display-overview-wrapper',
  950. 'effect' => 'fade',
  951. ),
  952. '#field_name' => $name,
  953. );
  954. if ($form_state['formatter_settings_edit'] == $name) {
  955. // We are currently editing this field's formatter settings. Display the
  956. // settings form and submit buttons.
  957. $table[$name]['format']['settings_edit_form'] = array();
  958. $settings_form = array();
  959. $function = $formatter['module'] . '_field_formatter_settings_form';
  960. if (function_exists($function)) {
  961. $settings_form = $function($field, $instance, $view_mode, $form, $form_state);
  962. }
  963. if ($settings_form) {
  964. $table[$name]['format']['#cell_attributes'] = array('colspan' => 3);
  965. $table[$name]['format']['settings_edit_form'] = array(
  966. '#type' => 'container',
  967. '#attributes' => array('class' => array('field-formatter-settings-edit-form')),
  968. '#parents' => array('fields', $name, 'settings_edit_form'),
  969. 'label' => array(
  970. '#markup' => t('Format settings:') . ' <span class="formatter-name">' . $formatter['label'] . '</span>',
  971. ),
  972. 'settings' => $settings_form,
  973. 'actions' => array(
  974. '#type' => 'actions',
  975. 'save_settings' => $base_button + array(
  976. '#type' => 'submit',
  977. '#name' => $name . '_formatter_settings_update',
  978. '#value' => t('Update'),
  979. '#op' => 'update',
  980. ),
  981. 'cancel_settings' => $base_button + array(
  982. '#type' => 'submit',
  983. '#name' => $name . '_formatter_settings_cancel',
  984. '#value' => t('Cancel'),
  985. '#op' => 'cancel',
  986. // Do not check errors for the 'Cancel' button, but make sure we
  987. // get the value of the 'formatter type' select.
  988. '#limit_validation_errors' => array(array('fields', $name, 'type')),
  989. ),
  990. ),
  991. );
  992. $table[$name]['#attributes']['class'][] = 'field-formatter-settings-editing';
  993. }
  994. }
  995. else {
  996. // Display a summary of the current formatter settings.
  997. $summary = module_invoke($formatter['module'], 'field_formatter_settings_summary', $field, $instance, $view_mode);
  998. $table[$name]['settings_summary'] = array();
  999. $table[$name]['settings_edit'] = array();
  1000. if ($summary) {
  1001. $table[$name]['settings_summary'] = array(
  1002. '#markup' => '<div class="field-formatter-summary">' . $summary . '</div>',
  1003. '#cell_attributes' => array('class' => array('field-formatter-summary-cell')),
  1004. );
  1005. $table[$name]['settings_edit'] = $base_button + array(
  1006. '#type' => 'image_button',
  1007. '#name' => $name . '_formatter_settings_edit',
  1008. '#src' => 'misc/configure.png',
  1009. '#attributes' => array('class' => array('field-formatter-settings-edit'), 'alt' => t('Edit')),
  1010. '#op' => 'edit',
  1011. // Do not check errors for the 'Edit' button, but make sure we get
  1012. // the value of the 'formatter type' select.
  1013. '#limit_validation_errors' => array(array('fields', $name, 'type')),
  1014. '#prefix' => '<div class="field-formatter-settings-edit-wrapper">',
  1015. '#suffix' => '</div>',
  1016. );
  1017. }
  1018. }
  1019. }
  1020. // Non-field elements.
  1021. foreach ($extra_fields as $name => $extra_field) {
  1022. $display = $extra_field['display'][$view_mode];
  1023. $table[$name] = array(
  1024. '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
  1025. '#row_type' => 'extra_field',
  1026. '#region_callback' => 'field_ui_display_overview_row_region',
  1027. '#js_settings' => array('rowHandler' => 'field'),
  1028. 'human_name' => array(
  1029. '#markup' => check_plain($extra_field['label']),
  1030. ),
  1031. 'weight' => array(
  1032. '#type' => 'textfield',
  1033. '#title' => t('Weight for @title', array('@title' => $extra_field['label'])),
  1034. '#title_display' => 'invisible',
  1035. '#default_value' => $display['weight'],
  1036. '#size' => 3,
  1037. '#attributes' => array('class' => array('field-weight')),
  1038. ),
  1039. 'parent_wrapper' => array(
  1040. 'parent' => array(
  1041. '#type' => 'select',
  1042. '#title' => t('Parents for @title', array('@title' => $extra_field['label'])),
  1043. '#title_display' => 'invisible',
  1044. '#options' => $table['#parent_options'],
  1045. '#empty_value' => '',
  1046. '#attributes' => array('class' => array('field-parent')),
  1047. '#parents' => array('fields', $name, 'parent'),
  1048. ),
  1049. 'hidden_name' => array(
  1050. '#type' => 'hidden',
  1051. '#default_value' => $name,
  1052. '#attributes' => array('class' => array('field-name')),
  1053. ),
  1054. ),
  1055. 'empty_cell' => array(
  1056. '#markup' => '&nbsp;',
  1057. ),
  1058. 'format' => array(
  1059. 'type' => array(
  1060. '#type' => 'select',
  1061. '#title' => t('Visibility for @title', array('@title' => $extra_field['label'])),
  1062. '#title_display' => 'invisible',
  1063. '#options' => $extra_visibility_options,
  1064. '#default_value' => $display['visible'] ? 'visible' : 'hidden',
  1065. '#parents' => array('fields', $name, 'type'),
  1066. '#attributes' => array('class' => array('field-formatter-type')),
  1067. ),
  1068. ),
  1069. 'settings_summary' => array(),
  1070. 'settings_edit' => array(),
  1071. );
  1072. }
  1073. $form['fields'] = $table;
  1074. // Custom display settings.
  1075. if ($view_mode == 'default') {
  1076. $form['modes'] = array(
  1077. '#type' => 'fieldset',
  1078. '#title' => t('Custom display settings'),
  1079. '#collapsible' => TRUE,
  1080. '#collapsed' => TRUE,
  1081. );
  1082. // Collect options and default values for the 'Custom display settings'
  1083. // checkboxes.
  1084. $options = array();
  1085. $default = array();
  1086. $entity_info = entity_get_info($entity_type);
  1087. $view_modes = $entity_info['view modes'];
  1088. $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
  1089. foreach ($view_modes as $view_mode_name => $view_mode_info) {
  1090. $options[$view_mode_name] = $view_mode_info['label'];
  1091. if (!empty($view_mode_settings[$view_mode_name]['custom_settings'])) {
  1092. $default[] = $view_mode_name;
  1093. }
  1094. }
  1095. $form['modes']['view_modes_custom'] = array(
  1096. '#type' => 'checkboxes',
  1097. '#title' => t('Use custom display settings for the following view modes'),
  1098. '#options' => $options,
  1099. '#default_value' => $default,
  1100. );
  1101. }
  1102. // In overviews involving nested rows from contributed modules (i.e
  1103. // field_group), the 'format type' selects can trigger a series of changes in
  1104. // child rows. The #ajax behavior is therefore not attached directly to the
  1105. // selects, but triggered by the client-side script through a hidden #ajax
  1106. // 'Refresh' button. A hidden 'refresh_rows' input tracks the name of
  1107. // affected rows.
  1108. $form['refresh_rows'] = array('#type' => 'hidden');
  1109. $form['refresh'] = array(
  1110. '#type' => 'submit',
  1111. '#value' => t('Refresh'),
  1112. '#op' => 'refresh_table',
  1113. '#submit' => array('field_ui_display_overview_multistep_submit'),
  1114. '#ajax' => array(
  1115. 'callback' => 'field_ui_display_overview_multistep_js',
  1116. 'wrapper' => 'field-display-overview-wrapper',
  1117. 'effect' => 'fade',
  1118. // The button stays hidden, so we hide the Ajax spinner too. Ad-hoc
  1119. // spinners will be added manually by the client-side script.
  1120. 'progress' => 'none',
  1121. ),
  1122. );
  1123. $form['actions'] = array('#type' => 'actions');
  1124. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  1125. $form['#attached']['js'][] = drupal_get_path('module', 'field_ui') . '/field_ui.js';
  1126. $form['#attached']['css'][] = drupal_get_path('module', 'field_ui') . '/field_ui.css';
  1127. // Add tabledrag behavior.
  1128. $form['#attached']['drupal_add_tabledrag'][] = array('field-display-overview', 'order', 'sibling', 'field-weight');
  1129. $form['#attached']['drupal_add_tabledrag'][] = array('field-display-overview', 'match', 'parent', 'field-parent', 'field-parent', 'field-name');
  1130. return $form;
  1131. }
  1132. /**
  1133. * Form submission handler for buttons in field_ui_display_overview_form().
  1134. */
  1135. function field_ui_display_overview_multistep_submit($form, &$form_state) {
  1136. $trigger = $form_state['triggering_element'];
  1137. $op = $trigger['#op'];
  1138. switch ($op) {
  1139. case 'edit':
  1140. // Store the field whose settings are currently being edited.
  1141. $field_name = $trigger['#field_name'];
  1142. $form_state['formatter_settings_edit'] = $field_name;
  1143. break;
  1144. case 'update':
  1145. // Store the saved settings, and set the field back to 'non edit' mode.
  1146. $field_name = $trigger['#field_name'];
  1147. $values = $form_state['values']['fields'][$field_name]['settings_edit_form']['settings'];
  1148. $form_state['formatter_settings'][$field_name] = $values;
  1149. unset($form_state['formatter_settings_edit']);
  1150. break;
  1151. case 'cancel':
  1152. // Set the field back to 'non edit' mode.
  1153. unset($form_state['formatter_settings_edit']);
  1154. break;
  1155. case 'refresh_table':
  1156. // If the currently edited field is one of the rows to be refreshed, set
  1157. // it back to 'non edit' mode.
  1158. $updated_rows = explode(' ', $form_state['values']['refresh_rows']);
  1159. if (isset($form_state['formatter_settings_edit']) && in_array($form_state['formatter_settings_edit'], $updated_rows)) {
  1160. unset($form_state['formatter_settings_edit']);
  1161. }
  1162. break;
  1163. }
  1164. $form_state['rebuild'] = TRUE;
  1165. }
  1166. /**
  1167. * Ajax handler for multistep buttons on the 'Manage display' screen.
  1168. */
  1169. function field_ui_display_overview_multistep_js($form, &$form_state) {
  1170. $trigger = $form_state['triggering_element'];
  1171. $op = $trigger['#op'];
  1172. // Pick the elements that need to receive the ajax-new-content effect.
  1173. switch ($op) {
  1174. case 'edit':
  1175. $updated_rows = array($trigger['#field_name']);
  1176. $updated_columns = array('format');
  1177. break;
  1178. case 'update':
  1179. case 'cancel':
  1180. $updated_rows = array($trigger['#field_name']);
  1181. $updated_columns = array('format', 'settings_summary', 'settings_edit');
  1182. break;
  1183. case 'refresh_table':
  1184. $updated_rows = array_values(explode(' ', $form_state['values']['refresh_rows']));
  1185. $updated_columns = array('settings_summary', 'settings_edit');
  1186. break;
  1187. }
  1188. foreach ($updated_rows as $name) {
  1189. foreach ($updated_columns as $key) {
  1190. $element = &$form['fields'][$name][$key];
  1191. $element['#prefix'] = '<div class="ajax-new-content">' . (isset($element['#prefix']) ? $element['#prefix'] : '');
  1192. $element['#suffix'] = (isset($element['#suffix']) ? $element['#suffix'] : '') . '</div>';
  1193. }
  1194. }
  1195. // Return the whole table.
  1196. return $form['fields'];
  1197. }
  1198. /**
  1199. * Form submission handler for field_ui_display_overview_form().
  1200. */
  1201. function field_ui_display_overview_form_submit($form, &$form_state) {
  1202. $form_values = $form_state['values'];
  1203. $entity_type = $form['#entity_type'];
  1204. $bundle = $form['#bundle'];
  1205. $view_mode = $form['#view_mode'];
  1206. // Save data for 'regular' fields.
  1207. foreach ($form['#fields'] as $field_name) {
  1208. // Retrieve the stored instance settings to merge with the incoming values.
  1209. $instance = field_read_instance($entity_type, $field_name, $bundle);
  1210. $values = $form_values['fields'][$field_name];
  1211. // Get formatter settings. They lie either directly in submitted form
  1212. // values (if the whole form was submitted while some formatter
  1213. // settings were being edited), or have been persisted in
  1214. // $form_state.
  1215. $settings = array();
  1216. if (isset($values['settings_edit_form']['settings'])) {
  1217. $settings = $values['settings_edit_form']['settings'];
  1218. }
  1219. elseif (isset($form_state['formatter_settings'][$field_name])) {
  1220. $settings = $form_state['formatter_settings'][$field_name];
  1221. }
  1222. elseif (isset($instance['display'][$view_mode]['settings'])) {
  1223. $settings = $instance['display'][$view_mode]['settings'];
  1224. }
  1225. // Only save settings actually used by the selected formatter.
  1226. $default_settings = field_info_formatter_settings($values['type']);
  1227. $settings = array_intersect_key($settings, $default_settings);
  1228. $instance['display'][$view_mode] = array(
  1229. 'label' => $values['label'],
  1230. 'type' => $values['type'],
  1231. 'weight' => $values['weight'],
  1232. 'settings' => $settings,
  1233. );
  1234. field_update_instance($instance);
  1235. }
  1236. // Get current bundle settings.
  1237. $bundle_settings = field_bundle_settings($entity_type, $bundle);
  1238. // Save data for 'extra' fields.
  1239. foreach ($form['#extra'] as $name) {
  1240. $bundle_settings['extra_fields']['display'][$name][$view_mode] = array(
  1241. 'weight' => $form_values['fields'][$name]['weight'],
  1242. 'visible' => $form_values['fields'][$name]['type'] == 'visible',
  1243. );
  1244. }
  1245. // Save view modes data.
  1246. if ($view_mode == 'default') {
  1247. $entity_info = entity_get_info($entity_type);
  1248. foreach ($form_values['view_modes_custom'] as $view_mode_name => $value) {
  1249. // Display a message for each view mode newly configured to use custom
  1250. // settings.
  1251. $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
  1252. if (!empty($value) && empty($view_mode_settings[$view_mode_name]['custom_settings'])) {
  1253. $view_mode_label = $entity_info['view modes'][$view_mode_name]['label'];
  1254. $path = _field_ui_bundle_admin_path($entity_type, $bundle) . "/display/$view_mode_name";
  1255. drupal_set_message(t('The %view_mode mode now uses custom display settings. You might want to <a href="@url">configure them</a>.', array('%view_mode' => $view_mode_label, '@url' => url($path))));
  1256. // Initialize the newly customized view mode with the display settings
  1257. // from the default view mode.
  1258. _field_ui_add_default_view_mode_settings($entity_type, $bundle, $view_mode_name, $bundle_settings);
  1259. }
  1260. $bundle_settings['view_modes'][$view_mode_name]['custom_settings'] = !empty($value);
  1261. }
  1262. }
  1263. // Save updated bundle settings.
  1264. field_bundle_settings($entity_type, $bundle, $bundle_settings);
  1265. drupal_set_message(t('Your settings have been saved.'));
  1266. }
  1267. /**
  1268. * Populates display settings for a new view mode from the default view mode.
  1269. *
  1270. * When an administrator decides to use custom display settings for a view mode,
  1271. * that view mode needs to be initialized with the display settings for the
  1272. * 'default' view mode, which it was previously using. This helper function
  1273. * adds the new custom display settings to this bundle's instances, and saves
  1274. * them. It also modifies the passed-in $settings array, which the caller can
  1275. * then save using field_bundle_settings().
  1276. *
  1277. * @param $entity_type
  1278. * The bundle's entity type.
  1279. * @param $bundle
  1280. * The bundle whose view mode is being customized.
  1281. * @param $view_mode
  1282. * The view mode that the administrator has set to use custom settings.
  1283. * @param $settings
  1284. * An associative array of bundle settings, as expected by
  1285. * field_bundle_settings().
  1286. *
  1287. * @see field_ui_display_overview_form_submit().
  1288. * @see field_bundle_settings()
  1289. */
  1290. function _field_ui_add_default_view_mode_settings($entity_type, $bundle, $view_mode, &$settings) {
  1291. // Update display settings for field instances.
  1292. $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle));
  1293. foreach ($instances as $instance) {
  1294. // If this field instance has display settings defined for this view mode,
  1295. // respect those settings.
  1296. if (!isset($instance['display'][$view_mode])) {
  1297. // The instance doesn't specify anything for this view mode, so use the
  1298. // default display settings.
  1299. $instance['display'][$view_mode] = $instance['display']['default'];
  1300. field_update_instance($instance);
  1301. }
  1302. }
  1303. // Update display settings for 'extra fields'.
  1304. foreach (array_keys($settings['extra_fields']['display']) as $name) {
  1305. if (!isset($settings['extra_fields']['display'][$name][$view_mode])) {
  1306. $settings['extra_fields']['display'][$name][$view_mode] = $settings['extra_fields']['display'][$name]['default'];
  1307. }
  1308. }
  1309. }
  1310. /**
  1311. * Returns an array of field_type options.
  1312. */
  1313. function field_ui_field_type_options() {
  1314. $options = &drupal_static(__FUNCTION__);
  1315. if (!isset($options)) {
  1316. $options = array();
  1317. $field_types = field_info_field_types();
  1318. $field_type_options = array();
  1319. foreach ($field_types as $name => $field_type) {
  1320. // Skip field types which have no widget types, or should not be add via
  1321. // uesr interface.
  1322. if (field_ui_widget_type_options($name) && empty($field_type['no_ui'])) {
  1323. $options[$name] = $field_type['label'];
  1324. }
  1325. }
  1326. asort($options);
  1327. }
  1328. return $options;
  1329. }
  1330. /**
  1331. * Returns an array of widget type options for a field type.
  1332. *
  1333. * If no field type is provided, returns a nested array of all widget types,
  1334. * keyed by field type human name.
  1335. */
  1336. function field_ui_widget_type_options($field_type = NULL, $by_label = FALSE) {
  1337. $options = &drupal_static(__FUNCTION__);
  1338. if (!isset($options)) {
  1339. $options = array();
  1340. $field_types = field_info_field_types();
  1341. foreach (field_info_widget_types() as $name => $widget_type) {
  1342. foreach ($widget_type['field types'] as $widget_field_type) {
  1343. // Check that the field type exists.
  1344. if (isset($field_types[$widget_field_type])) {
  1345. $options[$widget_field_type][$name] = $widget_type['label'];
  1346. }
  1347. }
  1348. }
  1349. }
  1350. if (isset($field_type)) {
  1351. return !empty($options[$field_type]) ? $options[$field_type] : array();
  1352. }
  1353. if ($by_label) {
  1354. $field_types = field_info_field_types();
  1355. $options_by_label = array();
  1356. foreach ($options as $field_type => $widgets) {
  1357. $options_by_label[$field_types[$field_type]['label']] = $widgets;
  1358. }
  1359. return $options_by_label;
  1360. }
  1361. return $options;
  1362. }
  1363. /**
  1364. * Returns an array of formatter options for a field type.
  1365. *
  1366. * If no field type is provided, returns a nested array of all formatters, keyed
  1367. * by field type.
  1368. */
  1369. function field_ui_formatter_options($field_type = NULL) {
  1370. $options = &drupal_static(__FUNCTION__);
  1371. if (!isset($options)) {
  1372. $field_types = field_info_field_types();
  1373. $options = array();
  1374. foreach (field_info_formatter_types() as $name => $formatter) {
  1375. foreach ($formatter['field types'] as $formatter_field_type) {
  1376. // Check that the field type exists.
  1377. if (isset($field_types[$formatter_field_type])) {
  1378. $options[$formatter_field_type][$name] = $formatter['label'];
  1379. }
  1380. }
  1381. }
  1382. }
  1383. if ($field_type) {
  1384. return !empty($options[$field_type]) ? $options[$field_type] : array();
  1385. }
  1386. return $options;
  1387. }
  1388. /**
  1389. * Returns an array of existing fields to be added to a bundle.
  1390. */
  1391. function field_ui_existing_field_options($entity_type, $bundle) {
  1392. $info = array();
  1393. $field_types = field_info_field_types();
  1394. foreach (field_info_instances() as $existing_entity_type => $bundles) {
  1395. foreach ($bundles as $existing_bundle => $instances) {
  1396. // No need to look in the current bundle.
  1397. if (!($existing_bundle == $bundle && $existing_entity_type == $entity_type)) {
  1398. foreach ($instances as $instance) {
  1399. $field = field_info_field($instance['field_name']);
  1400. // Don't show
  1401. // - locked fields,
  1402. // - fields already in the current bundle,
  1403. // - fields that cannot be added to the entity type,
  1404. // - fields that should not be added via user interface.
  1405. if (empty($field['locked'])
  1406. && !field_info_instance($entity_type, $field['field_name'], $bundle)
  1407. && (empty($field['entity_types']) || in_array($entity_type, $field['entity_types']))
  1408. && empty($field_types[$field['type']]['no_ui'])) {
  1409. $info[$instance['field_name']] = array(
  1410. 'type' => $field['type'],
  1411. 'type_label' => $field_types[$field['type']]['label'],
  1412. 'field' => $field['field_name'],
  1413. 'label' => $instance['label'],
  1414. 'widget_type' => $instance['widget']['type'],
  1415. );
  1416. }
  1417. }
  1418. }
  1419. }
  1420. }
  1421. return $info;
  1422. }
  1423. /**
  1424. * Form constructor for the field settings edit page.
  1425. *
  1426. * @see field_ui_field_settings_form_submit()
  1427. * @ingroup forms
  1428. */
  1429. function field_ui_field_settings_form($form, &$form_state, $instance) {
  1430. $bundle = $instance['bundle'];
  1431. $entity_type = $instance['entity_type'];
  1432. $field = field_info_field($instance['field_name']);
  1433. drupal_set_title($instance['label']);
  1434. $description = '<p>' . t('These settings apply to the %field field everywhere it is used. These settings impact the way that data is stored in the database and cannot be changed once data has been created.', array('%field' => $instance['label'])) . '</p>';
  1435. // Create a form structure for the field values.
  1436. $form['field'] = array(
  1437. '#type' => 'fieldset',
  1438. '#title' => t('Field settings'),
  1439. '#description' => $description,
  1440. '#tree' => TRUE,
  1441. );
  1442. // See if data already exists for this field.
  1443. // If so, prevent changes to the field settings.
  1444. $has_data = field_has_data($field);
  1445. if ($has_data) {
  1446. $form['field']['#description'] = '<div class="messages error">' . t('There is data for this field in the database. The field settings can no longer be changed.') . '</div>' . $form['field']['#description'];
  1447. }
  1448. // Build the non-configurable field values.
  1449. $form['field']['field_name'] = array('#type' => 'value', '#value' => $field['field_name']);
  1450. $form['field']['type'] = array('#type' => 'value', '#value' => $field['type']);
  1451. $form['field']['module'] = array('#type' => 'value', '#value' => $field['module']);
  1452. $form['field']['active'] = array('#type' => 'value', '#value' => $field['active']);
  1453. // Add settings provided by the field module. The field module is
  1454. // responsible for not returning settings that cannot be changed if
  1455. // the field already has data.
  1456. $form['field']['settings'] = array();
  1457. $additions = module_invoke($field['module'], 'field_settings_form', $field, $instance, $has_data);
  1458. if (is_array($additions)) {
  1459. $form['field']['settings'] = $additions;
  1460. }
  1461. if (empty($form['field']['settings'])) {
  1462. $form['field']['settings'] = array(
  1463. '#markup' => t('%field has no field settings.', array('%field' => $instance['label'])),
  1464. );
  1465. }
  1466. $form['#entity_type'] = $entity_type;
  1467. $form['#bundle'] = $bundle;
  1468. $form['actions'] = array('#type' => 'actions');
  1469. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save field settings'));
  1470. return $form;
  1471. }
  1472. /**
  1473. * Form submission handler for field_ui_field_settings_form().
  1474. */
  1475. function field_ui_field_settings_form_submit($form, &$form_state) {
  1476. $form_values = $form_state['values'];
  1477. $field_values = $form_values['field'];
  1478. // Merge incoming form values into the existing field.
  1479. $field = field_info_field($field_values['field_name']);
  1480. $entity_type = $form['#entity_type'];
  1481. $bundle = $form['#bundle'];
  1482. $instance = field_info_instance($entity_type, $field['field_name'], $bundle);
  1483. // Update the field.
  1484. $field = array_merge($field, $field_values);
  1485. try {
  1486. field_update_field($field);
  1487. drupal_set_message(t('Updated field %label field settings.', array('%label' => $instance['label'])));
  1488. $form_state['redirect'] = field_ui_next_destination($entity_type, $bundle);
  1489. }
  1490. catch (Exception $e) {
  1491. drupal_set_message(t('Attempt to update field %label failed: %message.', array('%label' => $instance['label'], '%message' => $e->getMessage())), 'error');
  1492. }
  1493. }
  1494. /**
  1495. * Form constructor for the widget selection form.
  1496. *
  1497. * Path: BUNDLE_ADMIN_PATH/fields/%field/widget-type, where BUNDLE_ADMIN_PATH is
  1498. * the path stored in the ['admin']['info'] property in the return value of
  1499. * hook_entity_info().
  1500. *
  1501. * @see field_ui_menu()
  1502. * @see field_ui_widget_type_form_submit()
  1503. * @ingroup forms
  1504. */
  1505. function field_ui_widget_type_form($form, &$form_state, $instance) {
  1506. drupal_set_title($instance['label']);
  1507. $bundle = $instance['bundle'];
  1508. $entity_type = $instance['entity_type'];
  1509. $field_name = $instance['field_name'];
  1510. $field = field_info_field($field_name);
  1511. $field_type = field_info_field_types($field['type']);
  1512. $widget_type = field_info_widget_types($instance['widget']['type']);
  1513. $bundles = field_info_bundles();
  1514. $bundle_label = $bundles[$entity_type][$bundle]['label'];
  1515. $form = array(
  1516. '#bundle' => $bundle,
  1517. '#entity_type' => $entity_type,
  1518. '#field_name' => $field_name,
  1519. );
  1520. $form['basic'] = array(
  1521. '#type' => 'fieldset',
  1522. '#title' => t('Change widget'),
  1523. );
  1524. $form['basic']['widget_type'] = array(
  1525. '#type' => 'select',
  1526. '#title' => t('Widget type'),
  1527. '#required' => TRUE,
  1528. '#options' => field_ui_widget_type_options($field['type']),
  1529. '#default_value' => $instance['widget']['type'],
  1530. '#description' => t('The type of form element you would like to present to the user when creating this field in the %type type.', array('%type' => $bundle_label)),
  1531. );
  1532. $form['actions'] = array('#type' => 'actions');
  1533. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Continue'));
  1534. $form['#validate'] = array();
  1535. $form['#submit'] = array('field_ui_widget_type_form_submit');
  1536. return $form;
  1537. }
  1538. /**
  1539. * Form submission handler for field_ui_widget_type_form().
  1540. */
  1541. function field_ui_widget_type_form_submit($form, &$form_state) {
  1542. $form_values = $form_state['values'];
  1543. $bundle = $form['#bundle'];
  1544. $entity_type = $form['#entity_type'];
  1545. $field_name = $form['#field_name'];
  1546. // Retrieve the stored instance settings to merge with the incoming values.
  1547. $instance = field_read_instance($entity_type, $field_name, $bundle);
  1548. // Set the right module information.
  1549. $widget_type = field_info_widget_types($form_values['widget_type']);
  1550. $widget_module = $widget_type['module'];
  1551. $instance['widget']['type'] = $form_values['widget_type'];
  1552. $instance['widget']['module'] = $widget_module;
  1553. try {
  1554. field_update_instance($instance);
  1555. drupal_set_message(t('Changed the widget for field %label.', array('%label' => $instance['label'])));
  1556. }
  1557. catch (Exception $e) {
  1558. drupal_set_message(t('There was a problem changing the widget for field %label.', array('%label' => $instance['label'])), 'error');
  1559. }
  1560. $form_state['redirect'] = field_ui_next_destination($entity_type, $bundle);
  1561. }
  1562. /**
  1563. * Form constructor for removing a field instance from a bundle.
  1564. *
  1565. * @see field_ui_field_delete_form_submit()
  1566. * @ingroup forms
  1567. */
  1568. function field_ui_field_delete_form($form, &$form_state, $instance) {
  1569. $bundle = $instance['bundle'];
  1570. $entity_type = $instance['entity_type'];
  1571. $field = field_info_field($instance['field_name']);
  1572. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  1573. $form['entity_type'] = array('#type' => 'value', '#value' => $entity_type);
  1574. $form['bundle'] = array('#type' => 'value', '#value' => $bundle);
  1575. $form['field_name'] = array('#type' => 'value', '#value' => $field['field_name']);
  1576. $output = confirm_form($form,
  1577. t('Are you sure you want to delete the field %field?', array('%field' => $instance['label'])),
  1578. $admin_path . '/fields',
  1579. t('If you have any content left in this field, it will be lost. This action cannot be undone.'),
  1580. t('Delete'), t('Cancel'),
  1581. 'confirm'
  1582. );
  1583. if ($field['locked']) {
  1584. unset($output['actions']['submit']);
  1585. $output['description']['#markup'] = t('This field is <strong>locked</strong> and cannot be deleted.');
  1586. }
  1587. return $output;
  1588. }
  1589. /**
  1590. * Form submission handler for field_ui_field_delete_form().
  1591. *
  1592. * Removes a field instance from a bundle. If the field has no more instances,
  1593. * it will be marked as deleted too.
  1594. */
  1595. function field_ui_field_delete_form_submit($form, &$form_state) {
  1596. $form_values = $form_state['values'];
  1597. $field_name = $form_values['field_name'];
  1598. $bundle = $form_values['bundle'];
  1599. $entity_type = $form_values['entity_type'];
  1600. $field = field_info_field($field_name);
  1601. $instance = field_info_instance($entity_type, $field_name, $bundle);
  1602. $bundles = field_info_bundles();
  1603. $bundle_label = $bundles[$entity_type][$bundle]['label'];
  1604. if (!empty($bundle) && $field && !$field['locked'] && $form_values['confirm']) {
  1605. field_delete_instance($instance);
  1606. drupal_set_message(t('The field %field has been deleted from the %type content type.', array('%field' => $instance['label'], '%type' => $bundle_label)));
  1607. }
  1608. else {
  1609. drupal_set_message(t('There was a problem removing the %field from the %type content type.', array('%field' => $instance['label'], '%type' => $bundle_label)), 'error');
  1610. }
  1611. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  1612. $form_state['redirect'] = field_ui_get_destinations(array($admin_path . '/fields'));
  1613. // Fields are purged on cron. However field module prevents disabling modules
  1614. // when field types they provided are used in a field until it is fully
  1615. // purged. In the case that a field has minimal or no content, a single call
  1616. // to field_purge_batch() will remove it from the system. Call this with a
  1617. // low batch limit to avoid administrators having to wait for cron runs when
  1618. // removing instances that meet this criteria.
  1619. field_purge_batch(10);
  1620. }
  1621. /**
  1622. * Form constructor for the field instance settings form.
  1623. *
  1624. * @see field_ui_field_edit_form_validate()
  1625. * @see field_ui_field_edit_form_submit()
  1626. * @ingroup forms
  1627. */
  1628. function field_ui_field_edit_form($form, &$form_state, $instance) {
  1629. $bundle = $instance['bundle'];
  1630. $entity_type = $instance['entity_type'];
  1631. $field = field_info_field($instance['field_name']);
  1632. drupal_set_title($instance['label']);
  1633. $form['#field'] = $field;
  1634. $form['#instance'] = $instance;
  1635. if (!empty($field['locked'])) {
  1636. $form['locked'] = array(
  1637. '#markup' => t('The field %field is locked and cannot be edited.', array('%field' => $instance['label'])),
  1638. );
  1639. return $form;
  1640. }
  1641. $field_type = field_info_field_types($field['type']);
  1642. $widget_type = field_info_widget_types($instance['widget']['type']);
  1643. $bundles = field_info_bundles();
  1644. // Create a form structure for the instance values.
  1645. $form['instance'] = array(
  1646. '#tree' => TRUE,
  1647. '#type' => 'fieldset',
  1648. '#title' => t('%type settings', array('%type' => $bundles[$entity_type][$bundle]['label'])),
  1649. '#description' => t('These settings apply only to the %field field when used in the %type type.', array(
  1650. '%field' => $instance['label'],
  1651. '%type' => $bundles[$entity_type][$bundle]['label'],
  1652. )),
  1653. // Ensure field_ui_field_edit_instance_pre_render() gets called in addition
  1654. // to, not instead of, the #pre_render function(s) needed by all fieldsets.
  1655. '#pre_render' => array_merge(array('field_ui_field_edit_instance_pre_render'), element_info_property('fieldset', '#pre_render', array())),
  1656. );
  1657. // Build the non-configurable instance values.
  1658. $form['instance']['field_name'] = array(
  1659. '#type' => 'value',
  1660. '#value' => $instance['field_name'],
  1661. );
  1662. $form['instance']['entity_type'] = array(
  1663. '#type' => 'value',
  1664. '#value' => $entity_type,
  1665. );
  1666. $form['instance']['bundle'] = array(
  1667. '#type' => 'value',
  1668. '#value' => $bundle,
  1669. );
  1670. $form['instance']['widget']['weight'] = array(
  1671. '#type' => 'value',
  1672. '#value' => !empty($instance['widget']['weight']) ? $instance['widget']['weight'] : 0,
  1673. );
  1674. // Build the configurable instance values.
  1675. $form['instance']['label'] = array(
  1676. '#type' => 'textfield',
  1677. '#title' => t('Label'),
  1678. '#default_value' => !empty($instance['label']) ? $instance['label'] : $field['field_name'],
  1679. '#required' => TRUE,
  1680. '#weight' => -20,
  1681. );
  1682. $form['instance']['required'] = array(
  1683. '#type' => 'checkbox',
  1684. '#title' => t('Required field'),
  1685. '#default_value' => !empty($instance['required']),
  1686. '#weight' => -10,
  1687. );
  1688. $form['instance']['description'] = array(
  1689. '#type' => 'textarea',
  1690. '#title' => t('Help text'),
  1691. '#default_value' => !empty($instance['description']) ? $instance['description'] : '',
  1692. '#rows' => 5,
  1693. '#description' => t('Instructions to present to the user below this field on the editing form.<br />Allowed HTML tags: @tags', array('@tags' => _field_filter_xss_display_allowed_tags())),
  1694. '#weight' => -5,
  1695. );
  1696. // Build the widget component of the instance.
  1697. $form['instance']['widget']['type'] = array(
  1698. '#type' => 'value',
  1699. '#value' => $instance['widget']['type'],
  1700. );
  1701. $form['instance']['widget']['module'] = array(
  1702. '#type' => 'value',
  1703. '#value' => $widget_type['module'],
  1704. );
  1705. $form['instance']['widget']['active'] = array(
  1706. '#type' => 'value',
  1707. '#value' => !empty($field['instance']['widget']['active']) ? 1 : 0,
  1708. );
  1709. // Add additional field instance settings from the field module.
  1710. $additions = module_invoke($field['module'], 'field_instance_settings_form', $field, $instance);
  1711. if (is_array($additions)) {
  1712. $form['instance']['settings'] = $additions;
  1713. }
  1714. // Add additional widget settings from the widget module.
  1715. $additions = module_invoke($widget_type['module'], 'field_widget_settings_form', $field, $instance);
  1716. if (is_array($additions)) {
  1717. $form['instance']['widget']['settings'] = $additions;
  1718. $form['instance']['widget']['active']['#value'] = 1;
  1719. }
  1720. // Add handling for default value if not provided by any other module.
  1721. if (field_behaviors_widget('default value', $instance) == FIELD_BEHAVIOR_DEFAULT && empty($instance['default_value_function'])) {
  1722. $form['instance']['default_value_widget'] = field_ui_default_value_widget($field, $instance, $form, $form_state);
  1723. }
  1724. $has_data = field_has_data($field);
  1725. if ($has_data) {
  1726. $description = '<p>' . t('These settings apply to the %field field everywhere it is used. Because the field already has data, some settings can no longer be changed.', array('%field' => $instance['label'])) . '</p>';
  1727. }
  1728. else {
  1729. $description = '<p>' . t('These settings apply to the %field field everywhere it is used.', array('%field' => $instance['label'])) . '</p>';
  1730. }
  1731. // Create a form structure for the field values.
  1732. $form['field'] = array(
  1733. '#type' => 'fieldset',
  1734. '#title' => t('%field field settings', array('%field' => $instance['label'])),
  1735. '#description' => $description,
  1736. '#tree' => TRUE,
  1737. );
  1738. // Build the configurable field values.
  1739. $description = t('Maximum number of values users can enter for this field.');
  1740. if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_DEFAULT) {
  1741. $description .= '<br/>' . t("'Unlimited' will provide an 'Add more' button so the users can add as many values as they like.");
  1742. }
  1743. $form['field']['cardinality'] = array(
  1744. '#type' => 'select',
  1745. '#title' => t('Number of values'),
  1746. '#options' => array(FIELD_CARDINALITY_UNLIMITED => t('Unlimited')) + drupal_map_assoc(range(1, 10)),
  1747. '#default_value' => $field['cardinality'],
  1748. '#description' => $description,
  1749. );
  1750. // Add additional field type settings. The field type module is
  1751. // responsible for not returning settings that cannot be changed if
  1752. // the field already has data.
  1753. $additions = module_invoke($field['module'], 'field_settings_form', $field, $instance, $has_data);
  1754. if (is_array($additions)) {
  1755. $form['field']['settings'] = $additions;
  1756. }
  1757. $form['actions'] = array('#type' => 'actions');
  1758. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save settings'));
  1759. return $form;
  1760. }
  1761. /**
  1762. * Pre-render function for field instance settings.
  1763. *
  1764. * Combines the instance, widget, and other settings into a single fieldset so
  1765. * that elements within each group can be shown at different weights as if they
  1766. * all had the same parent.
  1767. */
  1768. function field_ui_field_edit_instance_pre_render($element) {
  1769. // Merge the widget settings into the main form.
  1770. if (isset($element['widget']['settings'])) {
  1771. foreach (element_children($element['widget']['settings']) as $key) {
  1772. $element['widget_' . $key] = $element['widget']['settings'][$key];
  1773. }
  1774. unset($element['widget']['settings']);
  1775. }
  1776. // Merge the instance settings into the main form.
  1777. if (isset($element['settings'])) {
  1778. foreach (element_children($element['settings']) as $key) {
  1779. $element['instance_' . $key] = $element['settings'][$key];
  1780. }
  1781. unset($element['settings']);
  1782. }
  1783. return $element;
  1784. }
  1785. /**
  1786. * Builds the default value fieldset for a given field instance.
  1787. */
  1788. function field_ui_default_value_widget($field, $instance, &$form, &$form_state) {
  1789. $field_name = $field['field_name'];
  1790. $element = array(
  1791. '#type' => 'fieldset',
  1792. '#title' => t('Default value'),
  1793. '#collapsible' => FALSE,
  1794. '#tree' => TRUE,
  1795. '#description' => t('The default value for this field, used when creating new content.'),
  1796. // Stick to an empty 'parents' on this form in order not to breaks widgets
  1797. // that do not use field_widget_[field|instance]() and still access
  1798. // $form_state['field'] directly.
  1799. '#parents' => array(),
  1800. );
  1801. // Insert the widget.
  1802. $items = $instance['default_value'];
  1803. $instance['required'] = FALSE;
  1804. $instance['description'] = '';
  1805. // @todo Allow multiple values (requires more work on 'add more' JS handler).
  1806. $element += field_default_form($instance['entity_type'], NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state, 0);
  1807. return $element;
  1808. }
  1809. /**
  1810. * Form validation handler for field_ui_field_edit_form().
  1811. *
  1812. * @see field_ui_field_edit_form_submit().
  1813. */
  1814. function field_ui_field_edit_form_validate($form, &$form_state) {
  1815. // Take the incoming values as the $instance definition, so that the 'default
  1816. // value' gets validated using the instance settings being submitted.
  1817. $instance = $form_state['values']['instance'];
  1818. $field_name = $instance['field_name'];
  1819. if (isset($form['instance']['default_value_widget'])) {
  1820. $element = $form['instance']['default_value_widget'];
  1821. $field_state = field_form_get_state($element['#parents'], $field_name, LANGUAGE_NONE, $form_state);
  1822. $field = $field_state['field'];
  1823. // Extract the 'default value'.
  1824. $items = array();
  1825. field_default_extract_form_values(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state);
  1826. // Validate the value and report errors.
  1827. $errors = array();
  1828. $function = $field['module'] . '_field_validate';
  1829. if (function_exists($function)) {
  1830. $function(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $errors);
  1831. }
  1832. if (isset($errors[$field_name][LANGUAGE_NONE])) {
  1833. // Store reported errors in $form_state.
  1834. $field_state['errors'] = $errors[$field_name][LANGUAGE_NONE];
  1835. field_form_set_state($element['#parents'], $field_name, LANGUAGE_NONE, $form_state, $field_state);
  1836. // Assign reported errors to the correct form element.
  1837. field_default_form_errors(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state);
  1838. }
  1839. }
  1840. }
  1841. /**
  1842. * Form submission handler for field_ui_field_edit_form().
  1843. *
  1844. * @see field_ui_field_edit_form_validate().
  1845. */
  1846. function field_ui_field_edit_form_submit($form, &$form_state) {
  1847. $instance = $form_state['values']['instance'];
  1848. $field = $form_state['values']['field'];
  1849. // Update any field settings that have changed.
  1850. $field_source = field_info_field($instance['field_name']);
  1851. $field = array_merge($field_source, $field);
  1852. try {
  1853. field_update_field($field);
  1854. }
  1855. catch (Exception $e) {
  1856. drupal_set_message(t('Attempt to update field %label failed: %message.', array('%label' => $instance['label'], '%message' => $e->getMessage())), 'error');
  1857. return;
  1858. }
  1859. // Handle the default value.
  1860. if (isset($form['instance']['default_value_widget'])) {
  1861. $element = $form['instance']['default_value_widget'];
  1862. // Extract field values.
  1863. $items = array();
  1864. field_default_extract_form_values(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state);
  1865. field_default_submit(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state);
  1866. $instance['default_value'] = $items ? $items : NULL;
  1867. }
  1868. // Retrieve the stored instance settings to merge with the incoming values.
  1869. $instance_source = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
  1870. $instance = array_merge($instance_source, $instance);
  1871. field_update_instance($instance);
  1872. drupal_set_message(t('Saved %label configuration.', array('%label' => $instance['label'])));
  1873. $form_state['redirect'] = field_ui_next_destination($instance['entity_type'], $instance['bundle']);
  1874. }
  1875. /**
  1876. * Extracts next redirect path from an array of multiple destinations.
  1877. *
  1878. * @see field_ui_next_destination()
  1879. */
  1880. function field_ui_get_destinations($destinations) {
  1881. $path = array_shift($destinations);
  1882. $options = drupal_parse_url($path);
  1883. if ($destinations) {
  1884. $options['query']['destinations'] = $destinations;
  1885. }
  1886. return array($options['path'], $options);
  1887. }
  1888. /**
  1889. * Returns the next redirect path in a multipage sequence.
  1890. */
  1891. function field_ui_next_destination($entity_type, $bundle) {
  1892. $destinations = !empty($_REQUEST['destinations']) ? $_REQUEST['destinations'] : array();
  1893. if (!empty($destinations)) {
  1894. unset($_REQUEST['destinations']);
  1895. }
  1896. // Remove any external URLs.
  1897. $destinations = array_diff($destinations, array_filter($destinations, 'url_is_external'));
  1898. if ($destinations) {
  1899. return field_ui_get_destinations($destinations);
  1900. }
  1901. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  1902. return $admin_path . '/fields';
  1903. }