title.admin.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 submissions.
  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 attachment.
  66. */
  67. function title_admin_settings_form() {
  68. $form['tabs'] = array(
  69. '#type' => 'vertical_tabs',
  70. );
  71. $form['settings']['title_general'] = array(
  72. '#type' => 'fieldset',
  73. '#collapsible' => TRUE,
  74. '#collapsed' => TRUE,
  75. '#tree' => TRUE,
  76. '#group' => 'tabs',
  77. '#title' => t('General'),
  78. );
  79. $general = variable_get('title_general', array('maxlength' => 255));
  80. $form['settings']['title_general']['maxlength'] = array(
  81. '#type' => 'textfield',
  82. '#title' => t('Default maximum field length'),
  83. '#description' => t('The default maximum length for the title field. This setting will have no effect once a title field for a specific entity type has been created, because the maximum field length is a global field setting and the databases tables will already have been created with a specific length.'),
  84. '#element_validate' => array('element_validate_integer_positive'),
  85. '#default_value' => $general['maxlength'],
  86. );
  87. foreach (entity_get_info() as $entity_type => $info) {
  88. if (empty($info['field replacement'])) {
  89. continue;
  90. }
  91. $form['settings']['title_' . $entity_type] = array(
  92. '#type' => 'fieldset',
  93. '#collapsible' => TRUE,
  94. '#collapsed' => TRUE,
  95. '#tree' => TRUE,
  96. '#group' => 'tabs',
  97. '#title' => check_plain($info['label']),
  98. );
  99. $options = array();
  100. foreach (array_keys($info['field replacement']) as $replacement) {
  101. $options[$replacement] = drupal_ucfirst($replacement);
  102. }
  103. $default = variable_get('title_' . $entity_type, array());
  104. $form['settings']['title_' . $entity_type]['auto_attach'] = array(
  105. '#type' => 'checkboxes',
  106. '#title' => t('Automatic field replacement'),
  107. '#options' => $options,
  108. '#default_value' => !empty($default['auto_attach']) ? $default['auto_attach'] : array(),
  109. '#description' => t('Automatically replace the selected field(s) when creating a new bundle.'),
  110. );
  111. $form['settings']['title_' . $entity_type]['hide_label'] = _title_hide_label_widget($default, $info['label']);
  112. }
  113. return system_settings_form($form);
  114. }