Jan 07 2017
Jan 07
If you want to submit node form (or any form) via ajax request you need to follow next steps:
  1. Alter needed form: add container for validation messages and ajax callback for submit action.
  2. Implement ajax callback that returns ajax commands.
<?php

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function module_form_node_form_alter(&$form, &$form_state, $form_id) {
  // Add container for validation messages.
  $form['#prefix'] = '
';

  // Add ajax callback for submit action.
  $form['actions']['submit']['#ajax'] = [
    'callback' => 'module_node_form_ajax_submit',
  ];

  // Don't forget to include node.pages.inc file. Without it Drupal will not be
  // able to find and call node_form_validate() function.
  form_load_include($form_state, 'inc', 'node', 'node.pages');
}

/**
 * Form ajax submit callback.
 *
 * @return array
 *   Array contains Drupal ajax commands.
 */
function module_node_form_ajax_submit() {
  // Remove all status messages from $_SESSION['messages'] variable
  // because we want to render only error messages.
  drupal_get_messages('status');

  // Check form for possible validation messages.
  if (form_get_errors()) {
    // Validation failure: return rendered validation messages.
    $commands[] = ajax_command_html('#validation-messages', [theme('status_messages')]);
  }
  else {
    // Validation success: do something, for instance redirect user.
    ctools_include('ajax');
    $commands[] = ctools_ajax_command_redirect('some/page');
  }

  return [
    '#type' => 'ajax',
    '#commands' => $commands,
  ];
}

Key notes:

Jan 07 2017
Jan 07
If you have redirect functionality implemented with drupal_goto() function on hook_init() probably you aren't able to run drush commands because they crashe with a message:
Drush command terminated abnormally due to an unrecoverable error.  [error]
It happens because drush bootstraps Drupal application but can't perform redirect. To avoid this you need to check whether script runs from cli or not:
<?php

/**
 * Implements hook_init().
 */
function module_init() {
  if (!drupal_is_cli()) {
    drupal_goto('some/path');
  }
}

Key notes:

Jan 07 2017
Jan 07
When you build sql select queries using Drupal database API often you need to debug them. I mean to copy sql string, paste into sql editor, run it and see a result. If you cast query object to string you simply get a sql query with placeholders instead of their values. To get a string with replaced placeholders by values you can use this function:

You can also use devel's dpq() function but I prefer have a small helper module with needed functions only. Usage example:


Key notes:

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web