diff --git a/core/lib/Drupal/Core/Entity/HtmlEntityFormController.php b/core/lib/Drupal/Core/Entity/HtmlEntityFormController.php index 8f62a40..41133e8 100644 --- a/core/lib/Drupal/Core/Entity/HtmlEntityFormController.php +++ b/core/lib/Drupal/Core/Entity/HtmlEntityFormController.php @@ -21,7 +21,7 @@ class HtmlEntityFormController extends HtmlFormController { * Due to reflection, the argument must be named $_entity_form. The parent * method has $request and $_form, but the parameter must match the route. */ - public function content(Request $request, $_entity_form) { + public function content(Request $request, $_entity_form, $_values = FALSE) { return parent::content($request, $_entity_form); } @@ -56,7 +56,25 @@ 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 and the callable is in + // class::method format. + if (($_values = $request->attributes->get('_values')) && strpos($_values, '::') !== FALSE) { + // Extract the class and method from the given string. + 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(); + } + + // Use _values callable to build default values for the entity. + $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 b8785de..7fc46e0 100644 --- a/core/modules/block/custom_block/custom_block.module +++ b/core/modules/block/custom_block/custom_block.module @@ -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( 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 ac4ec99..5a94841 100644 --- a/core/modules/block/custom_block/custom_block.routing.yml +++ b/core/modules/block/custom_block/custom_block.routing.yml @@ -12,3 +12,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..e8c04f0 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\CustomBlockTypeInterface; 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,37 @@ 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); + $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); + } + // @todo remove call to entity_get_form() when + // http://drupal.org/node/1982980 is in. + return entity_get_form($block); } return array('#theme' => 'custom_block_add_list', '#content' => $types); } + /** + * 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.