/**
* Alter values before they go through the sanitize & save routine.
*
* Here, we're enforcing $post_limit.
*
* @param array $values Field values being saved. This will either be
* an array of ints if this is a singular field,
* or an array of array of ints if $limit != 1.
* @param array $current_values Field's previous values.
* @return array Altered values.
*/
public function presave_alter_values($values, $current_values = array())
{
if ($this->post_limit > 0) {
if (!empty($values[0]) && is_array($values[0])) {
// If this is an array of arrays, limit each individually
$values = array_filter($values);
foreach ($values as $i => $value) {
if (!is_array($value)) {
unset($values[$i]);
} else {
$values[$i] = array_slice($value, 0, $this->post_limit);
}
}
} elseif (is_array($values)) {
// this is an array of ints, so we can enforce the limit on it
$values = array_slice($values, 0, $this->post_limit);
}
}
return parent::presave_alter_values($values, $current_values);
}