You are here

function editableviews_entity_save in Editable Views 7

Saves the entities in $entities array.

Parameters

array $entities: Entities to save.

string $save_messages: Configuration setting about messages to show to end users. It must be one of the values below:

  • 'none': Show no messages.
  • 'summary': Show a single message summarizing the changes.
  • 'individual': Show a message for each saved entity.
1 call to editableviews_entity_save()
editableviews_entity_form_submit_save in ./editableviews.module
Form submit handler the second: save entities.
1 string reference to 'editableviews_entity_save'
editableviews_entity_form_submit_save in ./editableviews.module
Form submit handler the second: save entities.

File

./editableviews.module, line 354
editableviews.module Contain module code. @todo:

Code

function editableviews_entity_save($entities, $save_messages) {
  $entity_info = entity_get_info();
  $save_message_labels = array();
  foreach (array_keys($entities) as $entity_type) {
    foreach ($entities[$entity_type] as $entity_id => $entity) {

      // Check whether a save has been requested.
      if (empty($entity->editableviews_needs_save)) {
        continue;
      }

      // Save the entity using Entity API.
      entity_save($entity_type, $entity);

      // If the entity was a new one, and on a forward relationship, we need
      // to set the referring entity to point to it. We can do this now that the
      // new entity has been saved and has an id.
      if (isset($entity->editableviews_future_reference)) {
        list($new_entity_id, ) = entity_extract_ids($entity_type, $entity);
        $referring_entity = $entities[$entity->editableviews_future_reference['entity_type']][$entity->editableviews_future_reference['entity_id']];
        $wrapper = entity_metadata_wrapper($entity->editableviews_future_reference['entity_type'], $referring_entity);

        // Make the existing entity point to the new entity.
        $relationship_field_name = $entity->editableviews_future_reference['field_name'];
        $wrapper->{$relationship_field_name}
          ->set($new_entity_id);

        // This needs to be saved a second time, as there's no simple way we can
        // guarantee the order we come to these in: they are in the order we
        // encounter them going through the field handlers.
        entity_save($entity->editableviews_future_reference['entity_type'], $referring_entity);
      }
      if ($save_messages == 'individual') {

        // Show a confirmation message. This could get pretty long on a big View!
        drupal_set_message(t("Saved %entity-type entity %label.", array(
          '%entity-type' => $entity_info[$entity_type]['label'],
          '%label' => entity_label($entity_type, $entity),
        )));
      }
      elseif ($save_messages == 'summary') {

        // Use drupal_placeholder() to format this the same as %label above.
        $save_message_labels[] = drupal_placeholder(entity_label($entity_type, $entity));
      }
    }
  }
  if ($save_messages == 'summary' && count($save_message_labels)) {
    drupal_set_message(t("Saved entities !labels.", array(
      // These have been sanitized already.
      '!labels' => implode(', ', $save_message_labels),
    )));
  }
}