diff --git a/core/lib/Drupal/Core/Entity/HtmlEntityFormController.php b/core/lib/Drupal/Core/Entity/HtmlEntityFormController.php
index 79ef787..2e5c872 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,33 @@ 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();
+ }
+
+ // Using reflection, find all of the parameters needed by the form in
+ // the request attributes.
+ $attributes = $request->attributes->all();
+ $reflection = new \ReflectionMethod($class, $method);
+ $params = $reflection->getParameters();
+ $args = array();
+ foreach ($params as $param) {
+ if (array_key_exists($param->name, $attributes)) {
+ $args[] = $attributes[$param->name];
+ }
+ }
+ $values = call_user_func_array(array($defaults_class, $method), $args);
+ }
+
+ $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.routing.yml b/core/modules/block/custom_block/custom_block.routing.yml
index 76e104c..194162d 100644
--- a/core/modules/block/custom_block/custom_block.routing.yml
+++ b/core/modules/block/custom_block/custom_block.routing.yml
@@ -5,8 +5,9 @@ custom_block_add_page:
requirements:
_permission: 'administer blocks'
custom_block_add_form:
- pattern: block/add/{custom_block_type}
+ pattern: 'block/add/{custom_block_type}'
defaults:
- _content: 'Drupal\custom_block\Controller\CustomBlockController::addForm'
+ _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 4264100..1b4345a 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
@@ -88,11 +88,7 @@ public function add() {
* @return array
* A form array as expected by drupal_render().
*/
- public function addForm(CustomBlockType $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);
+ protected function addForm(CustomBlockType $custom_block_type) {
$block = $this->entityManager->getStorageController('custom_block')->create(array(
'type' => $custom_block_type->id()
));
@@ -105,4 +101,20 @@ public function addForm(CustomBlockType $custom_block_type) {
}
return entity_get_form($block);
}
+
+
+ /**
+ * Returns default values for the new entity.
+ *
+ * @param Drupal\custom_block\Plugin\Core\Entity\CustomBlockType $custom_block_type
+ * The custom block type to add.
+ *
+ * @return array
+ * Array of default values to use whilst creating entity form.
+ */
+ public function defaults(CustomBlockType $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..cbb649a 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.
@@ -213,5 +221,4 @@ public function delete(array $form, array &$form_state) {
$block = $this->buildEntity($form, $form_state);
$form_state['redirect'] = array('block/' . $block->id() . '/delete', array('query' => $destination));
}
-
}