You are here

public function CommerceFileLicenseEntity::set_file in Commerce File 7

Set file field item values

  • Only can set if no line item references
  • Ensures fid is provided
  • 'data' is merged with the current file and field instance's defaults

File

includes/commerce_file_license.entity.inc, line 399
Provides a base class for CommerceFileLicenseEntity.

Class

CommerceFileLicenseEntity
A Commerce File License entity class.

Code

public function set_file($field_value = array()) {
  if (!empty($field_value)) {
    $wrapper = $this
      ->get_wrapper();
    $license_line_items_field_name = _commerce_file_get_field_names('license_line_items');
    if (!empty($field_value['fid']) && (empty($wrapper->{$license_line_items_field_name}) || !$wrapper->{$license_line_items_field_name}
      ->count())) {

      // allow changing only if no line items
      $field_name = _commerce_file_get_field_names('license_file');
      $field_instance = field_info_instance($this->entityType, $field_name, $this
        ->bundle());
      $current_file = $this
        ->get_file();
      $default_data = array();
      if (!empty($current_file['data'])) {
        $default_data += $current_file['data'];
      }
      if (!empty($field_instance['settings']['data'])) {
        $default_data += $field_instance['settings']['data'];
      }

      // ensure data is set
      if (empty($field_value['data'])) {
        $field_value['data'] = array();
      }

      // merge data defaults
      $field_value['data'] += $default_data;

      // set file field value
      $this
        ->_set_field_value($field_name, $field_value, 'fid');
      $this->file = NULL;
      return TRUE;
    }
  }
  return FALSE;
}