Unverified Commit 176346e7 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3461316 by amanbtr72, dcam, pasqualle, smustgrave, acbramley: Redirect...

Issue #3461316 by amanbtr72, dcam, pasqualle, smustgrave, acbramley: Redirect block/add if there is only one block type

(cherry picked from commit ed28d46c)
parent dedf8062
Loading
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -2,9 +2,9 @@

namespace Drupal\block_content\Controller;

use Drupal\block_content\BlockContentTypeInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\block_content\BlockContentTypeInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -88,7 +88,8 @@ public function add(Request $request) {
    uasort($types, [$this->blockContentTypeStorage->getEntityType()->getClass(), 'sort']);
    if ($types && count($types) == 1) {
      $type = reset($types);
      return $this->addForm($type, $request);
      $query = $request->query->all();
      return $this->redirect('block_content.add_form', ['block_content_type' => $type->id()], ['query' => $query]);
    }
    if (count($types) === 0) {
      return [
+0 −6
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@
use Drupal\Core\Menu\LocalActionDefault;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

@@ -54,11 +53,6 @@ public function getOptions(RouteMatchInterface $route_match) {
    if ($region = $this->requestStack->getCurrentRequest()->query->getString('region')) {
      $options['query']['region'] = $region;
    }

    // Adds a destination on content block listing.
    if ($route_match->getRouteName() == 'entity.block_content.collection') {
      $options['query']['destination'] = Url::fromRoute('<current>')->toString();
    }
    return $options;
  }

+1 −5
Original line number Diff line number Diff line
@@ -155,11 +155,7 @@ public function testBlockContentFormSubmitHandlers(): void {
    // Create a block and place in block layout.
    $this->drupalGet('/admin/content/block');
    $this->clickLink('Add content block');
    // Verify destination URL, when clicking "Save and configure" this
    // destination will be ignored.
    $base = base_path();
    $url = 'block/add?destination=' . $base . 'admin/content/block';
    $this->assertSession()->addressEquals($url);
    $this->assertSession()->addressEquals('/block/add/basic');
    $edit = [];
    $edit['info[0][value]'] = 'Test Block';
    $edit['body[0][value]'] = $this->randomMachineName(16);
+53 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\block_content\Functional;

/**
 * Tests block_content local action links.
 *
 * @group block_content
 */
class LocalActionTest extends BlockContentTestBase {

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->drupalLogin($this->adminUser);
  }

  /**
   * Tests the block_content_add_action link.
   */
  public function testAddContentBlockLink(): void {
    // Verify that the link takes you straight to the block form if there's only
    // one type.
    $this->drupalGet('/admin/content/block');
    $this->clickLink('Add content block');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->addressEquals('/block/add/basic');

    $type = $this->randomMachineName();
    $this->createBlockContentType([
      'id' => $type,
      'label' => $type,
    ]);

    // Verify that the link takes you to the block add page if there's more than
    // one type.
    $this->drupalGet('/admin/content/block');
    $this->clickLink('Add content block');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->addressEquals('/block/add');
  }

}