You are here

protected function DateTestBase::createField in Drupal 8

Same name in this branch
  1. 8 core/modules/datetime/src/Tests/DateTestBase.php \Drupal\datetime\Tests\DateTestBase::createField()
  2. 8 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/src/Tests/DateTestBase.php
Sets up a Drupal site for running functional and integration tests.

File

core/modules/datetime/src/Tests/DateTestBase.php, line 117

Class

DateTestBase
Provides a base class for testing Datetime field functionality.

Namespace

Drupal\datetime\Tests

Code

protected function createField() {
  $field_name = mb_strtolower($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,
    'bundle' => 'entity_test',
    'description' => 'Description for ' . $field_name,
    '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',
    ] + $this->defaultSettings,
  ];
  EntityViewDisplay::create([
    'targetEntityType' => $this->field
      ->getTargetEntityTypeId(),
    'bundle' => $this->field
      ->getTargetBundle(),
    'mode' => 'full',
    'status' => TRUE,
  ])
    ->setComponent($field_name, $this->displayOptions)
    ->save();
}