You are here

protected function TaxonomyPermissionsTestBase::createField in Taxonomy Permissions 8

Creates a taxonomy term reference field on the specified bundle.

Parameters

string $entity_type: The type of entity the field will be attached to.

string $bundle: The bundle name of the entity the field will be attached to.

string $field_name: The name of the field; if it already exists, a new instance of existing field will be created.

string $field_label: The label of the field.

string $target_entity_type: The type of the referenced entity.

string $selection_handler: The selection handler used by this field.

array $selection_handler_settings: An array of settings supported by the selection handler specified above. (e.g. 'target_bundles', 'sort', 'auto_create', etc).

int $cardinality: The cardinality of the field.

string $user_method: The method used for granting access to user.

int $priority: The priority access.

Return value

\Drupal\field\Entity\FieldConfig The field created or loaded.

See also

\Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()

1 call to TaxonomyPermissionsTestBase::createField()
TaxonomyPermissionsTestBase::attachFields in src/Tests/TaxonomyPermissionsTestBase.php
Helper function to create and attach a field to a node.

File

src/Tests/TaxonomyPermissionsTestBase.php, line 214

Class

TaxonomyPermissionsTestBase
General setup and helper function for testing taxonomy permissions module.

Namespace

Drupal\taxonomy_permissions\Tests

Code

protected function createField($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', array $selection_handler_settings = [], $cardinality = 1, $user_method = 'user', $priority = 0) {

  // Look for or add the specified field to the requested entity bundle.
  if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
    FieldStorageConfig::create([
      'field_name' => $field_name,
      'type' => 'entity_reference',
      'entity_type' => $entity_type,
      'cardinality' => $cardinality,
      'settings' => [
        'target_type' => $target_entity_type,
      ],
    ])
      ->save();
  }
  if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
    FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'label' => $field_label,
      'settings' => [
        'handler' => $selection_handler,
        'handler_settings' => $selection_handler_settings,
      ],
    ])
      ->save();
  }
  $field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
  return $field;
}