You are here

public function GatherContentFunctions::addMediaToEntity in GatherContent 7.2

Used during media import to add downloaded media to node.

File

includes/functions.inc, line 59
Class containing general functions used throughout the importing. Extended by GatherContentCurl.

Class

GatherContentFunctions
@file Class containing general functions used throughout the importing. Extended by GatherContentCurl.

Code

public function addMediaToEntity(&$entity, $is_file_field, $file_obj, $field, $more_than_1, $field_type, $counter) {
  if ($is_file_field) {
    $file_val = array(
      'fid' => $file_obj->fid,
      'display' => TRUE,
    );
    if (strpos($field_type, 'list') === 0) {
      $cur_val = $entity->{$field}
        ->value();
      if (!is_array($cur_val)) {
        $cur_val = array();
      }
      $cur_val[] = $file_val;
      $entity->{$field}
        ->set($cur_val);
    }
    else {
      $entity->{$field}
        ->set($file_val);
    }
  }
  else {
    $value = $entity->{$field}
      ->value();
    if ($field_type == 'text_formatted') {
      $value['value'] = $this
        ->addMediaToContent($value['value'], file_create_url($file_obj->uri), $file_obj->filename, $file_obj->filemime, $value['format'], $more_than_1, $counter);
      if (isset($value['safe_value'])) {
        unset($value['safe_value']);
      }
    }
    else {
      $value = $this
        ->addMediaToContent($value, file_create_url($file_obj->uri), $file_obj->filename, $file_obj->filemime, 'plain_text', $more_than_1, $counter);
    }
    $entity->{$field}
      ->set($value);
  }
  $entity
    ->save();
}