You are here

trait NameTestTrait in Name Field 8

Common testing traits.

Hierarchy

1 file declares its use of NameTestTrait
NameAutocompleteTest.php in tests/src/Kernel/NameAutocompleteTest.php

File

tests/src/Functional/NameTestTrait.php, line 11

Namespace

Drupal\Tests\name\Functional
View source
trait NameTestTrait {

  /**
   * Creates a name field with default settings.
   *
   * @param string $field_name
   *   The field name.
   * @param string $entity_type
   *   The entity type.
   * @param string $bundle
   *   The bundle.
   * @param array $storage_extras
   *   Additional field storage settings.
   *   - cardinality (int)
   *   - settings (array)
   * @param array $field_extras
   *   Additional field settings.
   *   - widget: ['type' => 'options_buttons']
   *   - settings (array)
   *
   * @return \Drupal\Core\Field\FieldDefinitionInterface
   *   The field definition.
   */
  public function createNameField($field_name, $entity_type, $bundle, array $storage_extras = [], array $field_extras = []) {
    FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'type' => 'name',
    ] + $storage_extras)
      ->save();
    $field_config = FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'type' => 'name',
      'bundle' => $bundle,
    ] + $field_extras);
    $field_config
      ->save();
    return $field_config;
  }

  /**
   * Forms an associative array from a linear array.
   *
   * @param array $values
   *   The arrays to combine.
   *
   * @return array
   *   The combined array.
   */
  public function mapAssoc(array $values) {
    return array_combine($values, $values);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NameTestTrait::createNameField public function Creates a name field with default settings.
NameTestTrait::mapAssoc public function Forms an associative array from a linear array.