Verified Commit 98887e87 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #1956134 by smustgrave, dawehner, larowlan, jibran, jbloomfield,...

Issue #1956134 by smustgrave, dawehner, larowlan, jibran, jbloomfield, lokapujya, Oscaner, elachlan, mgifford, olli, srilakshmier, Manuel Garcia, Nitin shrivastava, afeijo, alexpott, jgeryk, garphy, grisendo, jessebeach, xjm, joachim, tim.plunkett, andypost, Wim Leers, EclipseGc, damiankloip, benjy, klonos: Provide helpful editing links on "admin/structure/block" for deriver blocks (menu, views, block content, etc.)

(cherry picked from commit f8a34d34)
parent af7f5ed2
Loading
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -5,7 +5,9 @@
 * Allows the creation of content blocks through the user interface.
 */

use Drupal\block\BlockInterface;
use Drupal\block_content\BlockContentInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\field\Entity\FieldConfig;
@@ -214,3 +216,28 @@ function block_content_theme_suggestions_block_alter(array &$suggestions, array

  return $suggestions;
}

/**
 * Implements hook_entity_operation().
 */
function block_content_entity_operation(EntityInterface $entity): array {
  $operations = [];
  if ($entity instanceof BlockInterface) {
    $plugin = $entity->getPlugin();
    if ($plugin->getBaseId() === 'block_content') {
      $custom_block = \Drupal::entityTypeManager()->getStorage('block_content')->loadByProperties([
        'uuid' => $plugin->getDerivativeId(),
      ]);
      $custom_block = reset($custom_block);
      if ($custom_block && $custom_block->access('update')) {
        $operations['block-edit'] = [
          'title' => t('Edit block'),
          'url' => $custom_block->toUrl('edit-form')->setOptions([]),
          'weight' => 50,
        ];
      }
    }
  }

  return $operations;
}
+77 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\block_content\Kernel;

use Drupal\block\Entity\Block;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;

/**
 * Tests the block content.
 *
 * @group block_content
 */
class BlockContentTest extends KernelTestBase {

  use UserCreationTrait;
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['block', 'block_content', 'system', 'user'];

  /**
   * {@inheritdoc}
   */
  public function setUp(): void {
    parent::setUp();
    $this->installEntitySchema('user');
    $this->installEntitySchema('block_content');
  }

  /**
   * Tests the editing links for BlockContentBlock.
   */
  public function testOperationLinks(): void {
    // Create a block content type.
    BlockContentType::create([
      'id' => 'spiffy',
      'label' => 'Mucho spiffy',
      'description' => "Provides a block type that increases your site's spiffiness by up to 11%",
    ])->save();
    // And a block content entity.
    $block_content = BlockContent::create([
      'info' => 'Spiffy prototype',
      'type' => 'spiffy',
    ]);
    $block_content->save();
    $block = Block::create([
      'plugin' => 'block_content' . PluginBase::DERIVATIVE_SEPARATOR . $block_content->uuid(),
      'region' => 'content',
      'id' => 'machine_name',
      'theme' => 'stark',
    ]);

    // The anonymous user doesn't have the "administer block" permission.
    $this->assertEmpty(block_content_entity_operation($block));

    $this->setUpCurrentUser(['uid' => 1], ['edit any spiffy block content', 'administer blocks']);

    // The admin user does have the "administer block" permission.
    $this->assertEquals([
      'block-edit' => [
        'title' => $this->t('Edit block'),
        'url' => $block_content->toUrl('edit-form')->setOptions([]),
        'weight' => 50,
      ],
    ], block_content_entity_operation($block));
  }

}
+24 −0
Original line number Diff line number Diff line
@@ -8,6 +8,8 @@
 * used for navigation.
 */

use Drupal\block\BlockInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Cache\CacheableMetadata;
@@ -466,3 +468,25 @@ function menu_ui_theme(): array {
    ],
  ];
}

