From 8355142ba278f518377dd5644d24f7524566c82e Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 10 Jun 2013 13:14:35 +0100 Subject: [PATCH] Issue #1987668 by damiankloip, jibran, h3rj4n, mparker17: Convert config_test() module to routes. --- .../tests/config_test/config_test.module | 116 ++++-------------- .../tests/config_test/config_test.routing.yml | 47 +++++++ .../config_test/ConfigTestController.php | 62 ++++++++++ .../config_test/ConfigTestInterface.php | 17 +++ .../config_test/Form/ConfigTestDeleteForm.php | 74 +++++++++++ .../Plugin/Core/Entity/ConfigTest.php | 3 +- 6 files changed, 223 insertions(+), 96 deletions(-) create mode 100644 core/modules/config/tests/config_test/lib/Drupal/config_test/ConfigTestController.php create mode 100644 core/modules/config/tests/config_test/lib/Drupal/config_test/ConfigTestInterface.php create mode 100644 core/modules/config/tests/config_test/lib/Drupal/config_test/Form/ConfigTestDeleteForm.php diff --git a/core/modules/config/tests/config_test/config_test.module b/core/modules/config/tests/config_test/config_test.module index a5426dd51f3..5367d648480 100644 --- a/core/modules/config/tests/config_test/config_test.module +++ b/core/modules/config/tests/config_test/config_test.module @@ -32,15 +32,11 @@ function config_test_menu() { ); $items['admin/structure/config_test/add'] = array( 'title' => 'Add test configuration', - 'page callback' => 'config_test_add_page', - 'access callback' => TRUE, - 'type' => MENU_LOCAL_ACTION, + 'route_name' => 'config_test_entity_add', + 'type' => MENU_SIBLING_LOCAL_TASK, ); $items['admin/structure/config_test/manage/%config_test'] = array( - 'title' => 'Edit test configuration', - 'page callback' => 'config_test_edit_page', - 'page arguments' => array(4), - 'access callback' => TRUE, + 'route_name' => 'config_test_entity_edit', ); $items['admin/structure/config_test/manage/%config_test/edit'] = array( 'title' => 'Edit', @@ -48,26 +44,34 @@ function config_test_menu() { ); $items['admin/structure/config_test/manage/%config_test/delete'] = array( 'title' => 'Delete', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('config_test_delete_form', 4), - 'access callback' => TRUE, - 'type' => MENU_LOCAL_TASK, + 'route_name' => 'config_test_entity_delete', ); $items['admin/structure/config_test/manage/%config_test/enable'] = array( 'title' => 'Enable', - 'page callback' => 'config_test_entity_enable', - 'page arguments' => array(4), - 'access callback' => TRUE, + 'route_name' => 'config_test_entity_enable', ); $items['admin/structure/config_test/manage/%config_test/disable'] = array( 'title' => 'Disable', - 'page callback' => 'config_test_entity_disable', - 'page arguments' => array(4), - 'access callback' => TRUE, + 'route_name' => 'config_test_entity_disable', ); return $items; } +/** + * Implements hook_local_actions() + */ +function config_test_local_actions() { + return array( + array( + 'route_name' => 'config_test_entity_add', + 'title' => t('Add test configuration'), + 'appears_on' => array( + 'config_test_list_page', + ), + ), + ); +} + /** * Loads a ConfigTest object. * @@ -78,58 +82,6 @@ function config_test_load($id) { return entity_load('config_test', $id); } -/** - * Page callback: Presents the ConfigTest creation form. - * - * @return array - * A form array as expected by drupal_render(). - */ -function config_test_add_page() { - $entity = entity_create('config_test', array()); - return entity_get_form($entity); -} - -/** - * Page callback: Presents the ConfigTest edit form. - * - * @param Drupal\config_test\Plugin\Core\Entity\ConfigTest $config_test - * The ConfigTest object to edit. - * - * @return array - * A form array as expected by drupal_render(). - */ -function config_test_edit_page(ConfigTest $config_test) { - drupal_set_title(format_string('Edit %label', array('%label' => $config_test->label())), PASS_THROUGH); - return entity_get_form($config_test); -} - -/** - * Form constructor to delete a ConfigTest object. - * - * @param Drupal\config_test\Plugin\Core\Entity\ConfigTest $config_test - * The ConfigTest object to delete. - */ -function config_test_delete_form($form, &$form_state, ConfigTest $config_test) { - $form_state['config_test'] = $config_test; - - $form['id'] = array('#type' => 'value', '#value' => $config_test->id()); - return confirm_form($form, - format_string('Are you sure you want to delete %label', array('%label' => $config_test->label())), - 'admin/structure/config_test', - NULL, - 'Delete' - ); -} - -/** - * Form submission handler for config_test_delete_form(). - */ -function config_test_delete_form_submit($form, &$form_state) { - $form_state['config_test']->delete(); - drupal_set_message(format_string('%label configuration has been deleted.', array('%label' => $form_state['config_test']->label()))); - $form_state['redirect'] = 'admin/structure/config_test'; -} - /** * Implements hook_cache_flush(). */ @@ -147,32 +99,6 @@ function config_test_config_test_create(ConfigTest $config_test) { } } -/** - * Enables a ConfigTest object. - * - * @param Drupal\config_test\ConfigTest $config_test - * The ConfigTest object to enable. - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse. - */ -function config_test_entity_enable(ConfigTest $config_test) { - $config_test->enable()->save(); - return new RedirectResponse(url('admin/structure/config_test', array('absolute' => TRUE))); -} - -/** - * Disables a ConfigTest object. - * - * @param Drupal\config_test\ConfigTest $config_test - * The ConfigTest object to disable. - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse. - */ -function config_test_entity_disable(ConfigTest $config_test) { - $config_test->disable()->save(); - return new RedirectResponse(url('admin/structure/config_test', array('absolute' => TRUE))); -} - /** * Implements hook_entity_info_alter(). */ diff --git a/core/modules/config/tests/config_test/config_test.routing.yml b/core/modules/config/tests/config_test/config_test.routing.yml index d939b3074e6..61221c65e64 100644 --- a/core/modules/config/tests/config_test/config_test.routing.yml +++ b/core/modules/config/tests/config_test/config_test.routing.yml @@ -5,3 +5,50 @@ config_test_list_page: entity_type: 'config_test' requirements: _access: 'TRUE' + +config_test_entity_add: + pattern: 'admin/structure/config_test/add' + defaults: + _entity_form: 'config_test' + requirements: + _access: 'TRUE' + +config_test_entity: + pattern: 'admin/structure/config_test/manage/{config_test}' + defaults: + _controller: '\Drupal\config_test\ConfigTestController::edit' + entity_type: 'config_test' + requirements: + _access: 'TRUE' + +config_test_entity_edit: + pattern: 'admin/structure/config_test/manage/{config_test}/edit' + defaults: + _controller: '\Drupal\config_test\ConfigTestController::edit' + entity_type: 'config_test' + requirements: + _access: 'TRUE' + +config_test_entity_enable: + pattern: 'admin/structure/config_test/manage/{config_test}/enable' + defaults: + _content: '\Drupal\config_test\ConfigTestController::enable' + entity_type: 'config_test' + requirements: + _access: 'TRUE' + +config_test_entity_disable: + pattern: 'admin/structure/config_test/manage/{config_test}/disable' + defaults: + _content: '\Drupal\config_test\ConfigTestController::disable' + entity_type: 'config_test' + requirements: + _access: 'TRUE' + +config_test_entity_delete: + pattern: 'admin/structure/config_test/manage/{config_test}/delete' + defaults: + _form: '\Drupal\config_test\Form\ConfigTestDeleteForm' + entity_type: 'config_test' + requirements: + _access: 'TRUE' diff --git a/core/modules/config/tests/config_test/lib/Drupal/config_test/ConfigTestController.php b/core/modules/config/tests/config_test/lib/Drupal/config_test/ConfigTestController.php new file mode 100644 index 00000000000..b7b64589270 --- /dev/null +++ b/core/modules/config/tests/config_test/lib/Drupal/config_test/ConfigTestController.php @@ -0,0 +1,62 @@ + $config_test->label())), PASS_THROUGH); + return entity_get_form($config_test); + } + + /** + * Enables a ConfigTest object. + * + * @param \Drupal\config_test\ConfigTest $config_test + * The ConfigTest object to enable. + * + * @return \Symfony\Component\HttpFoundation\RedirectResponse + * A redirect response to the config_test listing page. + */ + function enable(ConfigTest $config_test) { + $config_test->enable()->save(); + return new RedirectResponse(url('admin/structure/config_test', array('absolute' => TRUE))); + } + + /** + * Disables a ConfigTest object. + * + * @param \Drupal\config_test\ConfigTest $config_test + * The ConfigTest object to disable. + * + * @return \Symfony\Component\HttpFoundation\RedirectResponse + * A redirect response to the config_test listing page. + */ + function disable(ConfigTest $config_test) { + $config_test->disable()->save(); + return new RedirectResponse(url('admin/structure/config_test', array('absolute' => TRUE))); + } + +} diff --git a/core/modules/config/tests/config_test/lib/Drupal/config_test/ConfigTestInterface.php b/core/modules/config/tests/config_test/lib/Drupal/config_test/ConfigTestInterface.php new file mode 100644 index 00000000000..2752b9cfdf9 --- /dev/null +++ b/core/modules/config/tests/config_test/lib/Drupal/config_test/ConfigTestInterface.php @@ -0,0 +1,17 @@ + $this->configTest->label())); + } + + /** + * {@inheritdoc} + */ + protected function getConfirmText() { + return t('Delete'); + } + + /** + * {@inheritdoc} + */ + protected function getCancelPath() { + return 'admin/structure/config_test'; + } + + /** + * {@inheritdoc} + */ + public function getFormID() { + return 'config_test_delete_form'; + } + + /** + * Implements \Drupal\Drupal\Core\Form\ConfirmFormBase::buildForm(). + * + * @param \Drupal\config_test\ConfigTestInterface $config_test + * (optional) The ConfigTestInterface object to delete. + */ + public function buildForm(array $form, array &$form_state, ConfigTestInterface $config_test = NULL) { + $this->configTest = $config_test; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $this->configTest->delete(); + drupal_set_message(String::format('%label configuration has been deleted.', array('%label' => $this->configTest->label()))); + $form_state['redirect'] = 'admin/structure/config_test'; + } + +} diff --git a/core/modules/config/tests/config_test/lib/Drupal/config_test/Plugin/Core/Entity/ConfigTest.php b/core/modules/config/tests/config_test/lib/Drupal/config_test/Plugin/Core/Entity/ConfigTest.php index 72df0fa79a0..7afb306729e 100644 --- a/core/modules/config/tests/config_test/lib/Drupal/config_test/Plugin/Core/Entity/ConfigTest.php +++ b/core/modules/config/tests/config_test/lib/Drupal/config_test/Plugin/Core/Entity/ConfigTest.php @@ -10,6 +10,7 @@ namespace Drupal\config_test\Plugin\Core\Entity; use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Entity\Annotation\EntityType; use Drupal\Core\Annotation\Translation; +use Drupal\config_test\ConfigTestInterface; /** * Defines the ConfigTest configuration entity. @@ -35,7 +36,7 @@ use Drupal\Core\Annotation\Translation; * } * ) */ -class ConfigTest extends ConfigEntityBase { +class ConfigTest extends ConfigEntityBase implements ConfigTestInterface { /** * The machine name for the configuration entity.