5

I have a page with a dropdown. When you click on that dropdown that content is loaded by Ajax. I would like to do the same for the Breadcrumb, so I created an Ajax-URL in my module:

function selectexamfeature_menu() {
  $items = array();
  $items['selectexamfeature/ajax-replace-breadcrumb/%'] = array(
      'title' => 'Ajax-Callback for replacing breadcrumb',
      'page callback' => 'selectexamfeature_ajax_callback',
      'page arguments' => array(2),
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK,
  );
  return $items;
}

function selectexamfeature_ajax_callback($url){
  $commands = array();
    // later on the $url will be used here for generating the breadcrumb html
  $ret = $url;
  $commands[] = ajax_command_html('.page-breadcrumb', $ret);
  $page = array('#type' => 'ajax', '#commands' => $commands);
  ajax_deliver($page);
}

and in my Javascript code I have:

  var custom_settings = {};
  custom_settings.url = '/selectexamfeature/ajax-replace-breadcrumb/SOMELINKASSTRING';
  custom_settings.keypress = false;
  custom_settings.prevent = false;
  Drupal.ajax['custom_ajax_action'] = new Drupal.ajax(null, $(document.body), custom_settings);

  Drupal.behaviors.selectexamfeature_ajax_replace_breadcrumb = {
    attach: function (context, settings) {
      $( "#edit-field-text-grade-tid" ).change(function() {
        // the event...
      });
    }
  };

So at the end I want to get the SOMELINKASSTRING in my $ret variable. But for now I just get a 404 error all the time:

An AJAX HTTP error occurred. HTTP response code: 404 Debugg information follows: /selectexamfeature/ajax-replace-breadcrumb/SOMELINKASSTRING Statustext: Not Found...

I believe the issue is in the wildcard section. Somehow it does not replace it correctly.

If I leave the % away and also the SOMELINKASSTRING it works without issues. But I for sure need the parameter to know which page is requested.

Also watch this awesome article but it did not help either.

  • Is the dropdown part of a standard Drupal Form API form or is it custom? – Neograph734 Dec 15 '15 at 13:46
  • Not it's something custom. – kwoxer Dec 15 '15 at 14:00
2

If the form is part of a standard Drupal form, you can use Drupal's built in AJAX system. There is a detailed explanation here: Ajax Forms in Drupal 7. Basically it should look like this.

function hook_some_form($form, &$form_state) {
  // Create a frop down
  $form['some_select'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $default,
    '#ajax' => array(
      'callback' => 'some_ajax_callback', // This is the php function to be called
      'wrapper' => 'some_wrapper', // The javascript #id to put content in.
      'method' => 'replace', // Content placement option
    ),
  );

  // Result contents container
  $form['some_wrapper'] = array(
    '#type' => 'item',
    '#id' => 'some_wrapper',
    '#tree' => TRUE,
  );
}

And for the PHP callback

function some_ajax_callback($form, $form_state) {
  // Fetch the value AJAX gave us.
  $value = $form_state['values']['some_select'];

  // Recreate the wrapper as we overwrite the old one.
  $form['some_wrapper'] = array(
    '#type' => 'item',
    '#id' => 'some_wrapper',
    '#tree' => TRUE,
  );

  // Append the value of the drop down to it.
  $form['some_wrapper']['value'] = array(
    '#type' => 'item',
    '#markup' => check_plain($value);
  );
  // Return everything to Javascript.
  return $form['some_wrapper']
}

This allows you to pass form values to PHP for processing.

  • No it's not a form, it's just a dropdown and does not use form features. – kwoxer Dec 15 '15 at 13:59
  • Then this isn't of much value, sorry. – Neograph734 Dec 15 '15 at 14:00
  • No problem, maybe helpful for someone else. :) – kwoxer Dec 15 '15 at 14:00
0

I solved it with drupal hooks. So I use an already existing Ajax trigger.

function selectexamfeature_theme($existing, $type, $theme, $path) {
  return array(
    'breadcrumb' => array(
      'template' => 'breadcrumb',
    ),
  );
}

and

function selectexamfeature_views_ajax_data_alter(&$commands, $view) {
 ...
 // building the url variable
 ...
 $vars = array();
 themename_generate_breadcrumb($vars);
 $markup = theme('breadcrumb', array('breadcrumb' => drupal_get_breadcrumb()));
 $commands[] = ajax_command_replace('div.page-breadcrumb', $markup);
}

This works as intended.

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.