P
P
Pavel2015-01-19 12:28:51
Drupal
Pavel, 2015-01-19 12:28:51

Content rendering algorithm using ajax and cookies in drupal 7?

Hello. The situation is this, it is necessary to organize the output of content, depending on the value of the cookie. Those. there are two options for displaying the node, (line and block), the output changes depending on the value of the cookie, i.e. I stupidly use a check for cookies in node.tpl.php (though in hook_node_view - that's not the point). All this business certainly works, ie. by clicking on the buttons I change the value of the cookie, then the page reloads and we get the output format we need.
Now what's the difficulty? The whole thing refuses to work correctly when using ajax. Those. according to PHP rules - the assigned cookies are available on the next page load. Now my algorithm is

  1. Button event (click)
  2. I change the cookie value
  3. Next is the function that renders the node (and there, accordingly, there is a check for the cookie value)

but with this algorithm, the cookie value that was initially taken when the page was loaded , and not the one that was changed by clicking on the button. The question is how to pick up the changed cookie value in ajax?
module code example
<?php

/**
 * Implements hook_menu()
*/
function module_menu() {
  $items['domptabview'] = array(
    'page callback' => 'ajax_link_response',
    'access callback' => 'user_access',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}


/**
 * Отдаем полученный результат браузеру
 */
function ajax_link_response($type = 'ajax', $nid = 0) {
  $output = _module_load_noder($nid);
  if($type == 'ajax'){
    $commands = array();
    
    $commands[] = ajax_command_replace('#block-system-main .view-content', '<div class="view-content">' .$output .'</div>');
    $page = array(
      '#type' => 'ajax',
      '#commands' => $commands
    );
    ajax_deliver($page);
  }else{
    $output = '<div class="view-content">' . $output . '</div>';
    return $output;
  }
}

/**
 * Получаем содержимое ноды и возвращаем HTML
 */
function _module_load_noder($nid = 0) {
  // загрузили ноду
  $node = node_load($nid, NULL, false);
  if ($node) {
    $vnode = node_view($node, 'teaser');
    return drupal_render($vnode);
  }
}

I will clarify that I change the cookie value through jquery.cookie, again on the button event. I tried to assign inside ajax (setcookie ()), the result is the same.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
man1982, 2015-01-30
@man1982

Send the cookie value to the server as well. So that it comes to ajax_link_response as a variable

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question