function LayoutBuilderDefaultValuesTest::addTextFields

Same name in other branches
  1. 9 core/modules/layout_builder/tests/src/Functional/LayoutBuilderDefaultValuesTest.php \Drupal\Tests\layout_builder\Functional\LayoutBuilderDefaultValuesTest::addTextFields()
  2. 10 core/modules/layout_builder/tests/src/Functional/LayoutBuilderDefaultValuesTest.php \Drupal\Tests\layout_builder\Functional\LayoutBuilderDefaultValuesTest::addTextFields()

Helper function to add string fields.

1 call to LayoutBuilderDefaultValuesTest::addTextFields()
LayoutBuilderDefaultValuesTest::setUp in core/modules/layout_builder/tests/src/Functional/LayoutBuilderDefaultValuesTest.php

File

core/modules/layout_builder/tests/src/Functional/LayoutBuilderDefaultValuesTest.php, line 211

Class

LayoutBuilderDefaultValuesTest
Tests rendering default field values in Layout Builder.

Namespace

Drupal\Tests\layout_builder\Functional

Code

protected function addTextFields() : void {
    // String field with no default.
    $field_storage = FieldStorageConfig::create([
        'field_name' => 'field_string_no_default',
        'entity_type' => 'node',
        'type' => 'string',
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'test_node_type',
    ]);
    $field->save();
    // String field with default value.
    $field_storage = FieldStorageConfig::create([
        'field_name' => 'field_string_with_default',
        'entity_type' => 'node',
        'type' => 'string',
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'test_node_type',
    ]);
    $field->setDefaultValue('This is my default value');
    $field->save();
    // String field with default callback.
    $field_storage = FieldStorageConfig::create([
        'field_name' => 'field_string_with_callback',
        'entity_type' => 'node',
        'type' => 'string',
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'test_node_type',
    ]);
    $field->setDefaultValueCallback('Drupal\\Tests\\layout_builder\\Functional\\LayoutBuilderDefaultValuesTest::defaultValueCallback');
    $field->save();
    // String field with late default. We will set default later.
    $field_storage = FieldStorageConfig::create([
        'field_name' => 'field_string_late_default',
        'entity_type' => 'node',
        'type' => 'string',
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'test_node_type',
    ]);
    $field->save();
    
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $display_repository->getViewDisplay('node', 'test_node_type')
        ->setComponent('field_string_no_default', [
        'type' => 'string',
    ])
        ->setComponent('field_string_with_default', [
        'type' => 'string',
    ])
        ->setComponent('field_string_with_callback', [
        'type' => 'string',
    ])
        ->setComponent('field_string_late_default', [
        'type' => 'string',
    ])
        ->save();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.