I'm trying to use editablefields module on multifield subfields to allow editing of elements on the parent nodes view page or within views.
I have things pretty much working except that when editablefields calls entity_save() nothing actually gets written to the database.
entity_save is a no op because we don't have a save method on the entity, no "save callback" is defined for the entity info and MultifieldEntityController does not implement EntityAPIControllerInterface.
I cooked up a save callback that is working in my limited case
- use $entity->type to figure out the table holding the actual multifield data
- query the entity_type, bundle, entity_id, language, and delta from the field_data_$type table
- use the entity_type, bundle and entity_id to load the parent entity
- copy values from the pseudo entity into the correct field in the parent entity
- save the parent entity
/**
* Save a naked psuedo entity, generally called from entity_save.
* Only tested from editablefields module
* @param stdClass $entity
*/
function multifield_save_callback(stdClass $entity) {
// Query the field_data table for information needed to load the parent entity
$field_name = $entity->type;
$table = 'field_data_'.$field_name;
$id_column = $field_name."_id";
$row = db_select($table, 't')
->fields('t', array('entity_type', 'bundle', 'entity_id', 'language', 'delta'))
->condition($id_column, $entity->id, '=')
->execute()
->fetchObject();
$language = $row->language;
$delta = $row->delta;
// Now load the parent entity
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', $row->entity_type)
->entityCondition('bundle', $row->bundle)
->entityCondition('entity_id', $row->entity_id)
->execute();
if (isset($result[$row->entity_type])) {
$entity_ids = array_keys($result[$row->entity_type]);
$parent = entity_load_single($row->entity_type, $entity_ids[0]);
}
// Set values from the pseudo entity
foreach ($parent->{$field_name}[$language][$delta] as $key => $value) {
$parent->{$field_name}[$language][$delta][$key] = $entity->$key;
}
// save the parent entity
entity_save($row->entity_type, $parent);
}
I'm attaching a patch for the code I'm using.
Please note, that I've also applied patches for migrate field handler, views integration, and entity api support before working on this.
| Comment | File | Size | Author |
|---|---|---|---|
| multifield-savecallback.patch | 1.96 KB | bkat |
Comments