Commit 834c5a29 authored by alexpott's avatar alexpott

Issue #2403105 by larowlan, idebr, mshaver: Add an empty text on "Add Custom...

Issue #2403105 by larowlan, idebr, mshaver: Add an empty text on "Add Custom Block" page when no custom block types are available
parent 2b6baf60
......@@ -12,6 +12,7 @@
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\block_content\BlockContentTypeInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
......@@ -83,6 +84,13 @@ public function add(Request $request) {
$type = reset($types);
return $this->addForm($type, $request);
}
if (count($types) === 0) {
return array(
'#markup' => $this->t('You have not created any block types yet. Go to the <a href="!url">block type creation page</a> to add a new block type.', [
'!url' => Url::fromRoute('block_content.type_add')->toString(),
]),
);
}
return array('#theme' => 'block_content_add_list', '#content' => $types);
}
......
......@@ -43,14 +43,21 @@ abstract class BlockContentTestBase extends WebTestBase {
*/
public static $modules = array('block', 'block_content');
/**
* Whether or not to auto-create the basic block type during setup.
*
* @var bool
*/
protected $autoCreateBasicBlockType = TRUE;
/**
* Sets the test up.
*/
protected function setUp() {
parent::setUp();
// Ensure the basic bundle exists. This is provided by the standard profile.
$block_content_type = $this->createBlockContentType('basic');
block_content_add_body_field($block_content_type->id());
if ($this->autoCreateBasicBlockType) {
$this->createBlockContentType('basic', TRUE);
}
$this->adminUser = $this->drupalCreateUser($this->permissions);
}
......@@ -84,17 +91,22 @@ protected function createBlockContent($title = FALSE, $bundle = 'basic') {
*
* @param string $label
* The block type label.
* @param bool $create_body
* Whether or not to create the body field
*
* @return \Drupal\block_content\Entity\BlockContentType
* Created custom block type.
*/
protected function createBlockContentType($label) {
protected function createBlockContentType($label, $create_body = FALSE) {
$bundle = entity_create('block_content_type', array(
'id' => $label,
'label' => $label,
'revision' => FALSE
'revision' => FALSE,
));
$bundle->save();
if ($create_body) {
block_content_add_body_field($bundle->id());
}
return $bundle;
}
......
......@@ -7,6 +7,7 @@
namespace Drupal\block_content\Tests;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
/**
* Ensures that custom block type functions work correctly.
......@@ -32,19 +33,34 @@ class BlockContentTypeTest extends BlockContentTestBase {
'administer block_content fields'
);
/**
* Whether or not to create an initial block type.
*
* @var bool
*/
protected $autoCreateBasicBlockType = FALSE;
/**
* Tests creating a block type programmatically and via a form.
*/
public function testBlockContentTypeCreation() {
// Login a test user.
$this->drupalLogin($this->adminUser);
// Test the page with no block-types.
$this->drupalGet('block/add');
$this->assertResponse(200);
$this->assertRaw(t('You have not created any block types yet. Go to the <a href="!url">block type creation page</a> to add a new block type.', [
'!url' => Url::fromRoute('block_content.type_add')->toString(),
]));
// Now create an initial block-type.
$this->createBlockContentType('basic', TRUE);
// Create a block type programmaticaly.
$type = $this->createBlockContentType('other');
$block_type = entity_load('block_content_type', 'other');
$this->assertTrue($block_type, 'The new block type has been created.');
// Login a test user.
$this->drupalLogin($this->adminUser);
$this->drupalGet('block/add/' . $type->id());
$this->assertResponse(200, 'The new block type can be accessed at bloack/add.');
......@@ -69,6 +85,9 @@ public function testBlockContentTypeCreation() {
* Tests editing a block type using the UI.
*/
public function testBlockContentTypeEditing() {
// Now create an initial block-type.
$this->createBlockContentType('basic', TRUE);
$this->drupalLogin($this->adminUser);
// We need two block types to prevent /block/add redirecting.
$this->createBlockContentType('other');
......@@ -106,6 +125,9 @@ public function testBlockContentTypeEditing() {
* Tests deleting a block type that still has content.
*/
public function testBlockContentTypeDeletion() {
// Now create an initial block-type.
$this->createBlockContentType('basic', TRUE);
// Create a block type programmatically.
$type = $this->createBlockContentType('foo');
......@@ -136,6 +158,9 @@ public function testBlockContentTypeDeletion() {
* Tests that redirects work as expected when multiple block types exist.
*/
public function testsBlockContentAddTypes() {
// Now create an initial block-type.
$this->createBlockContentType('basic', TRUE);
$this->drupalLogin($this->adminUser);
// Create two block types programmatically.
$type = $this->createBlockContentType('foo');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment