field_ui.admin.inc

  1. drupal
    1. 7 modules/field_ui/field_ui.admin.inc
    2. 8 core/modules/field_ui/field_ui.admin.inc

Administrative interface for custom field type creation.

Functions & methods

NameDescription
field_ui_default_value_widgetBuilds the default value fieldset for a given field instance.
field_ui_display_overview_formForm constructor for the field display settings for a given view mode.
field_ui_display_overview_form_submitForm submission handler for field_ui_display_overview_form().
field_ui_display_overview_multistep_jsAjax handler for multistep buttons on the 'Manage display' screen.
field_ui_display_overview_multistep_submitForm submission handler for buttons in field_ui_display_overview_form().
field_ui_display_overview_row_regionReturns the region to which a row in the 'Manage display' screen belongs.
field_ui_existing_field_optionsReturns an array of existing fields to be added to a bundle.
field_ui_fields_listMenu callback; lists all defined fields for quick reference.
field_ui_field_delete_formForm constructor for removing a field instance from a bundle.
field_ui_field_delete_form_submitForm submission handler for field_ui_field_delete_form().
field_ui_field_edit_formForm constructor for the field instance settings form.
field_ui_field_edit_form_submitForm submission handler for field_ui_field_edit_form().
field_ui_field_edit_form_validateForm validation handler for field_ui_field_edit_form().
field_ui_field_edit_instance_pre_renderPre-render function for field instance settings.
field_ui_field_overview_formForm constructor for the 'Manage fields' form of a bundle.
field_ui_field_overview_form_submitForm submission handler for field_ui_field_overview_form().
field_ui_field_overview_form_validateForm validation handler for field_ui_field_overview_form().
field_ui_field_overview_row_regionReturns the region to which a row in the 'Manage fields' screen belongs.
field_ui_field_settings_formForm constructor for the field settings edit page.
field_ui_field_settings_form_submitForm submission handler for field_ui_field_settings_form().
field_ui_field_type_optionsReturns an array of field_type options.
field_ui_formatter_optionsReturns an array of formatter options for a field type.
field_ui_get_destinationsExtracts next redirect path from an array of multiple destinations.
field_ui_inactive_messageDisplays a message listing the inactive fields of a given bundle.
field_ui_next_destinationReturns the next redirect path in a multipage sequence.
field_ui_table_pre_renderPre-render callback for field_ui_table elements.
field_ui_widget_type_formForm constructor for the widget selection form.
field_ui_widget_type_form_submitForm submission handler for field_ui_widget_type_form().
field_ui_widget_type_optionsReturns an array of widget type options for a field type.
theme_field_ui_tableReturns HTML for Field UI overview tables.
_field_ui_add_default_view_mode_settingsPopulates display settings for a new view mode from the default view mode.
_field_ui_field_name_existsRender API callback: Checks if a field machine name is taken.
_field_ui_field_overview_form_validate_add_existingValidates the 'add existing field' row of field_ui_field_overview_form().
_field_ui_field_overview_form_validate_add_newValidates the 'add new field' row of field_ui_field_overview_form().
_field_ui_reduce_orderDetermines the rendering order of an array representing a tree.

File

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