1

Hi people am a newbie in Drupal hooks, until this time i never did need write a hook. But now i need write a widget fieldwith various elements to search and store multiples terms of a taxonomy.

I did a lot of research and tried to understand how the creation runs a hook form fields widget but could not reach a solution. As the attached figure, I need to create a form to search in terms of vocabulary and be able to select terms to other research and keep adding terms without submitting the node, just submitting the fields of the widget via ajax. Does anyone know a solution or help me? I am completely layman in development for Drupal hooks, yet had never needed to use.

Sorry for my English. enter image description here

Iam did write this code at the moment:

drupal_load('module', 'taxonomy');

/**
 * Implement hook_field_widget_info().
 */
function multitermo_field_widget_info() {
  return array(
    'multitermo' => array(
      'label' => t('Multitermo Nois na Fita'),
      'field types' => array('taxonomy_term_reference'),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_CUSTOM,
        'default value' => FIELD_BEHAVIOR_CUSTOM,
      ),
    ),
  );
}

/**
* Fuction callback dropdown_second
**/
function multitermo_dependent_dropdown_callback($form, $form_state) {
    return $element['dropdown_second']; //How can i return the element?
}

/**
* Fuction callback 
**/
function multitermo_update_checkboxes_unidades_callback($form, $form_state) {
    return $element['checkboxes_unidades']; //How can i return the element?
}

/**
 * Helper function to populate the first dropdown.
 *   Dropdown options.
 */
function _multitermo_get_first_dropdown_options($vid) {

  $termos =  taxonomy_get_tree($vid, $parent = 0, $max_depth = 1, $load_entities = TRUE);
  $arrayTermos = array();
  $arrayTermos +=  ['' => t('--Select--')];
  foreach($termos as $termo){
      $arrayTermos +=  [$termo->tid => $termo->name];
  }
    return $arrayTermos;
}

/**
 * Helper function to populate the second dropdown.
 *   Dropdown options
 */
function _multitermo_get_second_dropdown_options($selected = '', $vid) {

  $termos =  taxonomy_get_tree($vid, $parent = $selected, $max_depth = 1, $load_entities = TRUE);
  $arrayTermos = array();
  foreach($termos as $termo){
      $arrayTermos +=  [$termo->tid => $termo->name];
  }
  return $arrayTermos;
}

function _multitermo_get_units_options(){

    $arrayTermos = array();

    return $arrayTermos;
}

function multitermo_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $vocabulary = $field['settings']['allowed_values'][0]['vocabulary'];
  $vid = taxonomy_vocabulary_machine_name_load($vocabulary)->vid;
  $options_first = _multitermo_get_first_dropdown_options($vid);

  $element['dropdown_first'] = array(
    '#type' => 'select',
    '#title' => 'Select Institution',
    '#options' => $options_first,
    '#ajax' => array(
      //'event' => 'change',
      'callback' => 'multitermo_dependent_dropdown_callback',
      'wrapper' => 'dropdown-second-replace',
    ),
  );
  $element['dropdown_second'] = array(
    '#type' => 'select',
    //'#title' => $options_first[$selected] . ' ' . t('Selecione um Grupo de Unidade'),
    '#title' => t('Select Unit Group'),
    '#prefix' => '<div id="dropdown-second-replace">',
    '#suffix' => '</div>',
    '#options' => _multitermo_get_second_dropdown_options($selected, $vid),
    '#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
  );

  $element['multitermo_unit'] = array(
    '#type' => 'textfield',
    '#title' => 'Unit',
  );

  $element['multitermo_city'] = array(
    '#type' => 'textfield',
    '#title' => 'City',
  );

  $element['search_units'] = array(
    '#value' => t("Search Unit"),
    '#type' => 'submit',
    //'#type' => 'button',
    '#ajax' => array(
      'callback' => 'multitermo_update_checkboxes_unidades_callback',
      'wrapper' => 'multitermo-update-checkboxes-unidades-callback-value',
      'method' => 'replace',
    ),
  );

  $element['checkboxes_units'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Unidades'),
    '#prefix' => '<div id="multitermo-update-checkboxes-unidades-callback-value">',
    '#suffix' => '</div>',
    '#options' => _multitermo_get_units_options($selected, $vid),
  );

  //print var_export($element['multitermo_fieldset'],true);exit();
  //$form_state['dropdown_second'] = $element ;
  return $element;  

}

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.