You are here

trait FieldCreationTrait in Mini site 8

Provides methods for creating minisite fields.

Hierarchy

File

tests/src/Functional/FieldCreationTrait.php, line 12

Namespace

Drupal\Tests\minisite\Functional
View source
trait FieldCreationTrait {

  /**
   * Creates a new ministe field.
   *
   * @param string $name
   *   The name of the new field (all lowercase), exclude the "field_" prefix.
   * @param string $entity_type
   *   The entity type.
   * @param string $bundle
   *   The bundle that this field will be added to.
   * @param array $storage_settings
   *   A list of field storage settings that will be added to the defaults.
   * @param array $field_settings
   *   A list of instance settings that will be added to the instance defaults.
   * @param array $widget_settings
   *   A list of widget settings that will be added to the widget defaults.
   *
   * @return \Drupal\field\FieldStorageConfigInterface
   *   The minisite field.
   */
  public function createMinisiteField($name, $entity_type, $bundle, array $storage_settings = [], array $field_settings = [], array $widget_settings = []) {

    /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
    $field_storage = FieldStorageConfig::create([
      'entity_type' => $entity_type,
      'field_name' => $name,
      'type' => 'minisite',
      'settings' => $storage_settings,
      'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
    ]);
    $field_storage
      ->save();
    $this
      ->attachMinisiteField($name, $entity_type, $bundle, $field_settings, $widget_settings);
    return $field_storage;
  }

  /**
   * Attaches a file field to an entity.
   *
   * @param string $name
   *   The name of the new field (all lowercase), exclude the "field_" prefix.
   * @param string $entity_type
   *   The entity type this field will be added to.
   * @param string $bundle
   *   The bundle this field will be added to.
   * @param array $field_settings
   *   A list of field settings that will be added to the defaults.
   * @param array $widget_settings
   *   A list of widget settings that will be added to the widget defaults.
   */
  public function attachMinisiteField($name, $entity_type, $bundle, array $field_settings = [], array $widget_settings = []) {
    $field = [
      'field_name' => $name,
      'label' => $name,
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'required' => !empty($field_settings['required']),
      'settings' => $field_settings,
    ];
    FieldConfig::create($field)
      ->save();
    LegacyWrapper::getFormDisplay($entity_type, $bundle, 'default')
      ->setComponent($name, [
      'type' => 'file_generic',
      'settings' => $widget_settings,
    ])
      ->save();

    // Assign display settings.
    LegacyWrapper::getViewDisplay($entity_type, $bundle, 'default')
      ->setComponent($name, [
      'label' => 'hidden',
      'type' => 'file_default',
    ])
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldCreationTrait::attachMinisiteField public function Attaches a file field to an entity.
FieldCreationTrait::createMinisiteField public function Creates a new ministe field.