function editableviews_style_helper::connect_new_entities in Editable Views 7
Sets the properties so that new entities connect to existing ones.
For a forward relationship, the existing entity must know it has to point to the new entity once it has been saved. For a reverse relationship, the new entity must have the right property set (e.g. an entityreference field) so that it point back to the existing entity.
Parameters
$result_entities: The array of result entities from the style plugin. Passed by reference so the entities can be altered. (TODO: is this actually needed??)
$results_coordinates: The combined coordinates array, containing both the forward and reverse lookups for entities and results. @see editableviews_plugin_style_row_edit_table::get_form() for details.
$edit_field_handlers_grouped: The edit field handlers, grouped by relationship handler ID.
File
- ./
editableviews_plugin_style_row_edit_table.inc, line 630
Class
- editableviews_style_helper
- Helper class for the style plugin.
Code
function connect_new_entities(&$result_entities, $results_coordinates, $edit_field_handlers_grouped) {
$relationship_handlers = $this->plugin->display->handler
->get_handlers('relationship');
//dsm($edit_field_handlers_grouped);
//dsm($result_entities);
foreach (array_keys($result_entities) as $entity_type) {
foreach ($result_entities[$entity_type] as $entity_id => $entity) {
// New entities have a non-numeric fake id we just gave them.
if (!is_numeric($entity_id)) {
// Get the views coordinates for this entity.
list($relationship_id, $index) = $results_coordinates['entities_to_results'][$entity_type][$entity_id];
$relationship_handler = $relationship_handlers[$relationship_id];
//dsm($relationship_handler);
// Get the relationship that the relationship is on, so we can then
// get the entity for that relationship.
if (isset($relationship_handler->relationship)) {
$relationship_relationship = $relationship_handler->relationship;
}
else {
$relationship_relationship = $this->plugin->view->base_table;
}
// Only act if the new entity's relationship has editable fields:
// otherwise it's just an empty bunch of table cells, and there's
// nothing to connect to or from.
if (count($edit_field_handlers_grouped[$relationship_relationship]) == 0) {
continue;
}
if ($relationship_handler->definition['editableviews_direction'] == 'forward') {
// Get the entity on our relationship's relationship -- same
// as for reverse.
// Get the entity out of the imaginary Views grid that is on the same
// row as us, and in the $relationship_relationship relationship...
list($referring_entity_type, $referring_entity_id) = $results_coordinates['results_to_entities'][$relationship_relationship][$index];
$referring_entity = $result_entities[$referring_entity_type][$referring_entity_id];
// Store this entity's details on the current, new entity, so that
// when (and if!) we save it, we can go and make the referring
// entity point to it.
$entity->editableviews_future_reference = array(
'entity_type' => $referring_entity_type,
'entity_id' => $referring_entity_id,
'field_name' => $relationship_handler->definition['field_name'],
);
}
else {
// Would be nice to factor this out to a helper method, say
// '$this->new_entity_set_reverse_connection()' but we'd need to
// pass so many variables it's probably just as faffy.
// Get the entity out of the imaginary Views grid that is on the same
// row as us, and in the $relationship_relationship relationship...
list($referred_entity_type, $referred_entity_id) = $results_coordinates['results_to_entities'][$relationship_relationship][$index];
$referred_entity = $result_entities[$referred_entity_type][$referred_entity_id];
// From here on, this is just reverse relationships!
$wrapper = entity_metadata_wrapper($entity_type, $entity);
// This is what we need to set on the new entity in a reverse relationship.
$relationship_field_name = $relationship_handler->definition['field_name'];
// Make the new entity point to the entity on its relationship's
// relationship.
$wrapper->{$relationship_field_name}
->set($referred_entity_id);
}
}
}
}
}