/**
 * Implements hook_entity_operation().
 */
function menu_ui_entity_operation(EntityInterface $entity): array {
  $operations = [];
  if ($entity instanceof BlockInterface) {
    $plugin = $entity->getPlugin();
    if ($plugin->getBaseId() === 'system_menu_block') {
      $menu = Menu::load($plugin->getDerivativeId());
      if ($menu && $menu->access('edit')) {
        $operations['menu-edit'] = [
          'title' => t('Edit menu'),
          'url' => $menu->toUrl('edit-form'),
          'weight' => 50,
        ];
      }
    }
  }

  return $operations;
}
+88 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\menu_ui\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\system\Entity\Menu;
use Drupal\block\Entity\Block;
use Drupal\system\MenuInterface;
use Drupal\Tests\user\Traits\UserCreationTrait;

/**
 * Tests SystemMenuBlock.
 *
 * @group menu_ui
 */
class MenuBlockTest extends KernelTestBase {

  use UserCreationTrait;

  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = [
    'system',
    'block',
    'menu_ui',
    'user',
  ];

  /**
   * The menu for testing.
   *
   * @var \Drupal\system\MenuInterface
   */
  protected MenuInterface $menu;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->installEntitySchema('user');

    $this->setUpCurrentUser([], ['administer menu']);

    // Add a new custom menu.
    $menu_name = 'mock';
    $label = $this->randomMachineName(16);

    $this->menu = Menu::create([
      'id' => $menu_name,
      'label' => $label,
      'description' => 'Description text',
    ]);
    $this->menu->save();

  }

  /**
   * Tests the editing links for SystemMenuBlock.
   */
  public function testOperationLinks(): void {
    $block = Block::create([
      'plugin' => 'system_menu_block:' . $this->menu->id(),
      'region' => 'footer',
      'id' => 'machine_name',
      'theme' => 'stark',
    ]);

    // Test when user does have "administer menu" permission.
    $this->assertEquals([
      'menu-edit' => [
        'title' => 'Edit menu',
        'url' => $this->menu->toUrl('edit-form'),
        'weight' => 50,
      ],
    ], menu_ui_entity_operation($block));

    $this->setUpCurrentUser();
    // Test when user doesn't have "administer menu" permission.
    $this->assertEmpty(menu_ui_entity_operation($block));
  }

}
+82 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\views_ui\Kernel;

use Drupal\Core\Url;
use Drupal\block\Entity\Block;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Tests\ViewTestData;

/**
 * Tests ViewsBlock.
 *
 * @group views_ui
 */
class ViewsBlockTest extends ViewsKernelTestBase {

  use UserCreationTrait;

  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = [
    'system',
    'block',
    'block_test_views',
    'views_ui',
    'user',
  ];

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = ['test_view_block'];

  /**
   * {@inheritdoc}
   */
  protected function setUp($import_test_views = TRUE): void {
    parent::setUp();

    ViewTestData::createTestViews(static::class, ['block_test_views']);
  }

  /**
   * Tests the editing links for ViewsBlockBase.
   */
  public function testOperationLinks(): void {
    $this->setUpCurrentUser(['uid' => 0]);

    $block = Block::create([
      'plugin' => 'views_block:test_view_block-block_1',
      'region' => 'content',
      'id' => 'machine_name',
      'theme' => 'stark',
    ]);

    // The anonymous user doesn't have the "administer block" permission.
    $this->assertEmpty(views_ui_entity_operation($block));

    $this->setUpCurrentUser(['uid' => 1], ['administer views']);

    // The admin user does have the "administer block" permission.
    $this->assertEquals([
      'view-edit' => [
        'title' => 'Edit view',
        'url' => Url::fromRoute('entity.view.edit_display_form', [
          'view' => 'test_view_block',
          'display_id' => 'block_1',
        ]),
        'weight' => 50,
      ],
    ], views_ui_entity_operation($block));
  }

}
Loading