0

There is a field collection field for a content type, it is called field_item. I gave a direct link to the users to add content to this field based on the it's parent id. this is the url, /field-collection/field-item/add/node/$nid.

After saving the field collection field, the page is redirected to the field collection view page not the parent node and I need to accomplish this.

I tried a few solutions so far but non of them worked.

I tried to do this using rules module, the solution in Redirect from a Field Collection to its Parent Node or Entity with Rules but it didn't work.

I tried this code to test whether altering the form would change the redirect page

function hook_form_alter(&$form, &$form_state, $form_id){
    if($form['#entity_type'] == "field_collection_item" && $form['#bundle'] == "field_item"){
        $form_state['redirect'] = "/user";
        kpr($form_state);
    }
}

This code prints the form state

enter image description here

But after saving the form it still goes to the field collection page.

From what I know field collection is an entity so I tried this to test the redirection

function hook_entity_insert($entity, $type) {
   $info = entity_get_info($type);
   list($id) = entity_extract_ids($type, $entity);
   db_insert('example_entity')
       ->fields(array(
        'type' => $type,
        'id' => $id,
        'created' => REQUEST_TIME,
        'updated' => REQUEST_TIME,
        ))
         ->execute();
   drupal_goto("/user");

}

But it didn't work again. The admin theme is seven but the users theme is something else. I inserted these functions in both seven theme's and my custom theme's template.php. I also cleared the cache.

How do I can redirect the user to the parent node of the field collection after saving/updating the filed collection?


UPDATE

The only solution I have found so far is this

function hook_form_alter(&$form, &$form_state, $form_id){
    if($form['#entity_type'] == "field_collection_item" && $form['#bundle'] == "field_item"){
       $form['#submit'][] = "field_collection_item_goto_item";
    }
}

function field_collection_item_goto_item(){
    $url = current_path();
    $array = explode("/", $url);
    drupal_goto("/node/".$array[sizeof($array)-1]);
}

Since the node ID of the parent comes at the end of the url, because the url is /field-collection/field-item/add/node/$nidso this code works.

But it is preferred to get the host entity nid of the field collection item in the hook_form_alter and send this nid to the submit function as a parameter. I did my best but I couldn't find host entity nid so I had to use the url.

  • I had this same issue. Updating both field collection and redirect modules fixed it. – user3242790 Feb 7 '16 at 14:48
  • As far as I recall, you don't put a slash in front of the path. Drupal core uses calls like the following: drupal_goto('admin/config/services/aggregator'), drupal_goto("node/$node->nid"). – kiamlaluno Feb 7 '16 at 17:38
  • Can you expand on this? What version were you running before/after? Is there a particular issue that this is tied to? Etc. – mpdonadio Feb 8 '16 at 16:21
  • Redirect was 7.x-1.0-rc1 Field Collection was .x-1.0-beta8 They are now at Redirect 7.x-2.x-dev Field Collection 7.x-1.0-beta11 I have expanded the Basic Page content type to include a field collection of typical fields but if a user clicked the edit link to edit a single instance of the field collection it was creating a redirect. Updating fixed that issue. – user3242790 Feb 9 '16 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.