Commit 11c55fb3 authored by catch's avatar catch
Browse files

fix: #3593466 View mode form crashes if there are no bundles

By: sunlix
By: dcam
(cherry picked from commit 4f0c2dc6)
parent f883bb85
Loading
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -117,8 +117,9 @@ public function form(array $form, FormStateInterface $form_state) {

    $bundles_by_entity = [];
    $defaults = [];
    foreach (array_keys($bundles[$definition->id()]) as $bundle) {
      $bundles_by_entity[$bundle] = $bundles[$definition->id()][$bundle]['label'];
    $entity_bundles = $bundles[$definition->id()] ?? [];
    foreach (array_keys($entity_bundles) as $bundle) {
      $bundles_by_entity[$bundle] = $entity_bundles[$bundle]['label'];
      // Determine default display modes.
      if (!$this->entity->isNew()) {
        [, $display_mode_name] = explode('.', $this->entity->id());
+30 −0
Original line number Diff line number Diff line
@@ -11,9 +11,11 @@
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\Core\Form\FormState;
use Drupal\entity_test\EntityTestHelper;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field_ui\Form\EntityDisplayModeAddForm;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\Role;
@@ -549,6 +551,34 @@ public function testGetDisplayModeOptions(): void {
    $this->assertEquals(['default' => 'Default', 'register' => 'Register'], $form_modes);
  }

  /**
   * Tests building the view mode add form when an entity type has no bundles.
   *
   * This is a regression test for
   * https://www.drupal.org/project/drupal/issues/3593466.
   */
  public function testViewModeAddFormWithoutBundles(): void {
    $view_mode = EntityViewMode::create([
      'id' => 'node.test',
      'label' => 'Test view mode',
      'targetEntityType' => 'node',
    ]);
    $view_mode->save();

    $form_object = EntityDisplayModeAddForm::create($this->container);
    $form_object->setEntityTypeManager($this->container->get('entity_type.manager'));
    $form_object->setModuleHandler($this->container->get('module_handler'));
    $form_object->setEntity($view_mode);

    $form = $form_object->buildForm([], new FormState(), 'node');

    $this->assertSame([], $this->container->get('entity_type.bundle.info')->getBundleInfo('node'));
    $this->assertArrayHasKey('id', $form);
    $this->assertArrayHasKey('label', $form);
    $this->assertArrayHasKey('bundles_by_entity', $form);
    $this->assertSame([], $form['bundles_by_entity']['#options']);
  }

  /**
   * Tests components dependencies additions.
   */