function DateTestBase::createField

Same name in other branches
  1. 9 core/modules/datetime/tests/src/Functional/DateTestBase.php \Drupal\Tests\datetime\Functional\DateTestBase::createField()
  2. 8.9.x core/modules/datetime/src/Tests/DateTestBase.php \Drupal\datetime\Tests\DateTestBase::createField()
  3. 8.9.x core/modules/datetime/tests/src/Functional/DateTestBase.php \Drupal\Tests\datetime\Functional\DateTestBase::createField()
  4. 10 core/modules/datetime/tests/src/Functional/DateTestBase.php \Drupal\Tests\datetime\Functional\DateTestBase::createField()

Creates a date test field.

1 call to DateTestBase::createField()
DateTestBase::setUp in core/modules/datetime/tests/src/Functional/DateTestBase.php

File

core/modules/datetime/tests/src/Functional/DateTestBase.php, line 116

Class

DateTestBase
Provides a base class for testing Datetime field functionality.

Namespace

Drupal\Tests\datetime\Functional

Code

protected function createField() {
    $field_name = $this->randomMachineName();
    $field_label = Unicode::ucfirst($this->randomMachineName());
    $type = $this->getTestFieldType();
    $widget_type = $formatter_type = $type . '_default';
    $this->fieldStorage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'entity_test',
        'type' => $type,
        'settings' => [
            'datetime_type' => DateTimeItem::DATETIME_TYPE_DATE,
        ],
    ]);
    $this->fieldStorage
        ->save();
    $this->field = FieldConfig::create([
        'field_storage' => $this->fieldStorage,
        'label' => $field_label,
        'bundle' => 'entity_test',
        'description' => 'Description for ' . $field_label,
        'required' => TRUE,
    ]);
    $this->field
        ->save();
    EntityFormDisplay::load('entity_test.entity_test.default')->setComponent($field_name, [
        'type' => $widget_type,
    ])
        ->save();
    $this->displayOptions = [
        'type' => $formatter_type,
        'label' => 'hidden',
        'settings' => [
            'format_type' => 'medium',
        ],
    ];
    EntityViewDisplay::create([
        'targetEntityType' => $this->field
            ->getTargetEntityTypeId(),
        'bundle' => $this->field
            ->getTargetBundle(),
        'mode' => 'full',
        'status' => TRUE,
    ])
        ->setComponent($field_name, $this->displayOptions)
        ->save();
}

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