0

Drupal 7

Taxonomy vocabulary = Company activity (Activity 1, Activity 2, Activity 3 ….). This vocabulary has an OG group field.

Content type 1 = Company with an entity (terms) reference field showing the list of the terms of the activity vocabulary. The widget is “radio button box” and the administrator of the Company, during the creation of the content, may tag and save the appropriate activities performed. This content type has an OG group field. Example: Company 1 --> tag Activity 1 and Activity 2 – Company 2 --> tag Activity 1 and Activity 3

Content type 2 = Request of Service. Any Company can create a new request of service and first of all choose the needed activity in a entity (terms) reference field (fiel_tipologia_macchina) showing the list of the terms of the activity vocabulary. The widget is “selection list” – only one choice. This content type has an OG group audience field.

Now the question: In Content type 2 (request of service) I need to add a field that show the Company qualified to perform the requested service. Example: the service requested is “Activity 1” --> show Company 1 and Company 2.

The field has to be a radio button box so the author of the request can choose the company to send the request (one or more company).

I think that I have to use a view (page or block) but I am not able to find the right relation and contextual filters (entity reference or OG).

Thank you in advance for help.

  • It sounds like you want to alter the possible options in the Company field based on the option chosen in the Request field. Is that correct? – beltouche Apr 1 at 16:00
  • Hi beltouche. I want just filter, showing them in an entity reference selectable field, the "Company" tagged nodes for a referenced term, choosen in another entity ref. field of "Request" content type. The author add a new "Request", choose the service that he needs and automatically should appear a list of Company that can give that service. The list is selectable and he choose one or more Company to send the request of service. Thank you for cooperation. Ciao. – Lino Mancini Apr 4 at 14:31
0

I have accomplished something along the lines of what you describe with a combination of a hook_form_alter and some Javascript. The following should help you set up something that will interactively enable/disable possible company choices based on the selected activity. Take note that it really only works if those inputs are selects and not Drupal's default autocompletes.

In a hook_form_alter directed at the "Request" node and node edit forms, create a simple of array of arrays, where the key is the term IDs of the activity, and the subarrays are the node IDs of the companies. Here is some pseudo-code, since I don't the actual structures of your entities:

$nested_array = [];

$term_query = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->getQuery();
$term_ids = $term_query->condition('vid', 'activity')->execute();

foreach($term_ids as $term_id) {
    $node_query = \Drupal::entityTypeManager()->getStorage('node')->getQuery();
    $node_ids = $term_query
      ->condition('type', 'company')
      ->condition('field_ref_activity', $term_id, 'IN')
      ->execute();

    $nested_array[$term_id] = $node_ids;
 }

 $form['#attached']['drupalSettings']['company_activities'] = $nested_array;
 $form['#attached']['libraries'][] = 'my_module/my_module.library_name';

The above makes the nested arrays available to the front end for on-the-fly manipulation. Create a library for your module with this jQuery-based Javascript. Again, pseudocode which you'll have to modify to your form structure:

function limitCompanySelection(selected_activity) {
    let allowed_companies = drupalSettings.company_activities[selected_activity.val()];

    $('company-input-selector option').each(function(){
        if ($.InArray($(this).val(), allowed_companies)) {
            $(this).prop('disabled', false);
        }
        else {
            $(this).prop('disabled', true);
        }
    });
}

Set the above function to run on initial load and every time the selected activity is changed, finding the selected activity and passing it into the function.

| improve this answer | |
0

Hi Beltouche and thank you.

After two months of mental confusion, finally I solved the problem with https://www.drupal.org/project/reference_option_limit.

I followed this post: https://www.drupal.org/docs/7/modules/reference-field-option-limit/examples (Example 3) but I fixed my brain to use in the Request content a new entity ref field pointed to the activity vocabulary.

This was wrong because I should use, as conditional value, the existing field in the Company content. Now the dipendent entity ref field shows only the Companies that tagged the specific activity in their own account and this is perfect.

In any case, thank you very much for cooperation. Ciao.

Closed.

| improve this answer | |

Your Answer

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

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