profile2.admin.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @file
  4. * Profile type editing UI.
  5. */
  6. /**
  7. * UI controller.
  8. */
  9. class Profile2TypeUIController extends EntityDefaultUIController {
  10. /**
  11. * Overrides hook_menu() defaults.
  12. */
  13. public function hook_menu() {
  14. $items = parent::hook_menu();
  15. $items[$this->path]['description'] = 'Manage profiles, including fields.';
  16. return $items;
  17. }
  18. }
  19. /**
  20. * Generates the profile type editing form.
  21. */
  22. function profile2_type_form($form, &$form_state, $profile_type, $op = 'edit') {
  23. if ($op == 'clone') {
  24. $profile_type->label .= ' (cloned)';
  25. $profile_type->type = '';
  26. }
  27. $form['label'] = array(
  28. '#title' => t('Label'),
  29. '#type' => 'textfield',
  30. '#default_value' => $profile_type->label,
  31. '#description' => t('The human-readable name of this profile type.'),
  32. '#required' => TRUE,
  33. '#size' => 30,
  34. );
  35. // Machine-readable type name.
  36. $form['type'] = array(
  37. '#type' => 'machine_name',
  38. '#default_value' => isset($profile_type->type) ? $profile_type->type : '',
  39. '#maxlength' => 32,
  40. '#disabled' => $profile_type->isLocked() && $op != 'clone',
  41. '#machine_name' => array(
  42. 'exists' => 'profile2_get_types',
  43. 'source' => array('label'),
  44. ),
  45. '#description' => t('A unique machine-readable name for this profile type. It must only contain lowercase letters, numbers, and underscores.'),
  46. );
  47. $form['data']['#tree'] = TRUE;
  48. $form['data']['registration'] = array(
  49. '#type' => 'checkbox',
  50. '#title' => t('Show during user account registration.'),
  51. '#default_value' => !empty($profile_type->data['registration']),
  52. );
  53. $user_roles = user_roles();
  54. // Exclude anonymous user role.
  55. unset($user_roles[DRUPAL_ANONYMOUS_RID]);
  56. $form['data']['roles'] = array(
  57. '#type' => 'checkboxes',
  58. '#title' => t('Roles'),
  59. '#description' => t('Check user roles that should have this profile.'),
  60. '#options' => $user_roles,
  61. '#default_value' => !empty($profile_type->data['roles']) ? $profile_type->data['roles'] : array_keys($user_roles)
  62. );
  63. $form['actions'] = array('#type' => 'actions');
  64. $form['actions']['submit'] = array(
  65. '#type' => 'submit',
  66. '#value' => t('Save profile type'),
  67. '#weight' => 40,
  68. );
  69. $form['weight'] = array(
  70. '#type' => 'weight',
  71. '#title' => t('Weight'),
  72. '#default_value' => $profile_type->weight,
  73. '#description' => t('When showing profiles, those with lighter (smaller) weights get listed before profiles with heavier (larger) weights.'),
  74. '#weight' => 10,
  75. );
  76. if (!$profile_type->isLocked() && $op != 'add' && $op != 'clone') {
  77. $form['actions']['delete'] = array(
  78. '#type' => 'submit',
  79. '#value' => t('Delete profile type'),
  80. '#weight' => 45,
  81. '#limit_validation_errors' => array(),
  82. '#submit' => array('profile2_type_form_submit_delete')
  83. );
  84. }
  85. return $form;
  86. }
  87. /**
  88. * Form API submit callback for the type form.
  89. */
  90. function profile2_type_form_submit(&$form, &$form_state) {
  91. $form_state['values']['data']['roles'] = array_filter($form_state['values']['data']['roles']);
  92. $profile_type = entity_ui_form_submit_build_entity($form, $form_state);
  93. // Save and go back.
  94. $profile_type->save();
  95. $form_state['redirect'] = 'admin/structure/profiles';
  96. }
  97. /**
  98. * Form API submit callback for the delete button.
  99. */
  100. function profile2_type_form_submit_delete(&$form, &$form_state) {
  101. $form_state['redirect'] = 'admin/structure/profiles/manage/' . $form_state['profile2_type']->type . '/delete';
  102. }