-1

What I want to do?

I want to display all categories in first drop down and in second all titles of related nodes with the selected category.

To get that I have mentioned all the tasks I have done. Please read below.

1st: Created taxonomy named category with terms listed Laptop, Car, Mobile etc.

2nd: Content type named product with fields title, description, category etc. The category field uses term reference of category taxonomy. 3rd: Created a form with WebForm with fields named category and productas drop down 4th: First, categories drop down field, displayed all categories (Laptop, Car, Mobile) by using WebForm Term Options. 5th: Second, product drop down field is empty yet.

I have used hook_form_FORM_ID_alter() hook to alter the webForm I created. I have found the solution to get all the nodes pragmatically those are related with specific taxonomy term ID.

6th: I have fetched term related all node by this code:

$arg = arg();
$query = db_select('node','n');
$query->join('taxonomy_index','ti','n.nid=ti.nid');
$query->fields('n', array('nid', 'title', 'type'))
  ->condition('n.type', 'products')
  ->condition('ti.tid', $arg['2']);
$result = $query->execute()->fetchAll();
$dropdown_array = array('' => t('-Select-'));
foreach ($result as $nid => $node) {
    $dropdown_array[$node->title] = $node->title;
}
echo '<pre>';   print_r($dropdown_array);   echo '<pre>';

My first question is how to display all these nodes' titles in existing drop down field?

Another question is on category change, products must be change according to its nodes like ajax?

1

My first question is how to display all these nodes' titles in existing drop down field?

In a custom module you can define your own select option element with the following code:

function custom_tweaks_webform_select_options_info() {
  $items = array();

  if (function_exists('_get_products_by_termid')) {
    $items['my-products'] = array(
      'title' => t('My products'),
      'options callback' => '_get_products_by_termid',
    );
  }
  return $items;
}


function _get_products_by_termid($termids) {
  // Debug
  $termids = array(1,2,3);

  $nids = taxonomy_select_nodes($termids);
  $products = [];
  if (!empty($nids)) {
    foreach ($nids as $nid => $value) {
      $products[$value] = node_load($value)->title;
    }
  }
  return $products;
}
  • Enable this module
  • Add a select option element to your webform
  • Edit this element and select My products under the Load a pre-built option list field (see screenshots)
  • Make sure you also have a select option element to your webform with the categories taxonomy vocabulary given (see screenshots)

enter image description here enter image description here enter image description here

To answer your second question

Another question is on category change, products must be change according to its nodes like ajax?

replace the custom code from above with the code below. Still needs default value set and checking for empty values but it should point you in the right direction. The thing is that you ajaxify the category field (bij setting the #ajax property on it) and provide your own products field and fill that with values based on the term id from the categories field.

/**
 * Implements hook_form_alter().
 */
function custom_tweaks_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'webform_client_form_41') {
    $form['submitted']['categories']['#ajax'] = array(
        'callback' => '_show_products_based_on_category_term',
        'wrapper' => 'products-div',
        'method' => 'replace',
        'effect' => 'fade',
    );

    $term_id = $form_state['values']['submitted']['categories'];
    $tid = substr($term_id, 4);

    $form['submitted']['products'] = array(
      '#title' => 'Products',
      '#type'     => 'radios',
      '#weight'  => '2',
      '#options'  => _get_products_by_termid(array($tid)),
      '#prefix' => '<div id="products-div">',
      '#suffix' => '</div>',
    );
  }
}


function _show_products_based_on_category_term($form, $form_state) {
  return $form['submitted']['products'];
}

function _get_products_by_termid($termids) {
  $nids = taxonomy_select_nodes($termids);
  $products = [];
  if (!empty($nids)) {
    foreach ($nids as $nid => $value) {
      $products[$value] = node_load($value)->title;
    }
  }
  return $products;
}
  • Thanks for your solution. Your solution isn't working for me. However I have updated this question Please check the point number 6. – usmanjutt84 Jan 18 '17 at 7:27
  • Ah probably you didn't get ant results because no termid was given to the function _get_products_by_termid. I updated the code and added screenshots of how your webform fields should look – undersound Jan 18 '17 at 9:44
  • Thanks for the great solution... It works as I expected. I'm learning good with you all .Thanks again... – usmanjutt84 Jan 18 '17 at 11:46
  • Glad it worked. For your other question, show products based on selected term, you have to ajaxify the form elements. See [drupal.stackexchange.com/a/54362/8606 ] and [drupal.org/docs/7/api/javascript-api/ajax-forms-in-drupal-7] for examples. Short on time now but can help later on. – undersound Jan 18 '17 at 12:49
  • look at my edit. I added the second answer drupal.stackexchange.com/posts/225855/revisions – undersound Jan 25 '17 at 13:31
0

1) Have you looked at the Hierarchical Select module? https://www.drupal.org/project/hierarchical_select

  • Yeah, I have already looked. But most importantly have to use webForm module it provides user to send email his message. So, I have no reason to use Hierarchical Select module. – usmanjutt84 Jan 17 '17 at 19:19

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.