field_ui.admin.inc 77 KB

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