You are here

public function FillPdfTestHelper::createImageFieldEntity in FillPDF 7

Create an entity with an image.

Parameters

object $image: A file object representing the image to upload.

string $field_name: Name of the image field the image should be attached to.

string $entity_type: The entity's entity type.

string $bundle: The entity's bundle.

string $label: (optional) The entity's label.

Return value

int|false Entity ID of the created entity, or FALSE if the entity could not be created.

See also

ImageFieldTestCase::uploadNodeImage()

1 call to FillPdfTestHelper::createImageFieldEntity()
FillPdfMergeTestCase::testPdfMerging in tests/FillPdfMergeTestCase.test
Test PDF merging.

File

tests/FillPdfTestHelper.test, line 98

Class

FillPdfTestHelper
Helper functions for FillPDF testing.

Code

public function createImageFieldEntity($image, $field_name, $entity_type, $bundle, $label = NULL) {
  $info = entity_get_info($entity_type);
  if (empty($image->fid)) {

    // Create a managed file.
    file_save($image);
  }
  $entity = (object) array(
    'is_new' => TRUE,
    'language' => LANGUAGE_NONE,
  );
  if ($bundle_key = $info['entity keys']['bundle']) {
    $entity->{$bundle_key} = $bundle;
  }
  if ($label_key = $info['entity keys']['label']) {
    $entity->{$label_key} = isset($label) ? $label : $this
      ->randomName();
  }
  $entity->{$field_name} = array(
    LANGUAGE_NONE => array(
      0 => array(
        'fid' => $image->fid,
      ),
    ),
  );
  switch ($entity_type) {
    case 'node':
      node_save($entity);
      return $entity->nid;
    case 'taxonomy_term':
      $entity->vid = taxonomy_vocabulary_machine_name_load($bundle)->vid;
      $status = taxonomy_term_save($entity);
      return $status == SAVED_NEW ? $entity->tid : FALSE;
    case 'user':
      $user = user_save($entity);
      return $user->uid;
    default:
      if (module_exists('entity')) {
        entity_save($entity_type, $entity);
        $id_key = $info['entity keys']['id'];
        return (int) $entity->{$id_key};
      }
  }
  return FALSE;
}