References will most probably be deprecated in the near future in favor of Entity Reference, which should probably be considered first on fresh D7 projects. Another, more radical, alternative would be the Relation module.

- from http://drupal.org/project/references

Comments

discipolo’s picture

some places to investigate

- "Relation Select". for the relation route
- if going with Entity Reference check out Reference Dialog
- http://drupal.org/project/nodeconnect if we stick to the references project

drupallerina’s picture

Another entity reference auto-create approach:

http://drupal.org/node/1261574
http://drupal.org/sandbox/michaek/1451014

zirafa’s picture

Here is the auto-create pattern currently used in Pushtape Features with nodereference. It seems like we can just update this code and use entity reference to store the relationship when creating the album node.

/**
 *  Custom submit handler for track node form.
 *  Forces album->track relationship 1-1.
 */
function pushtape_core_track_node_form_submit($form, &$form_state) {

  // If we are creating a new album...
  if ($form['field_album_checkbox']['#value'] == 1 && !empty($form_state['values']['new_album'])) {
    global $user;
    // Create a new album node
    $node = new stdClass();
    $node->type = 'album';
    node_object_prepare($node);
    $node->title = $form_state['values']['new_album'];
    $node->author = $user->name;
    $node->uid = $user->uid;
    $node->language = LANGUAGE_NONE;
    $node->pathauto_perform_alias = FALSE;
    $node->field_tracklist[LANGUAGE_NONE][0]['nid'] = $form_state['nid'];
    // This function operates by reference on our node object, and will add $node->nid.
    node_save($node);
    // Now associate the track with the new album and redirect to it after saving the track.
    $track = node_load($form_state['nid']);
    $track->field_album[LANGUAGE_NONE][0]['nid'] = $node->nid;
    node_save($track);
    unset($form_state['rebuild']);
    $form_state['redirect'] = 'node/' . $node->nid . '/edit';
  }
  elseif ($form_state['values']['field_album_previous_value'] != $form_state['values']['field_album'][LANGUAGE_NONE][0]['nid']) {
    $track_id = $form_state['nid'];
    // Delete from previous album, if there was one.
    if ($form_state['values']['field_album_previous_value'] > 0) {
      $old_album = node_load($form_state['values']['field_album_previous_value']);
      $filtered = array();
      foreach ($old_album->field_tracklist[LANGUAGE_NONE] as $key => $value) {
        if ($value['nid'] != $track_id) {
          $filtered[$key]['nid'] = $value['nid'];
        }
      }
      $old_album->field_tracklist[LANGUAGE_NONE] = $filtered;
      node_save($old_album);
    }
    // Add to this album
    if ($form['field_album_checkbox']['#value'] == 1) {
      $this_album = node_load($form_state['values']['field_album'][LANGUAGE_NONE][0]['nid']);
      $delta = !empty($this_album->field_tracklist) ? count($this_album->field_tracklist[LANGUAGE_NONE]) : 0;
      if ($delta > 0) {
        // Avoid duplicates
        $filtered = array();
        $exists = FALSE;
        foreach ($this_album->field_tracklist[LANGUAGE_NONE] as $key => $value) {
          if ($value['nid'] == $track_id) {
            $exists = TRUE;
          }
        }
        if (!$exists) {
          $this_album->field_tracklist[LANGUAGE_NONE][$delta]['nid'] = $track_id;
        }
      }
      else {
        $this_album->field_tracklist[LANGUAGE_NONE][$delta]['nid'] = $track_id;
      }
      node_save($this_album);
    }
  }
}
discipolo’s picture

Status: Active » Needs review
StatusFileSize
new33.58 KB

here is a patch that attempts this switch to entityreference.
the patch also adds a mediafield (media2.x)
the artist field is rewritten to a term.

this is taken from my github upstream repository branch https://github.com/discipolo/pushtape_features/tree/7.x-2.x

drupallerina’s picture

zirafa’s picture

Currently Pushtape associates a track with an existing album reference, or prompts for the new album title that will create an album node upon track node submission. Then it lands the user on the album edit screen for further album info (if needed). This is a 1-2 step process.

Entity Connect caches the form, takes user to new form, and returns them back to the old form. This seems like an unnecessary back and forth of workflow.

discipolo’s picture

http://drupal.org/project/references_dialog now works with entity references

discipolo’s picture

just the switch

Status: Needs review » Needs work

The last submitted patch, pushtape_features_entityreference-1460296-8.patch, failed testing.

discipolo’s picture

Status: Needs work » Needs review
StatusFileSize
new10.01 KB

tried again, this time tested that it applies.

zirafa’s picture

Status: Needs review » Closed (fixed)