field_ui.admin.inc 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  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. // For hidden fields, $formatter will be NULL, but we expect an array later.
  943. // To maintain BC, but avoid PHP 7.4 Notices, ensure $formatter is an array
  944. // with a 'module' element.
  945. $formatter['module'] = isset($formatter['module']) ? $formatter['module'] : '';
  946. $instance['display'][$view_mode]['module'] = $formatter['module'];
  947. $instance['display'][$view_mode]['settings'] = $settings;
  948. // Base button element for the various formatter settings actions.
  949. $base_button = array(
  950. '#submit' => array('field_ui_display_overview_multistep_submit'),
  951. '#ajax' => array(
  952. 'callback' => 'field_ui_display_overview_multistep_js',
  953. 'wrapper' => 'field-display-overview-wrapper',
  954. 'effect' => 'fade',
  955. ),
  956. '#field_name' => $name,
  957. );
  958. if ($form_state['formatter_settings_edit'] == $name) {
  959. // We are currently editing this field's formatter settings. Display the
  960. // settings form and submit buttons.
  961. $table[$name]['format']['settings_edit_form'] = array();
  962. $settings_form = array();
  963. $function = $formatter['module'] . '_field_formatter_settings_form';
  964. if (function_exists($function)) {
  965. $settings_form = $function($field, $instance, $view_mode, $form, $form_state);
  966. }
  967. if ($settings_form) {
  968. $table[$name]['format']['#cell_attributes'] = array('colspan' => 3);
  969. $table[$name]['format']['settings_edit_form'] = array(
  970. '#type' => 'container',
  971. '#attributes' => array('class' => array('field-formatter-settings-edit-form')),
  972. '#parents' => array('fields', $name, 'settings_edit_form'),
  973. 'label' => array(
  974. '#markup' => t('Format settings:') . ' <span class="formatter-name">' . $formatter['label'] . '</span>',
  975. ),
  976. 'settings' => $settings_form,
  977. 'actions' => array(
  978. '#type' => 'actions',
  979. 'save_settings' => $base_button + array(
  980. '#type' => 'submit',
  981. '#name' => $name . '_formatter_settings_update',
  982. '#value' => t('Update'),
  983. '#op' => 'update',
  984. ),
  985. 'cancel_settings' => $base_button + array(
  986. '#type' => 'submit',
  987. '#name' => $name . '_formatter_settings_cancel',
  988. '#value' => t('Cancel'),
  989. '#op' => 'cancel',
  990. // Do not check errors for the 'Cancel' button, but make sure we
  991. // get the value of the 'formatter type' select.
  992. '#limit_validation_errors' => array(array('fields', $name, 'type')),
  993. ),
  994. ),
  995. );
  996. $table[$name]['#attributes']['class'][] = 'field-formatter-settings-editing';
  997. }
  998. }
  999. else {
  1000. // Display a summary of the current formatter settings.
  1001. $summary = module_invoke($formatter['module'], 'field_formatter_settings_summary', $field, $instance, $view_mode);
  1002. $table[$name]['settings_summary'] = array();
  1003. $table[$name]['settings_edit'] = array();
  1004. if ($summary) {
  1005. $table[$name]['settings_summary'] = array(
  1006. '#markup' => '<div class="field-formatter-summary">' . $summary . '</div>',
  1007. '#cell_attributes' => array('class' => array('field-formatter-summary-cell')),
  1008. );
  1009. $table[$name]['settings_edit'] = $base_button + array(
  1010. '#type' => 'image_button',
  1011. '#name' => $name . '_formatter_settings_edit',
  1012. '#src' => 'misc/configure.png',
  1013. '#attributes' => array('class' => array('field-formatter-settings-edit'), 'alt' => t('Edit')),
  1014. '#op' => 'edit',
  1015. // Do not check errors for the 'Edit' button, but make sure we get
  1016. // the value of the 'formatter type' select.
  1017. '#limit_validation_errors' => array(array('fields', $name, 'type')),
  1018. '#prefix' => '<div class="field-formatter-settings-edit-wrapper">',
  1019. '#suffix' => '</div>',
  1020. );
  1021. }
  1022. }
  1023. }
  1024. // Non-field elements.
  1025. foreach ($extra_fields as $name => $extra_field) {
  1026. $display = $extra_field['display'][$view_mode];
  1027. $table[$name] = array(
  1028. '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
  1029. '#row_type' => 'extra_field',
  1030. '#region_callback' => 'field_ui_display_overview_row_region',
  1031. '#js_settings' => array('rowHandler' => 'field'),
  1032. 'human_name' => array(
  1033. '#markup' => check_plain($extra_field['label']),
  1034. ),
  1035. 'weight' => array(
  1036. '#type' => 'textfield',
  1037. '#title' => t('Weight for @title', array('@title' => $extra_field['label'])),
  1038. '#title_display' => 'invisible',
  1039. '#default_value' => $display['weight'],
  1040. '#size' => 3,
  1041. '#attributes' => array('class' => array('field-weight')),
  1042. ),
  1043. 'parent_wrapper' => array(
  1044. 'parent' => array(
  1045. '#type' => 'select',
  1046. '#title' => t('Parents for @title', array('@title' => $extra_field['label'])),
  1047. '#title_display' => 'invisible',
  1048. '#options' => $table['#parent_options'],
  1049. '#empty_value' => '',
  1050. '#attributes' => array('class' => array('field-parent')),
  1051. '#parents' => array('fields', $name, 'parent'),
  1052. ),
  1053. 'hidden_name' => array(
  1054. '#type' => 'hidden',
  1055. '#default_value' => $name,
  1056. '#attributes' => array('class' => array('field-name')),
  1057. ),
  1058. ),
  1059. 'empty_cell' => array(
  1060. '#markup' => '&nbsp;',
  1061. ),
  1062. 'format' => array(
  1063. 'type' => array(
  1064. '#type' => 'select',
  1065. '#title' => t('Visibility for @title', array('@title' => $extra_field['label'])),
  1066. '#title_display' => 'invisible',
  1067. '#options' => $extra_visibility_options,
  1068. '#default_value' => $display['visible'] ? 'visible' : 'hidden',
  1069. '#parents' => array('fields', $name, 'type'),
  1070. '#attributes' => array('class' => array('field-formatter-type')),
  1071. ),
  1072. ),
  1073. 'settings_summary' => array(),
  1074. 'settings_edit' => array(),
  1075. );
  1076. }
  1077. $form['fields'] = $table;
  1078. // Custom display settings.
  1079. if ($view_mode == 'default') {
  1080. $form['modes'] = array(
  1081. '#type' => 'fieldset',
  1082. '#title' => t('Custom display settings'),
  1083. '#collapsible' => TRUE,
  1084. '#collapsed' => TRUE,
  1085. );
  1086. // Collect options and default values for the 'Custom display settings'
  1087. // checkboxes.
  1088. $options = array();
  1089. $default = array();
  1090. $entity_info = entity_get_info($entity_type);
  1091. $view_modes = $entity_info['view modes'];
  1092. $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
  1093. foreach ($view_modes as $view_mode_name => $view_mode_info) {
  1094. $options[$view_mode_name] = $view_mode_info['label'];
  1095. if (!empty($view_mode_settings[$view_mode_name]['custom_settings'])) {
  1096. $default[] = $view_mode_name;
  1097. }
  1098. }
  1099. $form['modes']['view_modes_custom'] = array(
  1100. '#type' => 'checkboxes',
  1101. '#title' => t('Use custom display settings for the following view modes'),
  1102. '#options' => $options,
  1103. '#default_value' => $default,
  1104. );
  1105. }
  1106. // In overviews involving nested rows from contributed modules (i.e
  1107. // field_group), the 'format type' selects can trigger a series of changes in
  1108. // child rows. The #ajax behavior is therefore not attached directly to the
  1109. // selects, but triggered by the client-side script through a hidden #ajax
  1110. // 'Refresh' button. A hidden 'refresh_rows' input tracks the name of
  1111. // affected rows.
  1112. $form['refresh_rows'] = array('#type' => 'hidden');
  1113. $form['refresh'] = array(
  1114. '#type' => 'submit',
  1115. '#value' => t('Refresh'),
  1116. '#op' => 'refresh_table',
  1117. '#submit' => array('field_ui_display_overview_multistep_submit'),
  1118. '#ajax' => array(
  1119. 'callback' => 'field_ui_display_overview_multistep_js',
  1120. 'wrapper' => 'field-display-overview-wrapper',
  1121. 'effect' => 'fade',
  1122. // The button stays hidden, so we hide the Ajax spinner too. Ad-hoc
  1123. // spinners will be added manually by the client-side script.
  1124. 'progress' => 'none',
  1125. ),
  1126. );
  1127. $form['actions'] = array('#type' => 'actions');
  1128. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  1129. $form['#attached']['js'][] = drupal_get_path('module', 'field_ui') . '/field_ui.js';
  1130. $form['#attached']['css'][] = drupal_get_path('module', 'field_ui') . '/field_ui.css';
  1131. // Add tabledrag behavior.
  1132. $form['#attached']['drupal_add_tabledrag'][] = array('field-display-overview', 'order', 'sibling', 'field-weight');
  1133. $form['#attached']['drupal_add_tabledrag'][] = array('field-display-overview', 'match', 'parent', 'field-parent', 'field-parent', 'field-name');
  1134. return $form;
  1135. }
  1136. /**
  1137. * Form submission handler for buttons in field_ui_display_overview_form().
  1138. */
  1139. function field_ui_display_overview_multistep_submit($form, &$form_state) {
  1140. $trigger = $form_state['triggering_element'];
  1141. $op = $trigger['#op'];
  1142. switch ($op) {
  1143. case 'edit':
  1144. // Store the field whose settings are currently being edited.
  1145. $field_name = $trigger['#field_name'];
  1146. $form_state['formatter_settings_edit'] = $field_name;
  1147. break;
  1148. case 'update':
  1149. // Store the saved settings, and set the field back to 'non edit' mode.
  1150. $field_name = $trigger['#field_name'];
  1151. $values = $form_state['values']['fields'][$field_name]['settings_edit_form']['settings'];
  1152. $form_state['formatter_settings'][$field_name] = $values;
  1153. unset($form_state['formatter_settings_edit']);
  1154. break;
  1155. case 'cancel':
  1156. // Set the field back to 'non edit' mode.
  1157. unset($form_state['formatter_settings_edit']);
  1158. break;
  1159. case 'refresh_table':
  1160. // If the currently edited field is one of the rows to be refreshed, set
  1161. // it back to 'non edit' mode.
  1162. $updated_rows = explode(' ', $form_state['values']['refresh_rows']);
  1163. if (isset($form_state['formatter_settings_edit']) && in_array($form_state['formatter_settings_edit'], $updated_rows)) {
  1164. unset($form_state['formatter_settings_edit']);
  1165. }
  1166. break;
  1167. }
  1168. $form_state['rebuild'] = TRUE;
  1169. }
  1170. /**
  1171. * Ajax handler for multistep buttons on the 'Manage display' screen.
  1172. */
  1173. function field_ui_display_overview_multistep_js($form, &$form_state) {
  1174. $trigger = $form_state['triggering_element'];
  1175. $op = $trigger['#op'];
  1176. // Pick the elements that need to receive the ajax-new-content effect.
  1177. switch ($op) {
  1178. case 'edit':
  1179. $updated_rows = array($trigger['#field_name']);
  1180. $updated_columns = array('format');
  1181. break;
  1182. case 'update':
  1183. case 'cancel':
  1184. $updated_rows = array($trigger['#field_name']);
  1185. $updated_columns = array('format', 'settings_summary', 'settings_edit');
  1186. break;
  1187. case 'refresh_table':
  1188. $updated_rows = array_values(explode(' ', $form_state['values']['refresh_rows']));
  1189. $updated_columns = array('settings_summary', 'settings_edit');
  1190. break;
  1191. }
  1192. foreach ($updated_rows as $name) {
  1193. foreach ($updated_columns as $key) {
  1194. $element = &$form['fields'][$name][$key];
  1195. $element['#prefix'] = '<div class="ajax-new-content">' . (isset($element['#prefix']) ? $element['#prefix'] : '');
  1196. $element['#suffix'] = (isset($element['#suffix']) ? $element['#suffix'] : '') . '</div>';
  1197. }
  1198. }
  1199. // Return the whole table.
  1200. return $form['fields'];
  1201. }
  1202. /**
  1203. * Form submission handler for field_ui_display_overview_form().
  1204. */
  1205. function field_ui_display_overview_form_submit($form, &$form_state) {
  1206. $form_values = $form_state['values'];
  1207. $entity_type = $form['#entity_type'];
  1208. $bundle = $form['#bundle'];
  1209. $view_mode = $form['#view_mode'];
  1210. // Save data for 'regular' fields.
  1211. foreach ($form['#fields'] as $field_name) {
  1212. // Retrieve the stored instance settings to merge with the incoming values.
  1213. $instance = field_read_instance($entity_type, $field_name, $bundle);
  1214. $values = $form_values['fields'][$field_name];
  1215. // Get formatter settings. They lie either directly in submitted form
  1216. // values (if the whole form was submitted while some formatter
  1217. // settings were being edited), or have been persisted in
  1218. // $form_state.
  1219. $settings = array();
  1220. if (isset($values['settings_edit_form']['settings'])) {
  1221. $settings = $values['settings_edit_form']['settings'];
  1222. }
  1223. elseif (isset($form_state['formatter_settings'][$field_name])) {
  1224. $settings = $form_state['formatter_settings'][$field_name];
  1225. }
  1226. elseif (isset($instance['display'][$view_mode]['settings'])) {
  1227. $settings = $instance['display'][$view_mode]['settings'];
  1228. }
  1229. // Only save settings actually used by the selected formatter.
  1230. $default_settings = field_info_formatter_settings($values['type']);
  1231. $settings = array_intersect_key($settings, $default_settings);
  1232. $instance['display'][$view_mode] = array(
  1233. 'label' => $values['label'],
  1234. 'type' => $values['type'],
  1235. 'weight' => $values['weight'],
  1236. 'settings' => $settings,
  1237. );
  1238. field_update_instance($instance);
  1239. }
  1240. // Get current bundle settings.
  1241. $bundle_settings = field_bundle_settings($entity_type, $bundle);
  1242. // Save data for 'extra' fields.
  1243. foreach ($form['#extra'] as $name) {
  1244. $bundle_settings['extra_fields']['display'][$name][$view_mode] = array(
  1245. 'weight' => $form_values['fields'][$name]['weight'],
  1246. 'visible' => $form_values['fields'][$name]['type'] == 'visible',
  1247. );
  1248. }
  1249. // Save view modes data.
  1250. if ($view_mode == 'default') {
  1251. $entity_info = entity_get_info($entity_type);
  1252. foreach ($form_values['view_modes_custom'] as $view_mode_name => $value) {
  1253. // Display a message for each view mode newly configured to use custom
  1254. // settings.
  1255. $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
  1256. if (!empty($value) && empty($view_mode_settings[$view_mode_name]['custom_settings'])) {
  1257. $view_mode_label = $entity_info['view modes'][$view_mode_name]['label'];
  1258. $path = _field_ui_bundle_admin_path($entity_type, $bundle) . "/display/$view_mode_name";
  1259. 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))));
  1260. // Initialize the newly customized view mode with the display settings
  1261. // from the default view mode.
  1262. _field_ui_add_default_view_mode_settings($entity_type, $bundle, $view_mode_name, $bundle_settings);
  1263. }
  1264. $bundle_settings['view_modes'][$view_mode_name]['custom_settings'] = !empty($value);
  1265. }
  1266. }
  1267. // Save updated bundle settings.
  1268. field_bundle_settings($entity_type, $bundle, $bundle_settings);
  1269. drupal_set_message(t('Your settings have been saved.'));
  1270. }
  1271. /**
  1272. * Populates display settings for a new view mode from the default view mode.
  1273. *
  1274. * When an administrator decides to use custom display settings for a view mode,
  1275. * that view mode needs to be initialized with the display settings for the
  1276. * 'default' view mode, which it was previously using. This helper function
  1277. * adds the new custom display settings to this bundle's instances, and saves
  1278. * them. It also modifies the passed-in $settings array, which the caller can
  1279. * then save using field_bundle_settings().
  1280. *
  1281. * @param $entity_type
  1282. * The bundle's entity type.
  1283. * @param $bundle
  1284. * The bundle whose view mode is being customized.
  1285. * @param $view_mode
  1286. * The view mode that the administrator has set to use custom settings.
  1287. * @param $settings
  1288. * An associative array of bundle settings, as expected by
  1289. * field_bundle_settings().
  1290. *
  1291. * @see field_ui_display_overview_form_submit().
  1292. * @see field_bundle_settings()
  1293. */
  1294. function _field_ui_add_default_view_mode_settings($entity_type, $bundle, $view_mode, &$settings) {
  1295. // Update display settings for field instances.
  1296. $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle));
  1297. foreach ($instances as $instance) {
  1298. // If this field instance has display settings defined for this view mode,
  1299. // respect those settings.
  1300. if (!isset($instance['display'][$view_mode])) {
  1301. // The instance doesn't specify anything for this view mode, so use the
  1302. // default display settings.
  1303. $instance['display'][$view_mode] = $instance['display']['default'];
  1304. field_update_instance($instance);
  1305. }
  1306. }
  1307. // Update display settings for 'extra fields'.
  1308. foreach (array_keys($settings['extra_fields']['display']) as $name) {
  1309. if (!isset($settings['extra_fields']['display'][$name][$view_mode])) {
  1310. $settings['extra_fields']['display'][$name][$view_mode] = $settings['extra_fields']['display'][$name]['default'];
  1311. }
  1312. }
  1313. }
  1314. /**
  1315. * Returns an array of field_type options.
  1316. */
  1317. function field_ui_field_type_options() {
  1318. $options = &drupal_static(__FUNCTION__);
  1319. if (!isset($options)) {
  1320. $options = array();
  1321. $field_types = field_info_field_types();
  1322. $field_type_options = array();
  1323. foreach ($field_types as $name => $field_type) {
  1324. // Skip field types which have no widget types, or should not be add via
  1325. // uesr interface.
  1326. if (field_ui_widget_type_options($name) && empty($field_type['no_ui'])) {
  1327. $options[$name] = $field_type['label'];
  1328. }
  1329. }
  1330. asort($options);
  1331. }
  1332. return $options;
  1333. }
  1334. /**
  1335. * Returns an array of widget type options for a field type.
  1336. *
  1337. * If no field type is provided, returns a nested array of all widget types,
  1338. * keyed by field type human name.
  1339. */
  1340. function field_ui_widget_type_options($field_type = NULL, $by_label = FALSE) {
  1341. $options = &drupal_static(__FUNCTION__);
  1342. if (!isset($options)) {
  1343. $options = array();
  1344. $field_types = field_info_field_types();
  1345. foreach (field_info_widget_types() as $name => $widget_type) {
  1346. foreach ($widget_type['field types'] as $widget_field_type) {
  1347. // Check that the field type exists.
  1348. if (isset($field_types[$widget_field_type])) {
  1349. $options[$widget_field_type][$name] = $widget_type['label'];
  1350. }
  1351. }
  1352. }
  1353. }
  1354. if (isset($field_type)) {
  1355. return !empty($options[$field_type]) ? $options[$field_type] : array();
  1356. }
  1357. if ($by_label) {
  1358. $field_types = field_info_field_types();
  1359. $options_by_label = array();
  1360. foreach ($options as $field_type => $widgets) {
  1361. $options_by_label[$field_types[$field_type]['label']] = $widgets;
  1362. }
  1363. return $options_by_label;
  1364. }
  1365. return $options;
  1366. }
  1367. /**
  1368. * Returns an array of formatter options for a field type.
  1369. *
  1370. * If no field type is provided, returns a nested array of all formatters, keyed
  1371. * by field type.
  1372. */
  1373. function field_ui_formatter_options($field_type = NULL) {
  1374. $options = &drupal_static(__FUNCTION__);
  1375. if (!isset($options)) {
  1376. $field_types = field_info_field_types();
  1377. $options = array();
  1378. foreach (field_info_formatter_types() as $name => $formatter) {
  1379. foreach ($formatter['field types'] as $formatter_field_type) {
  1380. // Check that the field type exists.
  1381. if (isset($field_types[$formatter_field_type])) {
  1382. $options[$formatter_field_type][$name] = $formatter['label'];
  1383. }
  1384. }
  1385. }
  1386. }
  1387. if ($field_type) {
  1388. return !empty($options[$field_type]) ? $options[$field_type] : array();
  1389. }
  1390. return $options;
  1391. }
  1392. /**
  1393. * Returns an array of existing fields to be added to a bundle.
  1394. */
  1395. function field_ui_existing_field_options($entity_type, $bundle) {
  1396. $info = array();
  1397. $field_types = field_info_field_types();
  1398. foreach (field_info_instances() as $existing_entity_type => $bundles) {
  1399. foreach ($bundles as $existing_bundle => $instances) {
  1400. // No need to look in the current bundle.
  1401. if (!($existing_bundle == $bundle && $existing_entity_type == $entity_type)) {
  1402. foreach ($instances as $instance) {
  1403. $field = field_info_field($instance['field_name']);
  1404. // Don't show
  1405. // - locked fields,
  1406. // - fields already in the current bundle,
  1407. // - fields that cannot be added to the entity type,
  1408. // - fields that should not be added via user interface.
  1409. if (empty($field['locked'])
  1410. && !field_info_instance($entity_type, $field['field_name'], $bundle)
  1411. && (empty($field['entity_types']) || in_array($entity_type, $field['entity_types']))
  1412. && empty($field_types[$field['type']]['no_ui'])) {
  1413. $info[$instance['field_name']] = array(
  1414. 'type' => $field['type'],
  1415. 'type_label' => $field_types[$field['type']]['label'],
  1416. 'field' => $field['field_name'],
  1417. 'label' => $instance['label'],
  1418. 'widget_type' => $instance['widget']['type'],
  1419. );
  1420. }
  1421. }
  1422. }
  1423. }
  1424. }
  1425. return $info;
  1426. }
  1427. /**
  1428. * Form constructor for the field settings edit page.
  1429. *
  1430. * @see field_ui_field_settings_form_submit()
  1431. * @ingroup forms
  1432. */
  1433. function field_ui_field_settings_form($form, &$form_state, $instance) {
  1434. $bundle = $instance['bundle'];
  1435. $entity_type = $instance['entity_type'];
  1436. $field = field_info_field($instance['field_name']);
  1437. drupal_set_title($instance['label']);
  1438. $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>';
  1439. // Create a form structure for the field values.
  1440. $form['field'] = array(
  1441. '#type' => 'fieldset',
  1442. '#title' => t('Field settings'),
  1443. '#description' => $description,
  1444. '#tree' => TRUE,
  1445. );
  1446. // See if data already exists for this field.
  1447. // If so, prevent changes to the field settings.
  1448. $has_data = field_has_data($field);
  1449. if ($has_data) {
  1450. $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'];
  1451. }
  1452. // Build the non-configurable field values.
  1453. $form['field']['field_name'] = array('#type' => 'value', '#value' => $field['field_name']);
  1454. $form['field']['type'] = array('#type' => 'value', '#value' => $field['type']);
  1455. $form['field']['module'] = array('#type' => 'value', '#value' => $field['module']);
  1456. $form['field']['active'] = array('#type' => 'value', '#value' => $field['active']);
  1457. // Add settings provided by the field module. The field module is
  1458. // responsible for not returning settings that cannot be changed if
  1459. // the field already has data.
  1460. $form['field']['settings'] = array();
  1461. $additions = module_invoke($field['module'], 'field_settings_form', $field, $instance, $has_data);
  1462. if (is_array($additions)) {
  1463. $form['field']['settings'] = $additions;
  1464. }
  1465. if (empty($form['field']['settings'])) {
  1466. $form['field']['settings'] = array(
  1467. '#markup' => t('%field has no field settings.', array('%field' => $instance['label'])),
  1468. );
  1469. }
  1470. $form['#entity_type'] = $entity_type;
  1471. $form['#bundle'] = $bundle;
  1472. $form['actions'] = array('#type' => 'actions');
  1473. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save field settings'));
  1474. return $form;
  1475. }
  1476. /**
  1477. * Form submission handler for field_ui_field_settings_form().
  1478. */
  1479. function field_ui_field_settings_form_submit($form, &$form_state) {
  1480. $form_values = $form_state['values'];
  1481. $field_values = $form_values['field'];
  1482. // Merge incoming form values into the existing field.
  1483. $field = field_info_field($field_values['field_name']);
  1484. $entity_type = $form['#entity_type'];
  1485. $bundle = $form['#bundle'];
  1486. $instance = field_info_instance($entity_type, $field['field_name'], $bundle);
  1487. // Update the field.
  1488. $field = array_merge($field, $field_values);
  1489. try {
  1490. field_update_field($field);
  1491. drupal_set_message(t('Updated field %label field settings.', array('%label' => $instance['label'])));
  1492. $form_state['redirect'] = field_ui_next_destination($entity_type, $bundle);
  1493. }
  1494. catch (Exception $e) {
  1495. drupal_set_message(t('Attempt to update field %label failed: %message.', array('%label' => $instance['label'], '%message' => $e->getMessage())), 'error');
  1496. }
  1497. }
  1498. /**
  1499. * Form constructor for the widget selection form.
  1500. *
  1501. * Path: BUNDLE_ADMIN_PATH/fields/%field/widget-type, where BUNDLE_ADMIN_PATH is
  1502. * the path stored in the ['admin']['info'] property in the return value of
  1503. * hook_entity_info().
  1504. *
  1505. * @see field_ui_menu()
  1506. * @see field_ui_widget_type_form_submit()
  1507. * @ingroup forms
  1508. */
  1509. function field_ui_widget_type_form($form, &$form_state, $instance) {
  1510. drupal_set_title($instance['label']);
  1511. $bundle = $instance['bundle'];
  1512. $entity_type = $instance['entity_type'];
  1513. $field_name = $instance['field_name'];
  1514. $field = field_info_field($field_name);
  1515. $field_type = field_info_field_types($field['type']);
  1516. $widget_type = field_info_widget_types($instance['widget']['type']);
  1517. $bundles = field_info_bundles();
  1518. $bundle_label = $bundles[$entity_type][$bundle]['label'];
  1519. $form = array(
  1520. '#bundle' => $bundle,
  1521. '#entity_type' => $entity_type,
  1522. '#field_name' => $field_name,
  1523. );
  1524. $form['basic'] = array(
  1525. '#type' => 'fieldset',
  1526. '#title' => t('Change widget'),
  1527. );
  1528. $form['basic']['widget_type'] = array(
  1529. '#type' => 'select',
  1530. '#title' => t('Widget type'),
  1531. '#required' => TRUE,
  1532. '#options' => field_ui_widget_type_options($field['type']),
  1533. '#default_value' => $instance['widget']['type'],
  1534. '#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)),
  1535. );
  1536. $form['actions'] = array('#type' => 'actions');
  1537. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Continue'));
  1538. $form['#validate'] = array();
  1539. $form['#submit'] = array('field_ui_widget_type_form_submit');
  1540. return $form;
  1541. }
  1542. /**
  1543. * Form submission handler for field_ui_widget_type_form().
  1544. */
  1545. function field_ui_widget_type_form_submit($form, &$form_state) {
  1546. $form_values = $form_state['values'];
  1547. $bundle = $form['#bundle'];
  1548. $entity_type = $form['#entity_type'];
  1549. $field_name = $form['#field_name'];
  1550. // Retrieve the stored instance settings to merge with the incoming values.
  1551. $instance = field_read_instance($entity_type, $field_name, $bundle);
  1552. // Set the right module information.
  1553. $widget_type = field_info_widget_types($form_values['widget_type']);
  1554. $widget_module = $widget_type['module'];
  1555. $instance['widget']['type'] = $form_values['widget_type'];
  1556. $instance['widget']['module'] = $widget_module;
  1557. try {
  1558. field_update_instance($instance);
  1559. drupal_set_message(t('Changed the widget for field %label.', array('%label' => $instance['label'])));
  1560. }
  1561. catch (Exception $e) {
  1562. drupal_set_message(t('There was a problem changing the widget for field %label.', array('%label' => $instance['label'])), 'error');
  1563. }
  1564. $form_state['redirect'] = field_ui_next_destination($entity_type, $bundle);
  1565. }
  1566. /**
  1567. * Form constructor for removing a field instance from a bundle.
  1568. *
  1569. * @see field_ui_field_delete_form_submit()
  1570. * @ingroup forms
  1571. */
  1572. function field_ui_field_delete_form($form, &$form_state, $instance) {
  1573. $bundle = $instance['bundle'];
  1574. $entity_type = $instance['entity_type'];
  1575. $field = field_info_field($instance['field_name']);
  1576. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  1577. $form['entity_type'] = array('#type' => 'value', '#value' => $entity_type);
  1578. $form['bundle'] = array('#type' => 'value', '#value' => $bundle);
  1579. $form['field_name'] = array('#type' => 'value', '#value' => $field['field_name']);
  1580. $output = confirm_form($form,
  1581. t('Are you sure you want to delete the field %field?', array('%field' => $instance['label'])),
  1582. $admin_path . '/fields',
  1583. t('If you have any content left in this field, it will be lost. This action cannot be undone.'),
  1584. t('Delete'), t('Cancel'),
  1585. 'confirm'
  1586. );
  1587. if ($field['locked']) {
  1588. unset($output['actions']['submit']);
  1589. $output['description']['#markup'] = t('This field is <strong>locked</strong> and cannot be deleted.');
  1590. }
  1591. return $output;
  1592. }
  1593. /**
  1594. * Form submission handler for field_ui_field_delete_form().
  1595. *
  1596. * Removes a field instance from a bundle. If the field has no more instances,
  1597. * it will be marked as deleted too.
  1598. */
  1599. function field_ui_field_delete_form_submit($form, &$form_state) {
  1600. $form_values = $form_state['values'];
  1601. $field_name = $form_values['field_name'];
  1602. $bundle = $form_values['bundle'];
  1603. $entity_type = $form_values['entity_type'];
  1604. $field = field_info_field($field_name);
  1605. $instance = field_info_instance($entity_type, $field_name, $bundle);
  1606. $bundles = field_info_bundles();
  1607. $bundle_label = $bundles[$entity_type][$bundle]['label'];
  1608. if (!empty($bundle) && $field && !$field['locked'] && $form_values['confirm']) {
  1609. field_delete_instance($instance);
  1610. drupal_set_message(t('The field %field has been deleted from the %type content type.', array('%field' => $instance['label'], '%type' => $bundle_label)));
  1611. }
  1612. else {
  1613. drupal_set_message(t('There was a problem removing the %field from the %type content type.', array('%field' => $instance['label'], '%type' => $bundle_label)), 'error');
  1614. }
  1615. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  1616. $form_state['redirect'] = field_ui_get_destinations(array($admin_path . '/fields'));
  1617. // Fields are purged on cron. However field module prevents disabling modules
  1618. // when field types they provided are used in a field until it is fully
  1619. // purged. In the case that a field has minimal or no content, a single call
  1620. // to field_purge_batch() will remove it from the system. Call this with a
  1621. // low batch limit to avoid administrators having to wait for cron runs when
  1622. // removing instances that meet this criteria.
  1623. field_purge_batch(10);
  1624. }
  1625. /**
  1626. * Form constructor for the field instance settings form.
  1627. *
  1628. * @see field_ui_field_edit_form_validate()
  1629. * @see field_ui_field_edit_form_submit()
  1630. * @ingroup forms
  1631. */
  1632. function field_ui_field_edit_form($form, &$form_state, $instance) {
  1633. $bundle = $instance['bundle'];
  1634. $entity_type = $instance['entity_type'];
  1635. $field = field_info_field($instance['field_name']);
  1636. drupal_set_title($instance['label']);
  1637. $form['#field'] = $field;
  1638. $form['#instance'] = $instance;
  1639. if (!empty($field['locked'])) {
  1640. $form['locked'] = array(
  1641. '#markup' => t('The field %field is locked and cannot be edited.', array('%field' => $instance['label'])),
  1642. );
  1643. return $form;
  1644. }
  1645. $field_type = field_info_field_types($field['type']);
  1646. $widget_type = field_info_widget_types($instance['widget']['type']);
  1647. $bundles = field_info_bundles();
  1648. // Create a form structure for the instance values.
  1649. $form['instance'] = array(
  1650. '#tree' => TRUE,
  1651. '#type' => 'fieldset',
  1652. '#title' => t('%type settings', array('%type' => $bundles[$entity_type][$bundle]['label'])),
  1653. '#description' => t('These settings apply only to the %field field when used in the %type type.', array(
  1654. '%field' => $instance['label'],
  1655. '%type' => $bundles[$entity_type][$bundle]['label'],
  1656. )),
  1657. // Ensure field_ui_field_edit_instance_pre_render() gets called in addition
  1658. // to, not instead of, the #pre_render function(s) needed by all fieldsets.
  1659. '#pre_render' => array_merge(array('field_ui_field_edit_instance_pre_render'), element_info_property('fieldset', '#pre_render', array())),
  1660. );
  1661. // Build the non-configurable instance values.
  1662. $form['instance']['field_name'] = array(
  1663. '#type' => 'value',
  1664. '#value' => $instance['field_name'],
  1665. );
  1666. $form['instance']['entity_type'] = array(
  1667. '#type' => 'value',
  1668. '#value' => $entity_type,
  1669. );
  1670. $form['instance']['bundle'] = array(
  1671. '#type' => 'value',
  1672. '#value' => $bundle,
  1673. );
  1674. $form['instance']['widget']['weight'] = array(
  1675. '#type' => 'value',
  1676. '#value' => !empty($instance['widget']['weight']) ? $instance['widget']['weight'] : 0,
  1677. );
  1678. // Build the configurable instance values.
  1679. $form['instance']['label'] = array(
  1680. '#type' => 'textfield',
  1681. '#title' => t('Label'),
  1682. '#default_value' => !empty($instance['label']) ? $instance['label'] : $field['field_name'],
  1683. '#required' => TRUE,
  1684. '#weight' => -20,
  1685. );
  1686. $form['instance']['required'] = array(
  1687. '#type' => 'checkbox',
  1688. '#title' => t('Required field'),
  1689. '#default_value' => !empty($instance['required']),
  1690. '#weight' => -10,
  1691. );
  1692. $form['instance']['description'] = array(
  1693. '#type' => 'textarea',
  1694. '#title' => t('Help text'),
  1695. '#default_value' => !empty($instance['description']) ? $instance['description'] : '',
  1696. '#rows' => 5,
  1697. '#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())),
  1698. '#weight' => -5,
  1699. );
  1700. // Build the widget component of the instance.
  1701. $form['instance']['widget']['type'] = array(
  1702. '#type' => 'value',
  1703. '#value' => $instance['widget']['type'],
  1704. );
  1705. $form['instance']['widget']['module'] = array(
  1706. '#type' => 'value',
  1707. '#value' => $widget_type['module'],
  1708. );
  1709. $form['instance']['widget']['active'] = array(
  1710. '#type' => 'value',
  1711. '#value' => !empty($field['instance']['widget']['active']) ? 1 : 0,
  1712. );
  1713. // Add additional field instance settings from the field module.
  1714. $additions = module_invoke($field['module'], 'field_instance_settings_form', $field, $instance);
  1715. if (is_array($additions)) {
  1716. $form['instance']['settings'] = $additions;
  1717. }
  1718. // Add additional widget settings from the widget module.
  1719. $additions = module_invoke($widget_type['module'], 'field_widget_settings_form', $field, $instance);
  1720. if (is_array($additions)) {
  1721. $form['instance']['widget']['settings'] = $additions;
  1722. $form['instance']['widget']['active']['#value'] = 1;
  1723. }
  1724. // Add handling for default value if not provided by any other module.
  1725. if (field_behaviors_widget('default value', $instance) == FIELD_BEHAVIOR_DEFAULT && empty($instance['default_value_function'])) {
  1726. $form['instance']['default_value_widget'] = field_ui_default_value_widget($field, $instance, $form, $form_state);
  1727. }
  1728. $has_data = field_has_data($field);
  1729. if ($has_data) {
  1730. $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>';
  1731. }
  1732. else {
  1733. $description = '<p>' . t('These settings apply to the %field field everywhere it is used.', array('%field' => $instance['label'])) . '</p>';
  1734. }
  1735. // Create a form structure for the field values.
  1736. $form['field'] = array(
  1737. '#type' => 'fieldset',
  1738. '#title' => t('%field field settings', array('%field' => $instance['label'])),
  1739. '#description' => $description,
  1740. '#tree' => TRUE,
  1741. );
  1742. // Build the configurable field values.
  1743. $description = t('Maximum number of values users can enter for this field.');
  1744. if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_DEFAULT) {
  1745. $description .= '<br/>' . t("'Unlimited' will provide an 'Add more' button so the users can add as many values as they like.");
  1746. }
  1747. $form['field']['cardinality'] = array(
  1748. '#type' => 'select',
  1749. '#title' => t('Number of values'),
  1750. '#options' => array(FIELD_CARDINALITY_UNLIMITED => t('Unlimited')) + drupal_map_assoc(range(1, 10)),
  1751. '#default_value' => $field['cardinality'],
  1752. '#description' => $description,
  1753. );
  1754. // Add additional field type settings. The field type module is
  1755. // responsible for not returning settings that cannot be changed if
  1756. // the field already has data.
  1757. $additions = module_invoke($field['module'], 'field_settings_form', $field, $instance, $has_data);
  1758. if (is_array($additions)) {
  1759. $form['field']['settings'] = $additions;
  1760. }
  1761. $form['actions'] = array('#type' => 'actions');
  1762. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save settings'));
  1763. return $form;
  1764. }
  1765. /**
  1766. * Pre-render function for field instance settings.
  1767. *
  1768. * Combines the instance, widget, and other settings into a single fieldset so
  1769. * that elements within each group can be shown at different weights as if they
  1770. * all had the same parent.
  1771. */
  1772. function field_ui_field_edit_instance_pre_render($element) {
  1773. // Merge the widget settings into the main form.
  1774. if (isset($element['widget']['settings'])) {
  1775. foreach (element_children($element['widget']['settings']) as $key) {
  1776. $element['widget_' . $key] = $element['widget']['settings'][$key];
  1777. }
  1778. unset($element['widget']['settings']);
  1779. }
  1780. // Merge the instance settings into the main form.
  1781. if (isset($element['settings'])) {
  1782. foreach (element_children($element['settings']) as $key) {
  1783. $element['instance_' . $key] = $element['settings'][$key];
  1784. }
  1785. unset($element['settings']);
  1786. }
  1787. return $element;
  1788. }
  1789. /**
  1790. * Builds the default value fieldset for a given field instance.
  1791. */
  1792. function field_ui_default_value_widget($field, $instance, &$form, &$form_state) {
  1793. $field_name = $field['field_name'];
  1794. $element = array(
  1795. '#type' => 'fieldset',
  1796. '#title' => t('Default value'),
  1797. '#collapsible' => FALSE,
  1798. '#tree' => TRUE,
  1799. '#description' => t('The default value for this field, used when creating new content.'),
  1800. // Stick to an empty 'parents' on this form in order not to breaks widgets
  1801. // that do not use field_widget_[field|instance]() and still access
  1802. // $form_state['field'] directly.
  1803. '#parents' => array(),
  1804. );
  1805. // Insert the widget.
  1806. $items = $instance['default_value'];
  1807. $instance['required'] = FALSE;
  1808. $instance['description'] = '';
  1809. // @todo Allow multiple values (requires more work on 'add more' JS handler).
  1810. $element += field_default_form($instance['entity_type'], NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state, 0);
  1811. return $element;
  1812. }
  1813. /**
  1814. * Form validation handler for field_ui_field_edit_form().
  1815. *
  1816. * @see field_ui_field_edit_form_submit().
  1817. */
  1818. function field_ui_field_edit_form_validate($form, &$form_state) {
  1819. // Take the incoming values as the $instance definition, so that the 'default
  1820. // value' gets validated using the instance settings being submitted.
  1821. $instance = $form_state['values']['instance'];
  1822. $field_name = $instance['field_name'];
  1823. if (isset($form['instance']['default_value_widget'])) {
  1824. $element = $form['instance']['default_value_widget'];
  1825. $field_state = field_form_get_state($element['#parents'], $field_name, LANGUAGE_NONE, $form_state);
  1826. $field = $field_state['field'];
  1827. // Extract the 'default value'.
  1828. $items = array();
  1829. field_default_extract_form_values(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state);
  1830. // Validate the value and report errors.
  1831. $errors = array();
  1832. $function = $field['module'] . '_field_validate';
  1833. if (function_exists($function)) {
  1834. $function(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $errors);
  1835. }
  1836. if (isset($errors[$field_name][LANGUAGE_NONE])) {
  1837. // Store reported errors in $form_state.
  1838. $field_state['errors'] = $errors[$field_name][LANGUAGE_NONE];
  1839. field_form_set_state($element['#parents'], $field_name, LANGUAGE_NONE, $form_state, $field_state);
  1840. // Assign reported errors to the correct form element.
  1841. field_default_form_errors(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state);
  1842. }
  1843. }
  1844. }
  1845. /**
  1846. * Form submission handler for field_ui_field_edit_form().
  1847. *
  1848. * @see field_ui_field_edit_form_validate().
  1849. */
  1850. function field_ui_field_edit_form_submit($form, &$form_state) {
  1851. $instance = $form_state['values']['instance'];
  1852. $field = $form_state['values']['field'];
  1853. // Update any field settings that have changed.
  1854. $field_source = field_info_field($instance['field_name']);
  1855. $field = array_merge($field_source, $field);
  1856. try {
  1857. field_update_field($field);
  1858. }
  1859. catch (Exception $e) {
  1860. drupal_set_message(t('Attempt to update field %label failed: %message.', array('%label' => $instance['label'], '%message' => $e->getMessage())), 'error');
  1861. return;
  1862. }
  1863. // Handle the default value.
  1864. if (isset($form['instance']['default_value_widget'])) {
  1865. $element = $form['instance']['default_value_widget'];
  1866. // Extract field values.
  1867. $items = array();
  1868. field_default_extract_form_values(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state);
  1869. field_default_submit(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $element, $form_state);
  1870. $instance['default_value'] = $items ? $items : NULL;
  1871. }
  1872. // Retrieve the stored instance settings to merge with the incoming values.
  1873. $instance_source = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
  1874. $instance = array_merge($instance_source, $instance);
  1875. field_update_instance($instance);
  1876. drupal_set_message(t('Saved %label configuration.', array('%label' => $instance['label'])));
  1877. $form_state['redirect'] = field_ui_next_destination($instance['entity_type'], $instance['bundle']);
  1878. }
  1879. /**
  1880. * Extracts next redirect path from an array of multiple destinations.
  1881. *
  1882. * @see field_ui_next_destination()
  1883. */
  1884. function field_ui_get_destinations($destinations) {
  1885. $path = array_shift($destinations);
  1886. $options = drupal_parse_url($path);
  1887. if ($destinations) {
  1888. $options['query']['destinations'] = $destinations;
  1889. }
  1890. return array($options['path'], $options);
  1891. }
  1892. /**
  1893. * Returns the next redirect path in a multipage sequence.
  1894. */
  1895. function field_ui_next_destination($entity_type, $bundle) {
  1896. $destinations = !empty($_REQUEST['destinations']) ? $_REQUEST['destinations'] : array();
  1897. if (!empty($destinations)) {
  1898. unset($_REQUEST['destinations']);
  1899. }
  1900. // Remove any external URLs.
  1901. $destinations = array_diff($destinations, array_filter($destinations, 'url_is_external'));
  1902. if ($destinations) {
  1903. return field_ui_get_destinations($destinations);
  1904. }
  1905. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  1906. return $admin_path . '/fields';
  1907. }