You are here

protected function CommentAlterTestBase::addField in Comment Alter 8

Adds a field to the entity_test entity type.

Parameters

string $field_type: The field type name (Eg. text).

string $widget_type: The widget name (Eg. text_textfield).

array $storage_settings: (optional) A list of field storage settings that will be added to the field.

array $field_settings: (optional) A list of instance settings that will be added to the field's instance.

boolean $comment_alter: (optional) Option to enable/disable comment_alter for this field.

Return value

string The name of the field that was created.

4 calls to CommentAlterTestBase::addField()
CommentAlterImageTest::addImageField in tests/src/Functional/CommentAlterImageTest.php
Adds an image field to the parent enity.
CommentAlterListStringTest::addOptionField in tests/src/Functional/CommentAlterListStringTest.php
Adds an Option Field to the parent enity.
CommentAlterNodeReferenceTest::addNodeReferenceField in tests/src/Functional/CommentAlterNodeReferenceTest.php
Adds a node reference field to the parent enity.
CommentAlterTextTest::addTextField in tests/src/Functional/CommentAlterTextTest.php
Adds an Text Field to the parent enity.

File

tests/src/Functional/CommentAlterTestBase.php, line 108

Class

CommentAlterTestBase
Base class for Comment Alter test cases.

Namespace

Drupal\Tests\comment_alter\Functional

Code

protected function addField($field_type, $widget_type, $storage_settings = [], $field_settings = [], $comment_alter = TRUE) {
  $field_name = Unicode::strtolower($this
    ->randomMachineName() . '_field_name');
  FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => $this->entityType,
    'type' => $field_type,
  ] + $storage_settings)
    ->save();
  FieldConfig::create([
    'field_name' => $field_name,
    'entity_type' => $this->entityType,
    'bundle' => $this->bundle,
    'settings' => $field_settings,
    'widget' => [
      'type' => $widget_type,
    ],
    'third_party_settings' => [
      'comment_alter' => [
        'comment_alter_enabled' => $comment_alter,
      ],
    ],
  ])
    ->save();

  // By default the added field is hidden so enable it and set the widget
  // type.
  entity_get_form_display($this->entityType, $this->bundle, 'default')
    ->setComponent($field_name, [
    'type' => $widget_type,
  ])
    ->save();

  // Invalidate cache after selecting comment_alter option for our field.
  \Drupal::cache()
    ->delete('comment_alter_fields:' . $this->entityType . ':' . $this->bundle);
  return $field_name;
}