You are here

function editableviews_style_helper::entity_create in Editable Views 7

Returns a new (unsaved) entity for the given relationship ID.

This is needed when editable field handlers are on a non-required relationship, and a particular result row has no data there. We create a new entity for FieldAPI to work on, and potentially save it on submission if the user enters data.

Parameters

$relationship_id: The id of the relationship that requires a new entity.

Return value

A new, unsaved entity. The entity type is implied by the handler, and should be known by the caller. The bundle will be set on this, given by the style plugin's options.

File

./editableviews_plugin_style_row_edit_table.inc, line 567

Class

editableviews_style_helper
Helper class for the style plugin.

Code

function entity_create($relationship_id) {
  $entity_type = $this->relationship_entity_fields[$relationship_id]['entity_type'];

  // This is complex. We know the entity type, but we need to be told
  // the bundle: that's one for the plugin settings.
  // Then when it's created, we need to know how to set the relationship
  // field.
  $entity_info = entity_get_info($entity_type);

  // Assume this exists, as it must do if the entity is fieldable, and
  // if your entity is not fieldable, what are you doing here? ;)
  $bundle_key = $entity_info['entity keys']['bundle'];
  $values = array(
    // The bundle of the new entity is set in the options for this
    // style plugin. This has to be set by the user, because there is
    // absolutely no other way to sniff this out!
    // TODO: cloud cuckoo land, but a form element field to specify
    // the bundle for each row would be nice!
    $bundle_key => $this->plugin->options['relationship_creation_bundle'][$relationship_id],
  );

  // Just a little bit of sugar to save this having to be done in a custom
  // form submit handler: for nodes and comments, set the uid property
  // to the current user. We would do this with anything that has a uid
  // property, but entity_get_property_info() calls it 'author' and it's just
  // starting to get faffy now.
  if ($entity_type == 'node' || $entity_type == 'comment') {
    $values['uid'] = $GLOBALS['user']->uid;
  }
  $entity = entity_create($entity_type, $values);

  // Add our own property to the entity, where we keep track of the properties
  // that are exposed as form elements in the view. This is how we will
  // determine whether or not to save it when the form is submitted.
  $entity->editableviews_exposed_fields = array();

  // Add our own property to specify whether this needs to be saved or not.
  // @see editableviews_entity_form_submit_build_values()
  $entity->editableviews_needs_save = FALSE;
  return $entity;
}