diff --git a/bean_admin_ui/bean_admin_ui.admin.inc b/bean_admin_ui/bean_admin_ui.admin.inc index 4ee92dc..3707e93 100644 --- a/bean_admin_ui/bean_admin_ui.admin.inc +++ b/bean_admin_ui/bean_admin_ui.admin.inc @@ -31,7 +31,7 @@ function bean_admin_ui_admin_page() { $row[] = array('data' => l(t('Delete'), 'admin/structure/block-types/manage/' . $bean_type->buildURL() . '/delete')); break; case 'Overridden': - $row[] = array('data' => l(t('Revert'), 'admin/structure/block-types/manage/' . $bean_type->buildURL() . '/delete')); + $row[] = array('data' => l(t('Revert'), 'admin/structure/block-types/manage/' . $bean_type->buildURL() . '/revert')); break; case 'Default': $row[] = array(); @@ -138,16 +138,26 @@ function bean_admin_ui_type_form($form, &$form_state, $bean_type = NULL) { '#disabled' => $disabled, ); - // This is a new bean type. - if (isset($plugin_info)) { - $form['actions']['delete'] = array( - '#type' => 'submit', - '#value' => t('Delete Block type'), - '#weight' => 45, - '#limit_validation_errors' => array(), - '#submit' => array('bean_type_form_submit'), - '#disabled' => $disabled, - ); + // Add Delete / Revert button if it's not a new bean type. + if (!empty($bean_type->type)) { + $export_status = $bean_type->getExportStatus(); + if ($export_status != 'Default') { + if ($export_status == 'Overridden') { + $op = 'revert'; + $value = t('Revert to defaults'); + } + else { + $op = 'delete'; + $value = t('Delete Block type'); + } + $form['actions'][$op] = array( + '#type' => 'submit', + '#name' => $op, + '#value' => $value, + '#weight' => 45, + '#limit_validation_errors' => array(), + ); + } } return $form; } @@ -156,34 +166,34 @@ function bean_admin_ui_type_form($form, &$form_state, $bean_type = NULL) { * Form API submit callback for the type form. */ function bean_admin_ui_type_form_submit(&$form, &$form_state) { - $bean_type = $form_state['values']['bean_type']; - $bean_type->type = $form_state['values']['name']; - $bean_type->setLabel($form_state['values']['label']); - $bean_type->setDescription($form_state['values']['description']); - $bean_type->save($form_state['values']['new']); - $form_state['redirect'] = 'admin/structure/block-types'; - ctools_include('export'); -} - -/** - * Form API submit callback for the delete button. - */ -function bean_admin_ui_type_form_submit_delete(&$form, &$form_state) { - $form_state['redirect'] = 'admin/structure/block-types/manage/' . $form_state['bean_type']->type . '/delete'; + $op = $form_state['clicked_button']['#name']; + if (in_array($op, array('revert', 'delete'))) { + $form_state['redirect'] = 'admin/structure/block-types/manage/' . $form['bean_type']['#value']->type . '/' . $op; + } + else { + $bean_type = $form_state['values']['bean_type']; + $bean_type->type = $form_state['values']['name']; + $bean_type->setLabel($form_state['values']['label']); + $bean_type->setDescription($form_state['values']['description']); + $bean_type->save($form_state['values']['new']); + $form_state['redirect'] = 'admin/structure/block-types'; + ctools_include('export'); + } } /** - * Menu callback; delete a single content type. + * Menu callback; Form builder to confirm delete/revert of a bean type. */ -function bean_admin_ui_type_delete_confirm($form, &$form_state, $type) { +function bean_admin_ui_type_op_confirm($form, &$form_state, $op, $type) { + $form['op'] = array('#type' => 'value', '#value' => $op); $form['type'] = array('#type' => 'value', '#value' => $type); $form['name'] = array('#type' => 'value', '#value' => $type->getLabel()); $caption = ''; - if (!method_exists($type, 'getExportStatus') || $type->getExportStatus() == 'Normal') { + if ($op == 'delete') { $message = t('Are you sure you want to delete the block type %type?', array('%type' => $type->getLabel())); $num_beans = db_query("SELECT COUNT(*) FROM {bean} WHERE type = :type", array(':type' => $type->type))->fetchField(); if ($num_beans) { - $caption .= '

' . format_plural($num_beans, '%type is used by 1 block on your site. If you remove this block type, you will not be able to edit the %type blocks and it may not display correctly.', '%type is used by @count pieces of content on your site. If you remove %type, you will not be able to edit the %type content and it may not display correctly.', array('%type' => $type->getLabel())) . '

'; + $caption .= '

' . format_plural($num_beans, '%type is used by 1 block on your site that is going to be removed along with %type.', '%type is used by @count blocks on your site that are going to be removed along %type.', array('%type' => $type->getLabel())) . '

'; } $action = t('Delete'); } @@ -196,7 +206,7 @@ function bean_admin_ui_type_delete_confirm($form, &$form_state, $type) { return confirm_form($form, $message, 'admin/structure/block-types', $caption, $action); } -function bean_admin_ui_type_delete_confirm_submit($form, &$form_state) { - $form_state['values']['type']->delete(); +function bean_admin_ui_type_op_confirm_submit($form, &$form_state) { + call_user_func(array($form_state['values']['type'], $form['op']['#value'])); $form_state['redirect'] = 'admin/structure/block-types'; } diff --git a/bean_admin_ui/bean_admin_ui.module b/bean_admin_ui/bean_admin_ui.module index c42370d..da45ea0 100644 --- a/bean_admin_ui/bean_admin_ui.module +++ b/bean_admin_ui/bean_admin_ui.module @@ -51,7 +51,13 @@ function bean_admin_ui_menu() { ); $items['admin/structure/block-types/manage/%bean_type/delete'] = array( 'title' => 'Delete', - 'page arguments' => array('bean_admin_ui_type_delete_confirm', 4), + 'page arguments' => array('bean_admin_ui_type_op_confirm', 'delete', 4), + 'access arguments' => array('administer bean types'), + 'file' => 'bean_admin_ui.admin.inc', + ); + $items['admin/structure/block-types/manage/%bean_type/revert'] = array( + 'title' => 'Revert', + 'page arguments' => array('bean_admin_ui_type_op_confirm', 'revert', 4), 'access arguments' => array('administer bean types'), 'file' => 'bean_admin_ui.admin.inc', ); diff --git a/bean_admin_ui/plugins/custom.inc b/bean_admin_ui/plugins/custom.inc index d8c417c..6922176 100644 --- a/bean_admin_ui/plugins/custom.inc +++ b/bean_admin_ui/plugins/custom.inc @@ -13,18 +13,25 @@ class BeanCustom extends BeanPlugin { * Delete the record from the database. */ public function delete() { - // Delete all beans belonging to this bean type if we are removing - // the actual bean. - if ($this->getExportStatus() == 'Normal') { - db_delete('bean') + // Delete all beans belonging to this bean type. + db_delete('bean') ->condition('type', $this->type) ->execute(); - } - db_delete('bean_type') - ->condition('name', $this->type) - ->execute(); + + // Delete the bean type and attached fields. + ctools_export_crud_delete('bean_type', $this->type); field_attach_delete_bundle('bean', $this->type); - drupal_flush_all_caches(); + + // Clear bean caches. + bean_reset(); + } + + /** + * Revert the bean type to code defaults. + */ + public function revert() { + ctools_export_crud_delete('bean_type', $this->type); + bean_reset(); } /**