diff --git a/core/lib/Drupal/Core/Entity/HtmlEntityFormController.php b/core/lib/Drupal/Core/Entity/HtmlEntityFormController.php
index 79ef787..e9aaab2 100644
--- a/core/lib/Drupal/Core/Entity/HtmlEntityFormController.php
+++ b/core/lib/Drupal/Core/Entity/HtmlEntityFormController.php
@@ -8,6 +8,7 @@
namespace Drupal\Core\Entity;
use Drupal\Core\HtmlFormController;
+use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
/**
@@ -15,16 +16,41 @@
*/
class HtmlEntityFormController extends HtmlFormController {
+
/**
* Overrides \Drupal\Core\HtmlFormController::content().
*
- * Due to reflection, the argument must be named $_entity_form. The parent
- * method has $request and $_form, but the parameter must match the route.
+ * @param \Symfony\Component\HttpFoundation\Request $request
+ * The request object.
+ * @param callable $_form
+ * The name of the form class for this request.
+ * @param string $_values
+ * (optional) The class and method that provides default values for creating
+ * an entity when one does not exist.
+ *
+ * @return \Symfony\Component\HttpFoundation\Response
+ * A response object.
*/
- public function content(Request $request, $_entity_form) {
- return parent::content($request, $_entity_form);
- }
+ public function content(Request $request, $_entity_form, $_values = FALSE) {
+ $form_object = $this->getFormObject($request, $_entity_form, $_values);
+
+ // Using reflection, find all of the parameters needed by the form in the
+ // request attributes, skipping $form and $form_state.
+ $attributes = $request->attributes->all();
+ $reflection = new \ReflectionMethod($form_object, 'buildForm');
+ $params = $reflection->getParameters();
+ $args = array();
+ foreach (array_splice($params, 2) as $param) {
+ if (array_key_exists($param->name, $attributes)) {
+ $args[] = $attributes[$param->name];
+ }
+ }
+ $form_state['build_info']['args'] = $args;
+ $form_id = _drupal_form_id($form_object, $form_state);
+ $form = drupal_build_form($form_id, $form_state);
+ return new Response(drupal_render_page($form));
+ }
/**
* Overrides \Drupal\Core\HtmlFormController::getFormObject().
*
@@ -45,7 +71,7 @@ public function content(Request $request, $_entity_form) {
* _entity_form: 'node'
* @endcode
*/
- protected function getFormObject(Request $request, $form_arg) {
+ protected function getFormObject(Request $request, $form_arg, $_values = FALSE) {
$manager = $this->container->get('plugin.manager.entity');
// If no operation is provided, use 'default'.
@@ -56,7 +82,22 @@ protected function getFormObject(Request $request, $form_arg) {
$entity = $request->attributes->get($entity_type);
}
else {
- $entity = $manager->getStorageController($entity_type)->create(array());
+ $values = array();
+ // A default values callback has been provided.
+ if ($_values && strpos($_values, '::') !== FALSE) {
+ list($class, $method) = explode('::', $_values);
+ // The default values callback is in a ControllerInterface object.
+ if (in_array('Drupal\Core\ControllerInterface', class_implements($class))) {
+ $defaults_class = $class::create($this->container);
+ }
+ else {
+ $defaults_class = new $class();
+ }
+
+ $values = call_user_func(array($defaults_class, $method), $request);
+ }
+
+ $entity = $manager->getStorageController($entity_type)->create($values);
}
return $manager->getFormController($entity_type, $operation)->setEntity($entity);
diff --git a/core/modules/block/custom_block/custom_block.module b/core/modules/block/custom_block/custom_block.module
index e8ffbf0..83330b4 100644
--- a/core/modules/block/custom_block/custom_block.module
+++ b/core/modules/block/custom_block/custom_block.module
@@ -76,13 +76,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(
diff --git a/core/modules/block/custom_block/custom_block.pages.inc b/core/modules/block/custom_block/custom_block.pages.inc
index 9c802c4..7b7d4b4 100644
--- a/core/modules/block/custom_block/custom_block.pages.inc
+++ b/core/modules/block/custom_block/custom_block.pages.inc
@@ -35,36 +35,6 @@ function theme_custom_block_add_list($variables) {
return $output;
}
-
-/**
- * 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.
*
diff --git a/core/modules/block/custom_block/custom_block.routing.yml b/core/modules/block/custom_block/custom_block.routing.yml
index ed4ab92..194162d 100644
--- a/core/modules/block/custom_block/custom_block.routing.yml
+++ b/core/modules/block/custom_block/custom_block.routing.yml
@@ -4,3 +4,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:
+ _entity_form: 'custom_block.default'
+ _values: 'Drupal\custom_block\Controller\CustomBlockController::defaults'
+ 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
index 783ef84..dcc241c 100644
--- 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
@@ -9,7 +9,7 @@
use Drupal\Core\ControllerInterface;
use Drupal\Core\Entity\EntityManager;
-use Drupal\Core\Extension\ModuleHandler;
+use Drupal\custom_block\Plugin\Core\Entity\CustomBlockType;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
@@ -30,20 +30,12 @@ class CustomBlockController implements ControllerInterface {
protected $entityManager;
/**
- * Module handler service.
- *
- * @var \Drupal\Core\Extension\ModuleHandler
- */
- protected $moduleHandler;
-
- /**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('request'),
- $container->get('plugin.manager.entity'),
- $container->get('module_handler')
+ $container->get('plugin.manager.entity')
);
}
@@ -54,13 +46,10 @@ public static function create(ContainerInterface $container) {
* 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) {
+ public function __construct(Request $request, EntityManager $entity_manager) {
$this->request = $request;
$this->entityManager = $entity_manager;
- $this->moduleHandler = $module_handler;
}
/**
@@ -84,13 +73,49 @@ public function add() {
$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 $this->addForm($type);
}
return array('#theme' => 'custom_block_add_list', '#content' => $types);
}
+ /**
+ * Presents the custom block creation form.
+ *
+ * @param \Drupal\custom_block\Plugin\Core\Entity\CustomBlockType $custom_block_type
+ * The custom block type to add.
+ *
+ * @return array
+ * A form array as expected by drupal_render().
+ */
+ protected function addForm(CustomBlockType $custom_block_type) {
+ $block = $this->entityManager->getStorageController('custom_block')->create(array(
+ 'type' => $custom_block_type->id()
+ ));
+ $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.
+ $block->setTheme($theme);
+ }
+ return entity_get_form($block);
+ }
+
+
+ /**
+ * Returns default values for the new entity.
+ *
+ * @param \Symfony\Component\HttpFoundation\Request $request
+ * The current request.
+ *
+ * @return array
+ * Array of default values to use whilst creating entity form.
+ */
+ public function defaults(Request $request) {
+ $custom_block_type = $request->attributes->get('custom_block_type');
+ return array(
+ 'type' => $custom_block_type->id()
+ );
+ }
}
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 1a09dbf..96b87e7 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
@@ -42,6 +42,14 @@ protected function prepareEntity() {
*/
public function form(array $form, array &$form_state) {
$block = $this->entity;
+
+ // @todo Remove this when https://drupal.org/node/1981644 is in.
+ if ($block->isNew()) {
+ drupal_set_title(t('Add %type custom block', array(
+ '%type' => $block->bundle()
+ )), 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.