Issue #1978166 by larowlan, smiletrl, tim.plunkett, effulgentsia: Convert block/add/{%custom_block_type()} to Controller.

8.0.x
Alex Pott 2013-06-17 01:38:28 +02:00
parent c5cecfde1d
commit 6f25f15885
4 changed files with 56 additions and 66 deletions

View File

@ -73,13 +73,9 @@ function custom_block_menu() {
'route_name' => 'custom_block_add_page',
);
$items['block/add/%custom_block_type'] = array(
'title callback' => 'entity_page_label',
'title arguments' => array(2),
'page callback' => 'custom_block_add',
'page arguments' => array(2),
'access arguments' => array('administer blocks'),
'title' => 'Add custom block',
'description' => 'Add custom block',
'file' => 'custom_block.pages.inc',
'route_name' => 'custom_block_add_form'
);
// There has to be a base-item in order for contextual links to work.
$items['block/%custom_block'] = array(

View File

@ -29,35 +29,6 @@ function template_preprocess_custom_block_add_list(&$variables) {
}
}
/**
* Page callback: Presents the custom block creation form.
*
* @param Drupal\custom_block\Plugin\Core\Entity\CustomBlockType $block_type
* The custom block type to add.
*
* @return array
* A form array as expected by drupal_render().
*
* @see custom_block_menu()
*/
function custom_block_add(CustomBlockType $block_type) {
drupal_set_title(t('Add %type custom block', array(
'%type' => $block_type->label()
)), PASS_THROUGH);
$block = entity_create('custom_block', array(
'type' => $block_type->id()
));
$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.
$block->setTheme($theme);
}
return entity_get_form($block);
}
/**
* Page callback: Presents the custom block edit form.
*

View File

@ -11,3 +11,10 @@ custom_block_add_page:
_content: 'Drupal\custom_block\Controller\CustomBlockController::add'
requirements:
_permission: 'administer blocks'
custom_block_add_form:
pattern: block/add/{custom_block_type}
defaults:
_content: 'Drupal\custom_block\Controller\CustomBlockController::addForm'
requirements:
_permission: 'administer blocks'

View File

@ -8,8 +8,9 @@
namespace Drupal\custom_block\Controller;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\custom_block\CustomBlockStorageController;
use Drupal\custom_block\CustomBlockTypeInterface;
use Drupal\custom_block\CustomBlockTypeStorageController;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
@ -23,27 +24,28 @@ class CustomBlockController implements ControllerInterface {
protected $request;
/**
* Entity Manager service.
* The custom block storage controller.
*
* @var \Drupal\Core\Entity\EntityManager
* @var \Drupal\custom_block\CustomBlockStorageController
*/
protected $entityManager;
protected $customBlockStorage;
/**
* Module handler service.
* The custom block type storage controller.
*
* @var \Drupal\Core\Extension\ModuleHandler
* @var \Drupal\custom_block\CustomBlockTypeStorageController
*/
protected $moduleHandler;
protected $customBlockTypeStorage;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$entity_manager = $container->get('plugin.manager.entity');
return new static(
$container->get('request'),
$container->get('plugin.manager.entity'),
$container->get('module_handler')
$entity_manager->getStorageController('custom_block'),
$entity_manager->getStorageController('custom_block_type')
);
}
@ -52,15 +54,15 @@ class CustomBlockController implements ControllerInterface {
*
* @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.
* @param \Drupal\custom_block\CustomBlockStorageController $custom_block_storage
* The custom block storage controller.
* @param \Drupal\custom_block\CustomBlockTypeStorageController $custom_block_type_storage
* The custom block type storage controller.
*/
public function __construct(Request $request, EntityManager $entity_manager, ModuleHandler $module_handler) {
public function __construct(Request $request, CustomBlockStorageController $custom_block_storage, CustomBlockTypeStorageController $custom_block_type_storage) {
$this->request = $request;
$this->entityManager = $entity_manager;
$this->moduleHandler = $module_handler;
$this->customBlockStorage = $custom_block_storage;
$this->customBlockTypeStorage = $custom_block_type_storage;
}
/**
@ -72,25 +74,39 @@ class CustomBlockController implements ControllerInterface {
* 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();
$types = $this->customBlockTypeStorage->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 $this->addForm($type);
}
return array('#theme' => 'custom_block_add_list', '#content' => $types);
}
/**
* Presents the custom block creation form.
*
* @param \Drupal\custom_block\CustomBlockTypeInterface $custom_block_type
* The custom block type to add.
*
* @return array
* A form array as expected by drupal_render().
*/
public function addForm(CustomBlockTypeInterface $custom_block_type) {
// @todo Remove this when https://drupal.org/node/1981644 is in.
drupal_set_title(t('Add %type custom block', array(
'%type' => $custom_block_type->label()
)), PASS_THROUGH);
$block = $this->customBlockStorage->create(array(
'type' => $custom_block_type->id()
));
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.
$block->setTheme($theme);
}
return entity_get_form($block);
}
}