Commit 803a98af authored by catch's avatar catch

Issue #2685917 by larowlan, tim.plunkett, cheeky-micah: Placing the same...

Issue #2685917 by larowlan, tim.plunkett, cheeky-micah: Placing the same custom block, more than once, with a period in the title, results in an Error
parent 7e7de025
......@@ -245,7 +245,7 @@ public function getMachineNameSuggestion() {
$transliterated = $this->transliteration()->transliterate($admin_label, LanguageInterface::LANGCODE_DEFAULT, '_');
$transliterated = Unicode::strtolower($transliterated);
$transliterated = preg_replace('@[^a-z0-9_.]+@', '', $transliterated);
$transliterated = preg_replace('@[^a-z0-9_]+@', '', $transliterated);
return $transliterated;
}
......
......@@ -147,7 +147,7 @@ public function form(array $form, FormStateInterface $form_state) {
'#default_value' => !$entity->isNew() ? $entity->id() : $this->getUniqueMachineName($entity),
'#machine_name' => array(
'exists' => '\Drupal\block\Entity\Block::load',
'replace_pattern' => '[^a-z0-9_.]+',
'replace_pattern' => '[^a-z0-9_]+',
'source' => array('settings', 'label'),
),
'#required' => TRUE,
......
<?php
namespace Drupal\Tests\block\Kernel;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Component\Plugin\PluginBase;
use Drupal\KernelTests\KernelTestBase;
use Drupal\simpletest\BlockCreationTrait;
/**
* Tests block machine names.
*
* @group block
*/
class BlockMachineNameTest extends KernelTestBase {
use BlockCreationTrait;
/**
* {@inheritdoc}
*/
public static $modules = ['block', 'block_content', 'system', 'user'];
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->installSchema('system', ['sequence']);
$this->installEntitySchema('block_content');
$this->installEntitySchema('user');
}
/**
* Tests machine name collisions.
*/
public function testMachineNamesShouldNotCollideIfBlockTitlesContainPeriod() {
// Create a block type.
$type = BlockContentType::create([
'id' => 'whizzy',
'label' => 'Tis whizzy',
]);
$type->save();
// And a block content entity.
$block_content = BlockContent::create([
'type' => 'Tis whizzy',
'info' => 'This. is whizzy',
]);
$block_content->save();
// Use \Drupal\Core\Block\BlockBase::getMachineNameSuggestion to generate
// an ID.
$plugin_id = 'block_content' . PluginBase::DERIVATIVE_SEPARATOR . $block_content->uuid();
$block_plugin = $this->container->get('plugin.manager.block')->createInstance($plugin_id, []);
$method = new \ReflectionMethod($block_plugin, 'getMachineNameSuggestion');
$method->setAccessible(TRUE);
$id = $method->invoke($block_plugin);
// Now place the block content entity.
$block = $this->placeBlock($plugin_id, [
'label' => 'This. is whizzy',
'id' => $id,
]);
$theme = $this->container->get('config.factory')->get('system.theme')->get('default');
// Create a new entity.
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = $this->container->get('entity_type.manager');
$new_entity = $entity_type_manager->getStorage('block')->create(array('plugin' => $plugin_id, 'theme' => $theme));
// Now generate a default ID like
// \Drupal\block\BlockForm::getUniqueMachineName does.
/** @var \Drupal\block\BlockForm $form_object */
$form_object = $entity_type_manager->getFormObject('block', 'default');
$form_object->setEntity($new_entity);
$unique_id = $form_object->getUniqueMachineName($new_entity);
// Assert that the new ID is different to the old one.
$this->assertNotEquals($unique_id, $block->id());
}
}
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