From c2e0cda721625a56f337a57e82f7af9719a751bc Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 22 Jul 2013 23:58:13 +0100
Subject: [PATCH] Issue #1983682 by tim.plunkett, h3rj4n, larowlan: Convert
 applicable custom_block_menu() entries to use _entity_form() in routing.yml.

---
 .../block/custom_block/custom_block.admin.inc | 36 -------------------
 .../block/custom_block/custom_block.module    | 35 ++++++++++--------
 .../block/custom_block/custom_block.pages.inc | 16 ---------
 .../custom_block/custom_block.routing.yml     | 24 ++++++++++++-
 .../CustomBlockFormController.php             |  5 +++
 .../CustomBlockTypeFormController.php         |  1 +
 .../Plugin/Core/Entity/CustomBlock.php        |  3 ++
 .../Plugin/Core/Entity/CustomBlockType.php    |  2 ++
 .../custom_block/Tests/PageEditTest.php       |  6 ++--
 9 files changed, 57 insertions(+), 71 deletions(-)
 delete mode 100644 core/modules/block/custom_block/custom_block.admin.inc

diff --git a/core/modules/block/custom_block/custom_block.admin.inc b/core/modules/block/custom_block/custom_block.admin.inc
deleted file mode 100644
index 8cb5c8ca9add..000000000000
--- a/core/modules/block/custom_block/custom_block.admin.inc
+++ /dev/null
@@ -1,36 +0,0 @@
-<?php
-
-/**
- * @file
- * Admin page callbacks for the custom block module.
- */
-
-use Drupal\custom_block\Plugin\Core\Entity\CustomBlockType;
-
-/**
- * Page callback: Presents the custom block type creation form.
- *
- * @return array
- *   A form array as expected by drupal_render().
- *
- * @see custom_block_menu()
- */
-function custom_block_type_add() {
-  $block_type = entity_create('custom_block_type', array());
-  return Drupal::entityManager()->getForm($block_type);
-}
-
-/**
- * Page callback: Presents the custom block type edit form.
- *
- * @param \Drupal\custom_block\Plugin\Core\Entity\CustomBlockType $block_type
- *   The custom block type to edit.
- *
- * @return array
- *   A form array as expected by drupal_render().
- *
- * @see custom_block_menu()
- */
-function custom_block_type_edit(CustomBlockType $block_type) {
-  return Drupal::entityManager()->getForm($block_type);
-}
diff --git a/core/modules/block/custom_block/custom_block.module b/core/modules/block/custom_block/custom_block.module
index 4e55a85e6e3a..2752ee8ab470 100644
--- a/core/modules/block/custom_block/custom_block.module
+++ b/core/modules/block/custom_block/custom_block.module
@@ -37,21 +37,15 @@ function custom_block_menu() {
     'route_name' => 'custom_block_type_list',
   );
   $items['admin/structure/custom-blocks/add'] = array(
-    'title' => 'Add custom block type',
-    'page callback' => 'custom_block_type_add',
-    'access arguments' => array('administer blocks'),
-    'type' => MENU_LOCAL_ACTION,
+    'route_name' => 'custom_block_type_add',
+    'type' => MENU_SIBLING_LOCAL_TASK,
     'weight' => 1,
-    'file' => 'custom_block.admin.inc',
   );
   $items['admin/structure/custom-blocks/manage/%custom_block_type'] = array(
     'title' => 'Edit custom block type',
     'title callback' => 'entity_page_label',
     'title arguments' => array(4),
-    'page callback' => 'custom_block_type_edit',
-    'page arguments' => array(4),
-    'access arguments' => array('administer blocks'),
-    'file' => 'custom_block.admin.inc',
+    'route_name' => 'custom_block_type_edit',
   );
   $items['admin/structure/custom-blocks/manage/%custom_block_type/edit'] = array(
     'title' => 'Edit',
@@ -76,11 +70,7 @@ function custom_block_menu() {
   // There has to be a base-item in order for contextual links to work.
   $items['block/%custom_block'] = array(
     'title' => 'Edit',
-    'page callback' => 'custom_block_edit',
-    'page arguments' => array(1),
-    'access callback' => 'entity_page_access',
-    'access arguments' => array(1, 'update'),
-    'file' => 'custom_block.pages.inc',
+    'route_name' => 'custom_block_edit',
   );
   $items['block/%custom_block/edit'] = array(
     'title' => 'Edit',
@@ -102,6 +92,21 @@ function custom_block_menu() {
   return $items;
 }
 
+/**
+ * Implements hook_local_actions().
+ */
+function custom_block_local_actions() {
+  return array(
+    array(
+      'route_name' => 'custom_block_type_add',
+      'title' => t('Add custom block type'),
+      'appears_on' => array(
+        'custom_block_type_list',
+      ),
+    ),
+  );
+}
+
 /**
  * Implements hook_theme().
  */
@@ -231,7 +236,7 @@ function custom_block_form_block_plugin_ui_alter(&$form, $form_state) {
     $custom_block = entity_load_by_uuid('custom_block', $derivative);
     $row['1']['data']['#links']['edit'] = array(
       'title' => t('Edit'),
-      'href' => 'block/' . $custom_block->id() . '/edit'
+      'href' => 'block/' . $custom_block->id(),
     );
   }
 }
diff --git a/core/modules/block/custom_block/custom_block.pages.inc b/core/modules/block/custom_block/custom_block.pages.inc
index f6c4076c10d6..ae7de3012788 100644
--- a/core/modules/block/custom_block/custom_block.pages.inc
+++ b/core/modules/block/custom_block/custom_block.pages.inc
@@ -29,22 +29,6 @@ function template_preprocess_custom_block_add_list(&$variables) {
   }
 }
 
-/**
- * Page callback: Presents the custom block edit form.
- *
- * @param Drupal\custom_block\Plugin\Core\Entity\CustomBlock $block
- *   The custom block to edit.
- *
- * @return array
- *   A form array as expected by drupal_render().
- *
- * @see custom_block_menu()
- */
-function custom_block_edit(CustomBlock $block) {
-  drupal_set_title(t('Edit custom block %label', array('%label' => $block->label())), PASS_THROUGH);
-  return Drupal::entityManager()->getForm($block);
-}
-
 /**
  * Page callback: Form constructor for the custom block deletion form.
  *
diff --git a/core/modules/block/custom_block/custom_block.routing.yml b/core/modules/block/custom_block/custom_block.routing.yml
index 2178d916d061..02bad1363995 100644
--- a/core/modules/block/custom_block/custom_block.routing.yml
+++ b/core/modules/block/custom_block/custom_block.routing.yml
@@ -6,7 +6,7 @@ custom_block_type_list:
     _permission: 'administer blocks'
 
 custom_block_add_page:
-  pattern: block/add
+  pattern: '/block/add'
   defaults:
     _content: 'Drupal\custom_block\Controller\CustomBlockController::add'
   requirements:
@@ -23,5 +23,27 @@ custom_block_type_delete:
   pattern: '/admin/structure/custom-blocks/manage/{custom_block_type}/delete'
   defaults:
     _entity_form: 'custom_block_type.delete'
+  requirements:
+    _entity_access: 'custom_block_type.delete'
+
+custom_block_edit:
+  pattern: '/block/{custom_block}'
+  defaults:
+    _entity_form: 'custom_block.edit'
+  requirements:
+    _entity_access: 'custom_block.update'
+    custom_block: \d+
+
+custom_block_type_add:
+  pattern: '/admin/structure/custom-blocks/add'
+  defaults:
+    _entity_form: 'custom_block_type.add'
   requirements:
     _permission: 'administer blocks'
+
+custom_block_type_edit:
+  pattern: '/admin/structure/custom-blocks/manage/{custom_block_type}'
+  defaults:
+    _entity_form: 'custom_block_type.edit'
+  requirements:
+    _entity_access: 'custom_block_type.update'
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
index 2f18d5bb677a..9ccf37135006 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
@@ -41,6 +41,11 @@ protected function prepareEntity() {
    */
   public function form(array $form, array &$form_state) {
     $block = $this->entity;
+
+    if ($this->operation == 'edit') {
+      // @todo Remove this once https://drupal.org/node/1981644 is in.
+      drupal_set_title(t('Edit custom block %label', array('%label' => $block->label())), PASS_THROUGH);
+    }
     // Override the default CSS class name, since the user-defined custom block
     // type name in 'TYPE-block-form' potentially clashes with third-party class
     // names.
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeFormController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeFormController.php
index cf88f5d2d79f..a98730e140d3 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeFormController.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockTypeFormController.php
@@ -21,6 +21,7 @@ public function form(array $form, array &$form_state) {
     $form = parent::form($form, $form_state);
 
     $block_type = $this->entity;
+
     $form['label'] = array(
       '#type' => 'textfield',
       '#title' => t('Label'),
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php
index 74dfc25d0493..6bc0f4d2c602 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlock.php
@@ -26,6 +26,8 @@
  *     "access" = "Drupal\custom_block\CustomBlockAccessController",
  *     "render" = "Drupal\custom_block\CustomBlockRenderController",
  *     "form" = {
+ *       "add" = "Drupal\custom_block\CustomBlockFormController",
+ *       "edit" = "Drupal\custom_block\CustomBlockFormController",
  *       "default" = "Drupal\custom_block\CustomBlockFormController"
  *     },
  *     "translation" = "Drupal\custom_block\CustomBlockTranslationController"
@@ -34,6 +36,7 @@
  *   revision_table = "custom_block_revision",
  *   route_base_path = "admin/structure/custom-blocks/manage/{bundle}",
  *   menu_base_path = "block/%custom_block",
+ *   menu_edit_path = "block/%custom_block",
  *   fieldable = TRUE,
  *   translatable = TRUE,
  *   entity_keys = {
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlockType.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlockType.php
index 0ceabd531ff4..685e370b0211 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlockType.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Core/Entity/CustomBlockType.php
@@ -25,6 +25,8 @@
  *     "access" = "Drupal\custom_block\CustomBlockTypeAccessController",
  *     "form" = {
  *       "default" = "Drupal\custom_block\CustomBlockTypeFormController",
+ *       "add" = "Drupal\custom_block\CustomBlockTypeFormController",
+ *       "edit" = "Drupal\custom_block\CustomBlockTypeFormController",
  *       "delete" = "Drupal\custom_block\Form\CustomBlockTypeDeleteForm"
  *     },
  *     "list" = "Drupal\custom_block\CustomBlockTypeListController"
diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/PageEditTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/PageEditTest.php
index e541c40b7de3..dab0eb6317f3 100644
--- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/PageEditTest.php
+++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/PageEditTest.php
@@ -46,7 +46,7 @@ public function testPageEdit() {
     $this->assertTrue($block, 'Custom block found in database.');
 
     // Load the edit page.
-    $this->drupalGet('block/' . $block->id() . '/edit');
+    $this->drupalGet('block/' . $block->id());
     $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
     $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
 
@@ -58,7 +58,7 @@ public function testPageEdit() {
     $this->drupalPost(NULL, $edit, t('Save'));
 
     // Edit the same block, creating a new revision.
-    $this->drupalGet("block/" . $block->id() . "/edit");
+    $this->drupalGet("block/" . $block->id());
     $edit = array();
     $edit['info'] = $this->randomName(8);
     $edit[$body_key] = $this->randomName(16);
@@ -70,7 +70,7 @@ public function testPageEdit() {
     $this->assertNotIdentical($block->revision_id->value, $revised_block->revision_id->value, 'A new revision has been created.');
 
     // Test deleting the block.
-    $this->drupalGet("block/" . $revised_block->id() . "/edit");
+    $this->drupalGet("block/" . $revised_block->id());
     $this->drupalPost(NULL, array(), t('Delete'));
     $this->assertText(format_string('Are you sure you want to delete !label?', array('!label' => $revised_block->label())));
   }
-- 
GitLab