0

I have Two Content types Store and Deals and one Vocabulary named as Outlets. Outlet has many terms. I have added outlets as a term reference field to stores content type, and title field of store content type is added as a node reference field to deals.

1.Store content type When I am adding a new store I am selecting some outlet terms for it and saving it.

2.Deals Content type In Deals content type when I am creating a new deal I am selecting any store in dropdown, which I have added here as a node reference of store content type. What my concern is now while selecting any store title I want its corresponding outlets to be displayed here, so that if I wish I can uncheck some of them and then save.

Any help would be much appreciated.Please find the screenshot for better understanding. This is the store content type adding a store cartersHere in deals content type I am selecting the store which I have previously added in stores, now while selecting carters the corresponding outlets should get selected

Top Image is the store content type adding a store carters Second image is the deals content type I am selecting the store which I have previously added in stores, now while selecting carters the corresponding outlets should get selected

5
  • Hello. Each and every sentence is OK, but at the same time I go totally lost about what you want. Could you possibly edit your question to make it simpler and more structured? We came to English language from all sides, all backgrounds, and sometimes it gets a bit hard. Also, try to shrink your browser to no more than 600px before taking a screenshot. Pictures here are limited to 630px width (less in quote), so anything more gets downsized and looses readability.
    – Mołot
    Commented Dec 3, 2013 at 8:06
  • Thanks for your comment. Try to edit your question, you can do it just as well (better, actually) as you can edit comment. And if I'll have an idea, I'll try to post it. And please do consider retaking screenshot in smaller size, to make it as easy as possible to answer. Maybe there is someone online who already knows it, but he does not realize that what he knows is what you need ;)
    – Mołot
    Commented Dec 3, 2013 at 8:18
  • Thanks, Molot I will try to explain my question more clearly. I am very Glad to see your reputation here.
    – Mudasir
    Commented Dec 3, 2013 at 9:07
  • Do you want selected store's outlets to be checked or other stores outlets to be hidden ? Do you have a custom module already installed ? Commented Dec 3, 2013 at 10:28
  • Dear Gregory Kapustin I want the stores outlets to be selected so that if I want to deselect any outlet, i should be able to do it by unchecking that outlet.I do not have installed the custom module yet.
    – Mudasir
    Commented Dec 3, 2013 at 10:47

1 Answer 1

1

I'm not sure you can do it with Conditional Fields, so I did it with a custom code. You have to :

  1. Hook on your form
  2. Get referenced node
  3. Get its taxonomy value(s)
  4. Check this values in your term reference field.

    function MYMODULE_form_alter(&$form, &$form_state, $form_id) {    
        if($form_id == "page_node_form") {
            $form['field_OUTLETS']['#prefix'] = '<div id="mytaxofield">';
            $form['field_OUTLETS']['#suffix'] = '</div>';
            $form['field_STOREREFERENCE']['und']['#ajax'] = array(
                'callback' => 'MYCALLBACK',
                'wrapper' => 'mytaxofield',
                'method' => 'replace',
                'effect' => 'fade',
            );
        }
    }
    
    function MYCALLBACK($form, $form_state) {
        if(!empty($form_state['input']['field_STOREREFERENCE'])) {
            $nr = node_load($form_state['input']['field_STOREREFERENCE']['und']);
            foreach($nr->field_OUTLETS['und'] as $delta => $value) {
                // Add term to default values
                $form['field_OUTLETS']['und']['#default_value'][] = $value['tid'];
                // As it's not enough (I might need help on that one), I force it with the '#checked' value
                $form['field_OUTLETS']['und'][$value['tid']]['#checked'] = TRUE;
            }
            $form['field_OUTLETS']['und']['#default_value'] = drupal_map_assoc($form['field_OUTLETS']['und']['#default_value']);
        }
        return $form['field_OUTLETS'];
    }
    
7
  • Thanks a lot, please let me know do I need to create a custom module for this.
    – Mudasir
    Commented Dec 3, 2013 at 11:44
  • Yes you do ; create a custom module and just put this code in your .module file. To know how to replace field_OUTLETS, field_STOREFEREFERENCE and so on, ask another question or add the line dpm($form); at the beginning of your hook_form_alter function. Commented Dec 3, 2013 at 12:19
  • I added a custom module named custom_module. In custom_module.module file I pasted the above code.
    – Mudasir
    Commented Dec 3, 2013 at 12:28
  • I replaced the following lines------ 1.page_node_form,( with deal-node-form)------- 2.field_OUTLETS ( As my outlets fields machine name is "field_outlets_deal"),----- 3.'field_store'(This is the reference field of store),----- Now what I have to do.
    – Mudasir
    Commented Dec 3, 2013 at 12:51
  • Just enable your custom module, it should work fine. Commented Dec 4, 2013 at 16:03

Your Answer

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

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