Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Currently, any handler wishing to override prepare() to do some sort of processing on incoming values has to perform two chunks of housekeeping at the start and end of the method:
public function prepare($entity, array $field_info, array $instance, array $values) {
// ********** DEAL WITH ARGUMENTS......
$arguments = array();
if (isset($values['arguments'])) {
$arguments = $values['arguments'];
unset($values['arguments']);
}
// ********** STOP.
$language = $this->getFieldLanguage($entity, $field_info, $arguments);
// Let the derived class skip empty values.
if ($this->skipEmpty) {
$values = array_filter($values, array($this, 'notNull'));
}
// ********** Prepare the array for field API
// Setup the Field API array for saving.
$delta = 0;
foreach ($values as $value) {
if (is_array($language)) {
$current_language = $language[$delta];
}
else {
$current_language = $language;
}
$return[$current_language][] = array($this->fieldValueKey => $value);
$delta++;
}
return isset($return) ? $return : NULL;
}
As far as I can tell, these are standard for all field types, and merely have to be copied and pasted into subclasses. It would be cleaner to have both of these in helper methods, ideally which the subclass doesn't have to deal with at all.
Comments
Comment #1
pifagor