function FileFieldTestBase::attachFileField

Attaches a file field to an entity.

Parameters

$name: The name of the new field (all lowercase), exclude the "field_" prefix.

$entity_type: The entity type this field will be added to.

$bundle: The bundle this field will be added to.

$field_settings: A list of field settings that will be added to the defaults.

$instance_settings: A list of instance settings that will be added to the instance defaults.

$widget_settings: A list of widget settings that will be added to the widget defaults.

2 calls to FileFieldTestBase::attachFileField()
FileFieldRevisionTest::testRevisions in drupal/core/modules/file/lib/Drupal/file/Tests/FileFieldRevisionTest.php
Tests creating multiple revisions of a node and managing attached files.
FileFieldTestBase::createFileField in drupal/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php
Creates a new file field.

File

drupal/core/modules/file/lib/Drupal/file/Tests/FileFieldTestBase.php, line 98
Definition of Drupal\file\Tests\FileFieldTestBase.

Class

FileFieldTestBase
Provides methods specifically for testing File module's field handling.

Namespace

Drupal\file\Tests

Code

function attachFileField($name, $entity_type, $bundle, $instance_settings = array(), $widget_settings = array()) {
  $instance = array(
    'field_name' => $name,
    'label' => $name,
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'required' => !empty($instance_settings['required']),
    'settings' => array(),
  );
  $instance['settings'] = array_merge($instance['settings'], $instance_settings);
  field_create_instance($instance);
  entity_get_form_display($entity_type, $bundle, 'default')
    ->setComponent($name, array(
    'type' => 'file_generic',
    'settings' => $widget_settings,
  ))
    ->save();
}