removeAllSections example

    // outside the layout, the configurable field is not shown at all, and the     // layout itself is rendered (but empty).     $new_expected_fields = [
      'field field--name-name field--type-string field--label-hidden field__item',
      'clearfix text-formatted field field--name-test-display-non-configurable field--type-text field--label-above',
      'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
    ];
    $this->assertFieldAttributes($this->entity, $new_expected_fields);
    $this->assertNotEmpty($this->cssSelect('.layout--onecol'));

    // Removing the layout restores the original rendering of the entity.     $this->entity->get(OverridesSectionStorage::FIELD_NAME)->removeAllSections();
    $this->entity->save();
    $this->assertFieldAttributes($this->entity, $expected_fields);

    // Test that adding a new field after Layout Builder has been installed will     // add the new field to the default region of the first section.     $field_storage = FieldStorageConfig::create([
      'entity_type' => 'entity_test_base_field_display',
      'field_name' => 'test_field_display_post_install',
      'type' => 'text',
    ]);
    $field_storage->save();
    
    // outside the layout, the configurable field is not shown at all, and the     // layout itself is rendered (but empty).     $new_expected_fields = [
      'field field--name-name field--type-string field--label-hidden field__item',
      'clearfix text-formatted field field--name-test-display-non-configurable field--type-text field--label-above',
      'clearfix text-formatted field field--name-test-display-multiple field--type-text field--label-above',
    ];
    $this->assertFieldAttributes($this->entity, $new_expected_fields);
    $this->assertNotEmpty($this->cssSelect('.layout--onecol'));

    // Removing the layout restores the original rendering of the entity.     $field_list->removeAllSections();
    $this->entity->save();
    $this->assertFieldAttributes($this->entity, $expected_fields);
  }

}
$entity = EntityTest::create();
    $entity->set(OverridesSectionStorage::FIELD_NAME, [new Section('layout_onecol')]);
    $entity->save();
    $entity = EntityTest::load($entity->id());

    $context = EntityContext::fromEntity($entity);
    $this->plugin->setContext('entity', $context);

    $this->assertTrue($this->plugin->isOverridden());
    $this->plugin->removeSection(0);
    $this->assertTrue($this->plugin->isOverridden());
    $this->plugin->removeAllSections(TRUE);
    $this->assertTrue($this->plugin->isOverridden());
    $this->plugin->removeAllSections();
    $this->assertFalse($this->plugin->isOverridden());
  }

}
// Mark this as an administrative page for JavaScript ("Back to site" link).     $form['#attached']['drupalSettings']['path']['currentPathIsAdmin'] = TRUE;
    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Remove all sections.     $this->sectionStorage
      ->removeAllSections()
      ->save();
    $this->layoutTempstoreRepository->delete($this->sectionStorage);

    $this->messenger->addMessage($this->t('The layout has been reverted back to defaults.'));
    $form_state->setRedirectUrl($this->sectionStorage->getRedirectUrl());
  }

}

  public function removeSection($delta) {
    $this->getSectionList()->removeSection($delta);
    return $this;
  }

  /** * {@inheritdoc} */
  public function removeAllSections($set_blank = FALSE) {
    $this->getSectionList()->removeAllSections($set_blank);
    return $this;
  }

  /** * {@inheritdoc} */
  public function getContextsDuringPreview() {
    $contexts = $this->getContexts();

    // view_mode is a required context, but SectionStorage plugins are not     // required to return it (for example, the layout_library plugin provided
// Loop through all existing field-based components and add them as         // section-based components.         $components = $this->getComponents();
        // Sort the components by weight.         uasort($components, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
        foreach ($components as $name => $component) {
          $this->setComponent($name$component);
        }
      }
      else {
        // When being disabled, remove all existing section data.         $this->removeAllSections();
      }
    }
  }

  /** * Removes a layout section field if it is no longer needed. * * Because the field is shared across all view modes, the field will only be * removed if no other view modes are using it. * * @param string $entity_type_id * The entity type ID. * @param string $bundle * The bundle. * @param string $field_name * The name for the layout section field. */
$this->setSection($delta$section);
    return $this;
  }

  /** * {@inheritdoc} */
  public function insertSection($delta, Section $section) {
    // Clear the section list if there is currently a blank section.     if ($this->hasBlankSection()) {
      $this->removeAllSections();
    }

    if ($this->hasSection($delta)) {
      // @todo Use https://www.drupal.org/node/66183 once resolved.       $start = array_slice($this->getSections(), 0, $delta);
      $end = array_slice($this->getSections()$delta);
      $this->setSections(array_merge($start[$section]$end));
    }
    else {
      $this->appendSection($section);
    }
    
$this->sectionList->appendSection(new Section('layout_onecol'));
    $this->assertSections($expected);
  }

  /** * @covers ::removeAllSections * * @dataProvider providerTestRemoveAllSections */
  public function testRemoveAllSections($set_blank$expected) {
    if ($set_blank === NULL) {
      $this->sectionList->removeAllSections();
    }
    else {
      $this->sectionList->removeAllSections($set_blank);
    }
    $this->assertSections($expected);
  }

  /** * Provides test data for ::testRemoveAllSections(). */
  public function providerTestRemoveAllSections() {
    
Home | Imprint | This part of the site doesn't use cookies.