Skip to content

Commit

Permalink
NEW Save/load info from CSV of IDs
Browse files Browse the repository at this point in the history
Allow mapping upload file IDs to a non-relationship field on a data object
as a comma separated list of IDs (and loading attachments from the same field)
instead of requiring a relationship to exist on a record.

Makes it easier to incorporate in other form types where a record doesn't
necessarily exist at the time of loading the form, but a value can be
set.
  • Loading branch information
Marcus Nyeholt committed Jul 2, 2015
1 parent c759c3f commit ee64916
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions code/FileAttachmentField.php
Expand Up @@ -188,11 +188,11 @@ public function saveInto(DataObjectInterface $record) {

if($relation = $this->getRelation()) {
$relation->setByIDList($this->Value());
}

elseif($record->has_one($fieldname)) {
} elseif($record->has_one($fieldname)) {
$record->{"{$fieldname}ID"} = $this->Value() ?: 0;
}
} elseif($record->hasField($fieldname)) {
$record->$fieldname = is_array($this->Value()) ? implode(',', $this->Value()) : $this->Value();
}

return $this;
}
Expand Down Expand Up @@ -608,6 +608,21 @@ public function AttachedFiles() {
}
}
}

if ($ids = $this->dataValue()) {
if (!is_array($ids)) {
$ids = explode(',', $ids);
}

$attachments = ArrayList::create();
foreach ($ids as $id) {
$file = File::get()->byID((int) $id);
if ($file && $file->canView()) {
$attachments->push($file);
}
}
return $attachments;
}

return false;
}
Expand Down

0 comments on commit ee64916

Please sign in to comment.