Unverified Commit 9d81d2cd authored by larowlan's avatar larowlan

Issue #2914484 by tim.plunkett, larowlan, xjm: Prevent the layout field from...

Issue #2914484 by tim.plunkett, larowlan, xjm: Prevent the layout field from being removed if overrides exist
parent bbfa34e7
......@@ -54,8 +54,6 @@ public function form(array $form, FormStateInterface $form_state) {
];
$entity_type = $this->entityTypeManager->getDefinition($this->entity->getTargetEntityTypeId());
// @todo Unchecking this box is a destructive action, this should be made
// clear to the user in https://www.drupal.org/node/2914484.
$form['layout']['allow_custom'] = [
'#type' => 'checkbox',
'#title' => $this->t('Allow each @entity to have its layout customized.', [
......@@ -63,12 +61,41 @@ public function form(array $form, FormStateInterface $form_state) {
]),
'#default_value' => $this->entity->isOverridable(),
];
$form['#entity_builders'][] = '::entityFormEntityBuild';
// Prevent turning off overrides while any exist.
if ($this->hasOverrides($this->entity)) {
$form['layout']['allow_custom']['#disabled'] = TRUE;
$form['layout']['allow_custom']['#description'] = $this->t('You must revert all customized layouts of this display before you can disable this option.');
}
else {
$form['#entity_builders'][] = '::entityFormEntityBuild';
}
}
return $form;
}
/**
* Determines if the defaults have any overrides.
*
* @param \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $display
* The entity display.
*
* @return bool
* TRUE if there are any overrides of this default, FALSE otherwise.
*/
protected function hasOverrides(LayoutEntityDisplayInterface $display) {
if (!$display->isOverridable()) {
return FALSE;
}
$entity_type = $this->entityTypeManager->getDefinition($display->getTargetEntityTypeId());
$query = $this->entityTypeManager->getStorage($display->getTargetEntityTypeId())->getQuery()
->exists('layout_builder__layout');
if ($bundle_key = $entity_type->getKey('bundle')) {
$query->condition($bundle_key, $display->getTargetBundle());
}
return (bool) $query->count()->execute();
}
/**
* Entity builder for layout options on the entity view display form.
*/
......
......@@ -123,6 +123,10 @@ public function testLayoutBuilderUi() {
$assert_session->pageTextNotContains('The first node body');
$assert_session->pageTextNotContains('Powered by Drupal');
// Assert that overrides cannot be turned off while overrides exist.
$this->drupalGet("$field_ui_prefix/display/default");
$assert_session->fieldDisabled('layout[allow_custom]');
// Alter the defaults.
$this->drupalGet("$field_ui_prefix/display-layout/default");
$assert_session->linkExists('Add Block');
......@@ -157,6 +161,11 @@ public function testLayoutBuilderUi() {
$assert_session->pageTextContains('The first node body');
$assert_session->pageTextContains('Powered by Drupal');
// Assert that overrides can be turned off now that all overrides are gone.
$this->drupalPostForm("$field_ui_prefix/display/default", ['layout[allow_custom]' => FALSE], 'Save');
$this->drupalGet('node/1');
$assert_session->linkNotExists('Layout');
// Add a new field.
$edit = [
'new_storage_type' => 'string',
......
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