You are here

function term_level_element_ajax_callback in Term Level Field 7

Returns the current field with updated terms.

1 string reference to 'term_level_element_ajax_callback'
term_level_element_process in ./term_level_element.inc
Processes the term_level element type.

File

./term_level_element.inc, line 113
term_level_element form type

Code

function term_level_element_ajax_callback($form, &$form_state) {
  $commands = array();
  if (isset($form_state['values']['default_terms']) || isset($form_state['values']['added_term'])) {
    $parents = $form_state['triggering_element']['#parents'];
    $field = $form;
    foreach ($parents as $parent) {
      if ($parent === 'tag_cloud_term_options') {
        break;
      }
      $field = $field[$parent];
    }
    $table = drupal_render($field);
    $rows = explode('</tr>', $table);
    array_pop($rows);

    //Remove last entry with </tbody></table>.
    if (count($rows) > 1) {

      // mMore than the header.
      $last_row = array_pop($rows);
      if (strpos($last_row, "term-level-element-tag-cloud-row") !== FALSE) {
        $tag_cloud_row = trim($last_row) . '</tr>';
        if (count($rows) > 1) {
          $last_row = array_pop($rows);
        }
      }
      $term_added = $form_state['values']['added_term'];
      if ($term_added && strpos($last_row, "term-level-element-table-row-" . $term_added) !== FALSE) {

        // Remove any unnecessary suffix content.
        $first_tr = strpos($last_row, '<tr');
        $added_term_row = substr($last_row, $first_tr) . '</tr>';
      }
    }
    $table_selector = '#term-level-element-table-' . $form_state['triggering_element']['#vid'] . '-' . $form_state['triggering_element']['#term_level_parent'];
    $commands[] = ajax_command_css($table_selector . ' tr.term-level-element-table-row-none', array(
      'display' => 'none',
    ));
    $tag_cloud_selector = $table_selector . ' tr.term-level-element-tag-cloud-row';
    if (isset($added_term_row)) {

      // Add table header, if not available yet.
      if (count($rows) == 1 && preg_match('/<thead>.*<\\/thead>/', $table, $matches)) {
        $commands[] = ajax_command_remove($table_selector . ' thead');
        $commands[] = ajax_command_before($table_selector . ' tbody', $matches[0]);
      }

      // Remove possible existing row with this term
      $commands[] = ajax_command_remove('#term-level-element-table-row-' . $term_added);
      $commands[] = ajax_command_before($tag_cloud_selector, $added_term_row);
    }
    if (isset($tag_cloud_row)) {
      $commands[] = ajax_command_replace($tag_cloud_selector, $tag_cloud_row);
    }
  }
  $commands[] = ajax_command_prepend("#content", theme('status_messages'));
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}