diff --git a/core/modules/config/tests/config_test/config_test.module b/core/modules/config/tests/config_test/config_test.module index 44df4da..696ad7d 100644 --- a/core/modules/config/tests/config_test/config_test.module +++ b/core/modules/config/tests/config_test/config_test.module @@ -57,24 +57,6 @@ function config_test_config_import_change($name, $new_config, $old_config) { } /** - * Implements hook_config_import_delete(). - */ -function config_test_config_import_delete($name, $new_config, $old_config) { - if (strpos($name, 'config_test.dynamic.') !== 0) { - return FALSE; - } - // Set a global value we can check in test code. - $GLOBALS['hook_config_import'] = __FUNCTION__; - - // @todo Make this less ugly. - list($entity_type) = explode('.', $name); - $entity_info = entity_get_info($entity_type); - $id = substr($name, strlen($entity_info['config prefix']) + 1); - config_test_delete($id); - return TRUE; -} - -/** * Implements hook_entity_info(). */ function config_test_entity_info() { @@ -83,6 +65,9 @@ function config_test_entity_info() { 'controller class' => 'Drupal\Core\Config\Entity\ConfigStorageController', 'entity class' => 'Drupal\config_test\ConfigTest', 'list controller class' => 'Drupal\Core\Config\Entity\ConfigEntityListController', + 'form controller class' => array( + 'default' => 'Drupal\config_test\ConfigTestFormController', + ), 'uri callback' => 'config_test_uri', 'config prefix' => 'config_test.dynamic', 'entity keys' => array( @@ -95,6 +80,19 @@ function config_test_entity_info() { } /** + * Implements hook_permission(). + */ +function config_test_permission() { + $permissions = array( + 'administer config_test content' => array( + 'title' => t('Administer entity_test content'), + 'description' => t('Manage config_test contnet'), + ) + ); + return $permissions; +} + +/** * Entity URI callback. * * @param Drupal\config_test\ConfigTest $config_test @@ -117,15 +115,14 @@ function config_test_menu() { ); $items['admin/structure/config_test/add'] = array( 'title' => 'Add test configuration', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('config_test_form'), + 'page callback' => 'config_test_add', 'access callback' => TRUE, 'type' => MENU_LOCAL_ACTION, ); $items['admin/structure/config_test/manage/%config_test'] = array( 'title' => 'Edit test configuration', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('config_test_form', 4), + 'page callback' => 'entity_get_form', + 'page arguments' => array(4), 'access callback' => TRUE, ); $items['admin/structure/config_test/manage/%config_test/edit'] = array( @@ -154,26 +151,6 @@ function config_test_load($id) { } /** - * Saves a ConfigTest object. - * - * @param Drupal\config_test\ConfigTest $config_test - * The ConfigTest object to save. - */ -function config_test_save(ConfigTest $config_test) { - return $config_test->save(); -} - -/** - * Deletes a ConfigTest object. - * - * @param string $id - * The ID of the ConfigTest object to delete. - */ -function config_test_delete($id) { - entity_delete_multiple('config_test', array($id)); -} - -/** * Page callback; Lists available ConfigTest objects. */ function config_test_list_page() { @@ -182,79 +159,11 @@ function config_test_list_page() { } /** - * Form constructor to add or edit a ConfigTest object. - * - * @param Drupal\config_test\ConfigTest $config_test - * (optional) An existing ConfigTest object to edit. If omitted, the form - * creates a new ConfigTest. + * Menu callback: displays the 'Add new config_test' form. */ -function config_test_form($form, &$form_state, ConfigTest $config_test = NULL) { - // Standard procedure for handling the entity argument in entity forms, taking - // potential form caching and rebuilds properly into account. - // @see http://drupal.org/node/1499596 - if (!isset($form_state['config_test'])) { - if (!isset($config_test)) { - $config_test = entity_create('config_test', array()); - } - $form_state['config_test'] = $config_test; - } - else { - $config_test = $form_state['config_test']; - } - - $form['label'] = array( - '#type' => 'textfield', - '#title' => 'Label', - '#default_value' => $config_test->label(), - '#required' => TRUE, - ); - $form['id'] = array( - '#type' => 'machine_name', - '#default_value' => $config_test->id(), - '#required' => TRUE, - '#machine_name' => array( - 'exists' => 'config_test_load', - // @todo Update form_process_machine_name() to use 'label' by default. - 'source' => array('label'), - ), - ); - $form['style'] = array( - '#type' => 'select', - '#title' => 'Image style', - '#options' => array(), - '#default_value' => $config_test->get('style'), - '#access' => FALSE, - ); - if (module_exists('image')) { - $form['style']['#access'] = TRUE; - $form['style']['#options'] = image_style_options(); - } - - $form['actions'] = array('#type' => 'actions'); - $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Save'); - - return $form; -} - -/** - * Form submission handler for config_test_form(). - */ -function config_test_form_submit($form, &$form_state) { - form_state_values_clean($form_state); - - $config_test = $form_state['config_test']; - entity_form_submit_build_entity('config_test', $config_test, $form, $form_state); - - $status = $config_test->save(); - - if ($status == SAVED_UPDATED) { - drupal_set_message(format_string('%label configuration has been updated.', array('%label' => $config_test->label()))); - } - else { - drupal_set_message(format_string('%label configuration has been created.', array('%label' => $config_test->label()))); - } - - $form_state['redirect'] = 'admin/structure/config_test'; +function config_test_add() { + $entity = entity_create('config_test', array()); + return entity_get_form($entity); } /** diff --git a/core/modules/config/tests/config_test/lib/Drupal/config_test/ConfigTestFormController.php b/core/modules/config/tests/config_test/lib/Drupal/config_test/ConfigTestFormController.php new file mode 100644 index 0000000..4e97032 --- /dev/null +++ b/core/modules/config/tests/config_test/lib/Drupal/config_test/ConfigTestFormController.php @@ -0,0 +1,105 @@ +getFormLangcode($form_state); + $name = $entity->get('name', $langcode); + $uid = $entity->get('uid', $langcode); + + $form['label'] = array( + '#type' => 'textfield', + '#title' => 'Label', + '#default_value' => $entity->label(), + '#required' => TRUE, + ); + $form['id'] = array( + '#type' => 'machine_name', + '#default_value' => $entity->id(), + '#required' => TRUE, + '#machine_name' => array( + 'exists' => 'config_test_load', + // @todo Update form_process_machine_name() to use 'label' by default. + 'source' => array('label'), + ), + ); + $form['style'] = array( + '#type' => 'select', + '#title' => 'Image style', + '#options' => array(), + '#default_value' => $entity->get('style'), + '#access' => FALSE, + ); + if (module_exists('image')) { + $form['style']['#access'] = TRUE; + $form['style']['#options'] = image_style_options(); + } + + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Save'); + + return $form; + } + + /** + * Overrides Drupal\Core\Entity\EntityFormController::submit(). + */ + public function submit(array $form, array &$form_state) { + form_state_values_clean($form_state); + $entity = parent::submit($form, $form_state); + + return $entity; + } + + /** + * Overrides Drupal\Core\Entity\EntityFormController::save(). + */ + public function save(array $form, array &$form_state) { + $entity = $this->getEntity($form_state); + $status = $entity->save(); + + if ($status == SAVED_UPDATED) { + drupal_set_message(t('%label configuration has been updated.', array('%label' => $entity->label()))); + } + else { + drupal_set_message(t('%label configuration has been created.', array('%label' => $entity->label()))); + } + + if ($entity->id()) { + $form_state['redirect'] = 'admin/structure/config_test/manage'; + } + else { + // Error on save. + drupal_set_message(t('The entity could not be saved.'), 'error'); + $form_state['rebuild'] = TRUE; + } + } + + /** + * Overrides Drupal\Core\Entity\EntityFormController::delete(). + */ + public function delete(array $form, array &$form_state) { + $entity = $this->getEntity($form_state); + $entity->delete(); + drupal_set_message(t('config_test @label has been deleted.', array('@label' => $entity->label()))); + $form_state['redirect'] = 'admin/structure/config_test/manage'; + } + +}