0

I don't know why, but using this code, when I click on the marker-category-add-submit button, it executes the type_callback callback, and not marker_category_add_callback. Why?

If I comment all marker-type fields in my form, it uses the right callback.

function food_truck_form_admin() {
  $content_types = get_content_types();
  $markers_categories = variable_get('food_truck_form_marker_categories', array());
  $vocabulary = get_vocabulary();
  $marker_types = get_marker_types($vocabulary->machine_name);

  // MARKER CATEGORY
  $form['marker-category']['#markup'] = '<h2>Gestion des catégories de marqueurs</h2>';
  $form['marker-category']['#prefix'] = '<div id="category-wrapper">';

  $form['marker-category-add'] = array(
      '#title' => t('Nom'),
      '#type' => 'select',
      '#prefix' => '<div class="row"><div class="column">',
      '#options' => $content_types,
      '#description' => t('Ajouter une catégorie de marqueur')
  );
  $form['marker-category-add-submit'] = array(
      '#type' => 'button',
      '#suffix' => '</div>',
      '#value' => t('Ajouter'),
      '#ajax' => array(
        'callback' => 'marker_category_add_callback',
        'wrapper' => 'category-wrapper',
        'event' => 'click'
      )
  );
  $form['marker-category-remove'] = array(
      '#title' => t('Supprimer des catégories de marqueurs'),
      '#type' => 'select',
      '#prefix' => '<div class="column">',
      '#multiple' => TRUE,
      '#validated' => TRUE,
      '#options' => $markers_categories
  );
  $form['marker-category-remove-submit'] = array(
      '#type' => 'button',
      '#value' => t('Supprimer'),
      '#suffix' => '</div></div></div>',
      '#ajax' => array(
          'callback' => 'marker_category_remove_callback',
          'wrapper' => 'category-wrapper',
          'event' => 'click'
      )
  );

  // TEST ZONE

  $form['marker-type'] = array(
      '#title' => t('test'),
      '#type' => 'textfield',
      '#prefix' => '<div class="type-wrapper">'
  );
  $form['marker-type-submit'] = array(
      '#type' => 'button',
      '#value' => t('Ajouter'),
      '#ajax' => array(
          'callback' => 'type_callback',
          'wrapper' => 'type-wrapper',
          'event' => 'click'
      )
  );
  $form['marker-type-list'] = array(
      '#title' => t('test'),
      '#type' => 'select',
      '#suffix' => '</div>',
      '#multiple' => TRUE,
      '#validated' => TRUE,
      '#options' => array()
  );

  // $form = array_merge($form, generate_marker_types_management_form($markers_categories));

  $form['marker-wrapper-end']['#suffix'] = '</div>';

  // ATTACHEMENTS
  $form['#attached']['css'] = array(
      drupal_get_path('module', 'food_truck_form') . '/css/global.css',
      drupal_get_path('module', 'food_truck_form') . '/css/config_form.css'
  );
  $form['#submit'][] = 'food_truck_form_admin_submit';
  return system_settings_form($form);
}

function type_callback($form, &$form_state) {
  array_push($form['marker-type-list'], $form_state['value']['marker-type']);
  return array(
      $form['marker-type'],
      $form['marker-type-submit'],
      $form['marker-type-list']
  );
}

/**
 * CALLBACK : Ajouter une catégorie de marqueur
 */
function marker_category_add_callback($form, &$form_state) {
  // On récupère la clé et le nom de la catégorie
  $key = $form_state['values']['marker-category-add'];
  $marker_name = $form['marker-category-add']['#options'][$key];

  // On récupère les catégories existantes
  $food_truck_form_marker_categories = variable_get('food_truck_form_marker_categories', array());
  // Si on ajoute une catégorie qui n'existe pas déjà
  if (!array_key_exists($key, $food_truck_form_marker_categories)) {
    // On sauvegarde les catégories
    $food_truck_form_marker_categories[$key] = $marker_name;
    variable_set('food_truck_form_marker_categories', $food_truck_form_marker_categories);
    drupal_set_message(t('La catégorie de marqueur à été ajouté'));
    $form['marker-category-remove']['#options'] = $food_truck_form_marker_categories;
  } else {
    drupal_set_message(t('Cette catégorie de marqueur à déjà été ajouté !'), 'warning');
  }
  return array(
      $form['marker-category'],
      $form['marker-category-add'],
      $form['marker-category-add-submit'],
      $form['marker-category-remove'],
      $form['marker-category-remove-submit']
  ); // return array_merge(get_return_callback($form), generate_marker_types_management_form($food_truck_form_marker_categories));
}

1 Answer 1

0

The solution was using différent '#name' in my buttons ;) coworker found the solutions :D

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.