diff --git a/core/modules/block/custom_block/custom_block.module b/core/modules/block/custom_block/custom_block.module index 4a1cbcb..ff637f6 100644 --- a/core/modules/block/custom_block/custom_block.module +++ b/core/modules/block/custom_block/custom_block.module @@ -74,9 +74,7 @@ function custom_block_menu() { $items['block/add'] = array( 'title' => 'Add custom block', - 'page callback' => 'custom_block_add_page', - 'access arguments' => array('administer blocks'), - 'file' => 'custom_block.pages.inc', + 'route_name' => 'custom_block_add_page', ); $items['block/add/%custom_block_type'] = array( 'title callback' => 'entity_page_label', diff --git a/core/modules/block/custom_block/custom_block.pages.inc b/core/modules/block/custom_block/custom_block.pages.inc index 439030e..9c802c4 100644 --- a/core/modules/block/custom_block/custom_block.pages.inc +++ b/core/modules/block/custom_block/custom_block.pages.inc @@ -10,36 +10,6 @@ use Symfony\Component\HttpFoundation\RedirectResponse; /** - * Page callback: Displays add custom block links for available types. - * - * @return array - * A render array for a list of the custom block types that can be added or - * if there is only one custom block type defined for the site, the function - * returns the custom block add page for that custom block type. - * - * @see custom_block_menu() - */ -function custom_block_add_page() { - $options = array(); - $request = drupal_container()->get('request'); - if (($theme = $request->attributes->get('theme')) && in_array($theme, array_keys(list_themes()))) { - // We have navigated to this page from the block library and will keep track - // of the theme for redirecting the user to the configuration page for the - // newly created block in the given theme. - $options = array( - 'query' => array('theme' => $theme) - ); - } - $types = entity_load_multiple('custom_block_type'); - if ($types && count($types) == 1) { - $type = reset($types); - return custom_block_add($type); - } - - return array('#theme' => 'custom_block_add_list', '#content' => $types); -} - -/** * Returns HTML for a list of available custom block types for block creation. * * @param $variables diff --git a/core/modules/block/custom_block/custom_block.routing.yml b/core/modules/block/custom_block/custom_block.routing.yml new file mode 100644 index 0000000..ed4ab92 --- /dev/null +++ b/core/modules/block/custom_block/custom_block.routing.yml @@ -0,0 +1,6 @@ +custom_block_add_page: + pattern: block/add + defaults: + _content: 'Drupal\custom_block\Controller\CustomBlockController::add' + requirements: + _permission: 'administer blocks' diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Controller/CustomBlockController.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Controller/CustomBlockController.php new file mode 100644 index 0000000..783ef84 --- /dev/null +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Controller/CustomBlockController.php @@ -0,0 +1,96 @@ +get('request'), + $container->get('plugin.manager.entity'), + $container->get('module_handler') + ); + } + + /** + * Constructs a CustomBlock object. + * + * @param \Symfony\Component\HttpFoundation\Request $request + * Current request. + * @param \Drupal\Core\Entity\EntityManager $entity_manager + * Entity manager service. + * @param \Drupal\Core\Extension\ModuleHandler $module_handler + * Module Handler service. + */ + public function __construct(Request $request, EntityManager $entity_manager, ModuleHandler $module_handler) { + $this->request = $request; + $this->entityManager = $entity_manager; + $this->moduleHandler = $module_handler; + } + + /** + * Displays add custom block links for available types. + * + * @return array + * A render array for a list of the custom block types that can be added or + * if there is only one custom block type defined for the site, the function + * returns the custom block add page for that custom block type. + */ + public function add() { + $options = array(); + if (($theme = $this->request->attributes->get('theme')) && in_array($theme, array_keys(list_themes()))) { + // We have navigated to this page from the block library and will keep track + // of the theme for redirecting the user to the configuration page for the + // newly created block in the given theme. + $options = array( + 'query' => array('theme' => $theme) + ); + } + $types = $this->entityManager->getStorageController('custom_block_type')->load(); + if ($types && count($types) == 1) { + $type = reset($types); + // @todo convert this to OO once block/add/%type uses a Controller. Will + // be fixed in http://drupal.org/node/1978166. + $this->moduleHandler->loadInclude('custom_block', 'pages.inc'); + return custom_block_add($type); + } + + return array('#theme' => 'custom_block_add_list', '#content' => $types); + } + +}