I'm trying to write a FIeldtype that stores tags.
The basic idea is that it uses a standard Text Inputfield and takes the string entered, separates the values by comma and then saves each one as a tag. WHen it loads the values I preferably want the values to remain as an array of item in the page data, but be displayed as a simple comma-seperated lsit inthe InputField.
I'm extending a FieldTypeMulti but I see mto be running into some sort of problem when there is no value associated with the filed in the DB, as it displays "WireArray(0)" in the InputField.
I was originally taking the WireArray being returned from the DB and concatenating (using PHP implode() method) back into a string so the InputFIeld can display it.
I've gone round in circles and I can't work out exactly what I have t odo to successfully either:
A. Keep the data as an array from the DB, but successfully display it as a string in the InputFIeld.
OR
B. Convert the array from the DB into a simple string, and I can then re-explode it into individual tags if I want to iterate them in my template.
An extract of my FieldTypeTags code looks like this at present, although I've tried about 5 different things before this!!
public function ___sleepValue(Page $page, Field $field, $value) {
$data = array();
foreach(explode(',', $value) as $tag) {
$data[] = array('data' => trim($tag));
}
$clean = array_filter($data, function($item) {
return !empty($item['data']);
});
return $clean;
}
public function ___wakeupValue(Page $page, Field $field, $value) {
return implode(', ', $value);
}
public function sanitizeValue(Page $page, Field $field, $value) {
return $value;
}
I'm going absolutely mad with this. I can successfully store the tags in the DB as separate values, whcih is great for searching and doing tag cloud counts etc, but I can't get the InputField to behave properly when there is nothing stored for the field.