Issue #3397493 by borisson_, phenaproxima, Wim Leers: Add validation constraints to block_content.type.*

merge-requests/6163/head
Lee Rowlands 2024-01-15 15:35:13 +10:00
parent 2a232304d7
commit 4b5138e6c3
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
13 changed files with 75 additions and 13 deletions

View File

@ -5,6 +5,7 @@
* Post update functions for Content Block.
*/
use Drupal\block_content\BlockContentTypeInterface;
use Drupal\Core\Config\Entity\ConfigEntityUpdater;
use Drupal\user\Entity\Role;
use Drupal\views\Entity\View;
@ -80,3 +81,14 @@ function block_content_post_update_sort_permissions(&$sandbox = NULL) {
return FALSE;
});
}
/**
* Update configuration for revision type.
*/
function block_content_post_update_revision_type(&$sandbox = NULL) {
\Drupal::classResolver(ConfigEntityUpdater::class)
->update($sandbox, 'block_content_type', function (BlockContentTypeInterface $block_content_type) {
$block_content_type->set('revision', (bool) $block_content_type->get('revision'));
return TRUE;
});
}

View File

@ -11,8 +11,14 @@ block_content.type.*:
type: required_label
label: 'Label'
revision:
type: integer
label: 'Create new revision'
type: boolean
label: 'Whether a new revision should be created by default'
description:
type: text
label: 'Description'
nullable: true
constraints:
NotBlank:
allowNull: true
constraints:
FullyValidatable: ~

View File

@ -3,6 +3,7 @@
namespace Drupal\block_content;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\language\Entity\ContentLanguageSettings;
@ -91,6 +92,17 @@ class BlockContentTypeForm extends BundleEntityFormBase {
return $this->protectBundleIdElement($form);
}
/**
* {@inheritdoc}
*/
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
// An empty description violates config schema.
if (trim($form_state->getValue('description', '')) === '') {
$form_state->unsetValue('description');
}
parent::copyFormValuesToEntity($entity, $form, $form_state);
}
/**
* {@inheritdoc}
*/

View File

@ -74,20 +74,20 @@ class BlockContentType extends ConfigEntityBundleBase implements BlockContentTyp
*
* @var bool
*/
protected $revision;
protected $revision = FALSE;
/**
* The description of the block type.
*
* @var string
* @var string|null
*/
protected $description;
protected $description = NULL;
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->description;
return $this->description ?? '';
}
/**

View File

@ -55,7 +55,7 @@ abstract class BlockContentTypeResourceTestBase extends ConfigEntityResourceTest
'id' => 'pascal',
'label' => 'Pascal',
'langcode' => 'en',
'revision' => 0,
'revision' => FALSE,
'status' => TRUE,
'uuid' => $this->entity->uuid(),
];

View File

@ -2,6 +2,7 @@
namespace Drupal\Tests\block_content\Functional\Update;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
use Drupal\user\Entity\User;
use Drupal\views\Entity\View;
@ -22,6 +23,32 @@ class BlockContentUpdateTest extends UpdatePathTestBase {
];
}
/**
* Tests converting block types' `revision` flag to boolean.
*/
public function testConvertBlockContentTypeRevisionFlagToBoolean(): void {
$no_new_revisions = BlockContentType::create([
'id' => 'no_new_revisions',
'label' => 'Does not create new revisions',
'revision' => 0,
]);
$no_new_revisions->trustData()->save();
$new_revisions = BlockContentType::create([
'id' => 'new_revisions',
'label' => 'Creates new revisions',
'revision' => 1,
]);
$new_revisions->trustData()->save();
// Ensure that an integer was stored, so we can be sure that the update
// path converts it to a boolean.
$this->assertSame(0, $no_new_revisions->get('revision'));
$this->assertSame(1, $new_revisions->get('revision'));
$this->runUpdates();
$this->assertFalse(BlockContentType::load('no_new_revisions')->get('revision'));
$this->assertTrue(BlockContentType::load('new_revisions')->get('revision'));
}
/**
* Tests moving the content block library to Content.
*

View File

@ -17,6 +17,11 @@ class BlockContentTypeValidationTest extends ConfigEntityValidationTestBase {
*/
protected static $modules = ['block_content'];
/**
* {@inheritdoc}
*/
protected static array $propertiesWithOptionalValues = ['description'];
/**
* {@inheritdoc}
*/

View File

@ -91,7 +91,7 @@ class BlockContentTypeTest extends ConfigEntityResourceTestBase {
'description' => 'Provides a competitive alternative to the "basic" type',
'label' => 'Pascal',
'langcode' => 'en',
'revision' => 0,
'revision' => FALSE,
'status' => TRUE,
'drupal_internal__id' => 'pascal',
],

View File

@ -3,5 +3,5 @@ status: true
dependencies: { }
id: banner_block
label: 'Banner block'
revision: 0
revision: false
description: 'A banner block contains a title, summary, link to content and a background image. The background image is scaled to fill the browser''s width.'

View File

@ -3,5 +3,5 @@ status: true
dependencies: { }
id: basic
label: 'Basic block'
revision: 0
revision: false
description: 'A basic block contains a title and a body.'

View File

@ -3,5 +3,5 @@ status: true
dependencies: { }
id: disclaimer_block
label: 'Disclaimer block'
revision: 0
revision: false
description: 'A disclaimer block contains disclaimer and copyright text.'

View File

@ -3,5 +3,5 @@ status: true
dependencies: { }
id: footer_promo_block
label: 'Footer promo block'
revision: 0
revision: false
description: 'A footer promo block contains a title, promo text, and a "find out more" link.'

View File

@ -3,5 +3,5 @@ status: true
dependencies: { }
id: basic
label: 'Basic block'
revision: 0
revision: false
description: 'A basic block contains a title and a body.'