I had the issue to prepopulate an selectbox of taxonomyterms but I had only the complete taxonomyterm available (via webform). It is possible to prepopulate taxonomyterms, but you need the termid.

The patch attached enhances prepopulate to find the taxonomyid based on the term, if [term] is added to the parameter e.g.

node/add/CONTENTTYPE?edit[FIELDNAME][und][term]=MY_TAXONOMY_TERM

This is only one possible way to implement this, patch is attached

CommentFileSizeAuthor
enhance_taxonomy_term.patch773 bytessmussbach
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

A.Kotov’s picture

Issue summary: View changes

Thanks a lot.

I could not prepopulate taxonomy term in dropdown menu following the manual. And this code solved problem.
Had a correct a bit, because variable names already changed.

Had this in template


</div><div class="field-type-taxonomy-term-reference field-name-field-form-of-contract field-widget-options-select form-wrapper" id="edit-field-form-of-contract"><div class="form-item form-type-select form-item-field-form-of-contract-und">
  <label for="edit-field-form-of-contract-und">Form of Contract </label>
 <select id="edit-field-form-of-contract-und" name="field_form_of_contract[und]" class="form-select"><option value="_none">- None -</option><option value="275" selected="selected">Sale</option><option value="276">Rent</option><option value="1118">Swap</option><option value="277">Lease</option><option value="1117">XRP trade</option><option value="1116">Activation</option><option value="278">Other</option></select>

After this (in prepopulate.module)

function _prepopulate_request_walk(&$form, &$request_slice) {
  $limited_types = array('value', 'hidden', 'button', 'image_button');
  if (is_array($request_slice)) {
    foreach (array_keys($request_slice) as $request_variable) {
      if (element_child($request_variable) && !empty($form[$request_variable]) &&
       (!isset($form[$request_variable]['#type']) || !in_array($form[$request_variable]['#type'], $limited_types))) {
        if (!isset($form[$request_variable]['#access']) || $form[$request_variable]['#access'] != FALSE) {
          _prepopulate_request_walk($form[$request_variable], $request_slice[$request_variable]);
        }
      }

Added this


//fetch taxonomytermid if asked to
     elseif($request_variable == "term"){
        $termName = $request_slice[$request_variable];
        $term = taxonomy_get_term_by_name($termName);
        if(1 == count($term)){
          $tid = key($term);
          $form['#value'] = $tid;
       }
      }

node/add/CONTENTTYPE?edit[FIELDNAME][und][term]=MY_TAXONOMY_TERM_NAME (not tid)

may be somebody could compile patch from this