0

I have an interesting setup That uses a field collection as a host for line items. These line items contain two entity references to different nodes.

One entity reference is for a node that is considered a parent or a trunk item. When a user is ready as part of an external workflow, they view this field collection line item and create a 'child item' which is intended to populate the second entity reference. And the 'child item' is also linked to its parent.

Generally this has been working well, and there is a custom module that handles the child item generation (which is essentially a clone of the parent item).

The problem is, suddenly a situation has developed where in certain cases When the user initiates the copy from the field collection level, it can attach the 'child item' to more than one item. (if there is three in a list, it may attach to the first and last item in the field collection, if there is four, first and last, if it's 8, it could be first and 5th, for example.

This is the code that decides if it should make a copy. (below) the strange thing is I pass into the function ONLY that field collection item, the story id, and the node that contains the field collection. No other line items are loaded, so it shouldn't even be able to do what it is doing.

/**
 * Implements hook_menu().
 */
function click_printflow_menu() {

  $items['print-create/%/%/%'] = array(
    'title' => 'Generating Story from Wire',
    'page callback' => '_click_print_create',
    'page arguments' => array(1, 2, 3),
    'access arguments' => array('stories curate'),
    'type' => MENU_LOCAL_TASK,
  );
  $items['generate-shell/%'] = array(
    'title' => 'Generating Story from Wire',
    'page callback' => '_generate_empty_node',
    'page arguments' => array(1),
    'access arguments' => array('stories curate'),
    'type' => MENU_LOCAL_TASK,
  );

  return $items;
}

/**
 * Function to check for and hand off creation of print child.
 *
 * @param int $rnid
 *   Node ID of run list.
 * @param int $snid
 *   Node Id of story we want print child from.
 * @param int $fcitem
 *   Field collection item ID that is host to this story.
 */
function _click_print_create($rnid, $snid, $fcitem) {
  $host_line_item = entity_metadata_wrapper('field_collection_item', $fcitem);

  $story = $host_line_item->field_story->value();
  $child = $host_line_item->field_print_child->value();

  // First, let's check for held flag. If set, Prevent movement.
  if (isset($story->field_hold_for_approval[LANGUAGE_NONE])) {
    $held = $story->field_hold_for_approval[LANGUAGE_NONE][0]['value'];

    if ($held == "held") {
      drupal_set_message(
        'This story has been <strong>held</strong> for approval. It will need to be approved to continue.',
        'error'
      );
      drupal_goto('node/' . $story->nid);
    }
  }

  if (isset($story)) {
    if (isset($child)) {
      drupal_set_message('A Print child already exists. It has been loaded below.', 'status');
      drupal_goto('node/' . $child->nid);
      return FALSE;
    }
    else {
      $new_print_child = _generate_print_entity($rnid, $snid, $fcitem);

      // Go to print child edit screen.
      drupal_goto('node/' . $new_print_child . '/edit');
    }
  }
  else {
    // drupal_set_message('Invalidly set run list item detected, skipping', 'warning');
  }
}

For fun, here's the function that actually writes the child node: (_generate_print_entity($rnid, $snid, $fcitem))

/**
 * Function to generate new print entity and return nid.
 *
 * @param int $rnid
 *   Node ID of run list.
 * @param int $snid
 *   Node ID to clone from.
 * @param int $fcitem
 *   Field collection item ID that is host to this story.
 *
 * @return int
 *   Return nid of new child node.
 *
 * @throws Exception
 */
function _generate_print_entity($rnid, $snid, $fcitem) {

  $node = node_load($snid);

  global $user;

  $values = array(
    'type' => 'print_child_story',
    'uid' => $user->uid,
    // Do not publish story.
    'status' => 0,
  );

  $print_version = entity_create('node', $values);

  // Set entity wrapper for new node.
  $entity_data = entity_metadata_wrapper('node', $print_version);

  $entity_data->language(LANGUAGE_NONE);

  // Set up data for new node inside wrapper.
  if (isset($node->title)) {
    $entity_data->title->set($node->title);
  }

  if (!empty($node->body)) {
    $body = array(
      'value' => $node->body[LANGUAGE_NONE][0]['value'],
      'format' => 'filtered_html',
    );
  }

  if (isset($body)) {
    $entity_data->body->set($body);
  }

  if (isset($node->field_related_photos[LANGUAGE_NONE])) {
    foreach($node->field_related_photos[LANGUAGE_NONE] as $files) {
      $fid = $files['fid'];
      $files_array[] = array('fid' => $fid, 'display' => 1);
    }
    $entity_data->field_related_photos->set($files_array);
  }

  if (isset($node->field_name_story[LANGUAGE_NONE])) {
    $entity_data->field_name_story->set($node->field_name_story[LANGUAGE_NONE][0]['value']);
  }

  if (isset($node->field_byline_final[LANGUAGE_NONE])) {
    $entity_data->field_byline_final->set($node->field_byline_final[LANGUAGE_NONE][0]['value']);
  }

  if (isset($node->field_owner[LANGUAGE_NONE])) {
    $entity_data->field_owner->set(intval($node->field_owner[LANGUAGE_NONE][0]['target_id']));
  }

  if (isset($node->field_publication_of_origin[LANGUAGE_NONE])) {
    $entity_data->field_publication_of_origin->set(intval($node->field_publication_of_origin[LANGUAGE_NONE][0]['tid']));
  }

  if (isset($node->field_category[LANGUAGE_NONE])) {
    $entity_data->field_category[0]->set(intval($node->field_category[LANGUAGE_NONE][0]['target_id']));
  }

  if (isset($node->field_factbox_headline[LANGUAGE_NONE])) {
    $entity_data->field_factbox_headline->set($node->field_factbox_headline[LANGUAGE_NONE][0]['value']);
  }

  if (isset($node->field_factbox_text_area[LANGUAGE_NONE])) {
    $entity_data->field_factbox_text_area->set($node->field_factbox_text_area[LANGUAGE_NONE][0]['value']);
  }

  if (isset($node->field_jump_headline[LANGUAGE_NONE])) {
    $entity_data->field_jump_headline->set($node->field_jump_headline[LANGUAGE_NONE][0]['value']);
  }

  if (isset($node->field_field_pull_quote[LANGUAGE_NONE])) {
    $entity_data->field_pull_quote->set($node->field_field_pull_quote[LANGUAGE_NONE][0]['value']);
  }

  if (isset($node->field_field_pull_quote[LANGUAGE_NONE])) {
    $entity_data->field_pull_quote->set($node->field_field_pull_quote[LANGUAGE_NONE][0]['value']);
  }

  if (isset($node->field_subheadline[LANGUAGE_NONE])) {
    $entity_data->field_subheadline->set($node->field_subheadline[LANGUAGE_NONE][0]['value']);
  }

  if (isset($node->field_notes[LANGUAGE_NONE])) {
    $entity_data->field_notes->set($node->field_notes[LANGUAGE_NONE][0]['value']);
  }

  if (isset($node->field_slug[LANGUAGE_NONE])) {
    $entity_data->field_slug_text->set($node->field_slug[LANGUAGE_NONE][0]['value_token_filtered']);
  }

  // Set parent story link.
  if (isset($node->nid)) {
    $entity_data->field_parent_story->set($node->nid);
  }

  // Set Run list link.
  if (isset($rnid)) {
    $entity_data->field_run_list->set($rnid);
  }

  // Entity API cannot set dates. Old Method:
  $print_version->field_story_date[LANGUAGE_NONE][0] = array(
    'value' => date_format(date_create($node->field_story_date[LANGUAGE_NONE][0]['value']), 'Y-m-d 00:00:00'),
    'timezone' => 'America/Moncton',
    'timezone_db' => 'America/Moncton',
  );

  if (isset($node->field_embargo[LANGUAGE_NONE])) {
    $print_version->field_embargo[LANGUAGE_NONE][0] = array(
      'value' => date_format(date_create($node->field_embargo[LANGUAGE_NONE][0]['value']), 'Y-m-d 00:00:00'),
      'timezone' => 'America/Moncton',
      'timezone_db' => 'America/Moncton',
    );
  }

  $entity_data->revision->set(TRUE);
  $entity_data->log->set('Print Story Created.');

  $entity_data->save();

  $the_node = $entity_data->getIdentifier();

  // Load and Add to run list item
  $rli_row = entity_metadata_wrapper('field_collection_item', $fcitem);
  $rli_row->field_print_child->set(intval($the_node));
  $rli_row->save(TRUE);

  return $the_node;
}

At the end of this function, it loads the field collection item, and sets the entity referennce for the child item and saves that field collection row only (not the entire run list node). could it have something to do with this?

1 Answer 1

0

Turns out there was a module that was interfering called Corresponding Entity References (CER) that was not needed anymore. Disabling it solved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.