title.admin.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the Title module.
  5. */
  6. /**
  7. * Provide settings to enable title field.
  8. */
  9. function title_form_field_ui_overview(&$form, &$form_state) {
  10. $entity_info = entity_get_info($form['#entity_type']);
  11. if (!empty($entity_info['field replacement'])) {
  12. $field_replacement_info = $entity_info['field replacement'];
  13. $admin_path = _field_ui_bundle_admin_path($form['#entity_type'], $form['#bundle']);
  14. foreach (element_children($form['fields']) as $field_name) {
  15. if (isset($field_replacement_info[$field_name])) {
  16. $form['fields'][$field_name]['delete'] = array(
  17. '#type' => 'link',
  18. '#title' => t('replace'),
  19. '#href' => $admin_path . '/fields/replace/' . $field_name,
  20. '#options' => array('attributes' => array('title' => t('Replace %field with a customizable field instance that can be translated.', array('%field' => $field_name)))),
  21. );
  22. }
  23. }
  24. }
  25. }
  26. /**
  27. * Generate a field replacement form.
  28. */
  29. function title_field_replacement_form($form, $form_state, $entity_type, $bundle, $field_name) {
  30. $bundle_name = field_extract_bundle($entity_type, $bundle);
  31. $entity_info = entity_get_info($entity_type);
  32. $info = $entity_info['field replacement'][$field_name];
  33. $instance = field_info_instance($entity_type, $info['field']['field_name'], $bundle_name);
  34. $enabled = !empty($instance);
  35. $form['#entity_type'] = $entity_type;
  36. $form['#bundle'] = $bundle_name;
  37. $form['#field_name'] = $field_name;
  38. $form['enabled'] = array(
  39. '#type' => 'checkbox',
  40. '#title' => t('Replace %field with a field instance', array('%field' => $field_name)),
  41. '#description' => t('If this is enabled the %field will be replaced with a customizable field that can be translated.', array('%field' => $field_name)),
  42. '#default_value' => $enabled,
  43. '#disabled' => $enabled,
  44. );
  45. $form['actions'] = array('#type' => 'actions');
  46. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save settings'));
  47. return $form;
  48. }
  49. /**
  50. * Process field replacement form subissions.
  51. */
  52. function title_field_replacement_form_submit($form, &$form_state) {
  53. if ($form_state['values']['enabled'] != $form['enabled']['#default_value']) {
  54. if (title_field_replacement_toggle($form['#entity_type'], $form['#bundle'], $form['#field_name'])) {
  55. drupal_set_message(t('%field replaced with a field instance.', array('%field' => $form['#field_name'])));
  56. title_field_replacement_batch_set($form['#entity_type'], $form['#bundle'], $form['#field_name']);
  57. }
  58. else {
  59. drupal_set_message(t('Field replacement removed.'));
  60. }
  61. }
  62. $form_state['redirect'] = _field_ui_bundle_admin_path($form['#entity_type'], $form['#bundle']) . '/fields';
  63. }
  64. /**
  65. * Form settings for automated title_field attachement.
  66. */
  67. function title_admin_settings_form() {
  68. $form['tabs'] = array(
  69. '#type' => 'vertical_tabs',
  70. );
  71. foreach (entity_get_info() as $entity_type => $info) {
  72. if (empty($info['field replacement'])) {
  73. continue;
  74. }
  75. $form['settings']['title_' . $entity_type] = array(
  76. '#type' => 'fieldset',
  77. '#collapsible' => TRUE,
  78. '#collapsed' => TRUE,
  79. '#tree' => TRUE,
  80. '#group' => 'tabs',
  81. '#title' => $info['label'],
  82. );
  83. $options = array();
  84. foreach (array_keys($info['field replacement']) as $replacement) {
  85. $options[$replacement] = drupal_ucfirst($replacement);
  86. }
  87. $default = variable_get('title_' . $entity_type, array());
  88. $form['settings']['title_' . $entity_type]['auto_attach'] = array(
  89. '#type' => 'checkboxes',
  90. '#title' => t('Automatic field replacement'),
  91. '#options' => $options,
  92. '#default_value' => !empty($default['auto_attach']) ? $default['auto_attach'] : array(),
  93. '#description' => t('Automatically replace the selected field(s) when creating a new bundle.'),
  94. );
  95. $form['settings']['title_' . $entity_type]['hide_label'] = _title_hide_label_widget($default, $info['label']);
  96. }
  97. return system_settings_form($form);
  98. }