You are here

autofill.module in Autofill 6

Same filename and directory in other branches
  1. 8 autofill.module

Main module file for the autofill module for Nojoshmo

File

autofill.module
View source
<?php

/**
 * @file
 * Main module file for the autofill module for Nojoshmo
 */

/**
 * Implements hook_help().
 */
function autofill_help() {
  if ($path == 'admin/help#autofill') {
    return t('Provide autocompletion to Drupal fields');
  }
}

/**
 * Implements hook_init().
 */
function autofill_init() {

  // Add jQuery Autofill library
  if ($path = libraries_get_path('jquery-autofill')) {
    drupal_add_js($path . '/jquery.autofill.min.js');
  }

  // Include user generated conditions for Autofill
  $path = 'sites/default/files';
  if (file_exists($path . '/autofill.js')) {

    // Include user generated conditions in sites/all/default/files/autofill.js
    drupal_add_js($path . '/autofill.js');
  }
  else {

    // Include user generated conditions from admin/settings/autofill
    $path = drupal_get_path('module', 'autofill');
    drupal_add_js($path . '/js/autofill.js');
  }
  $fields = variable_get('autofill_id_field_combos', array());
  if (empty($fields)) {

    // Get fields.
    $fields_pre = array(
      array(
        'id' => '#edit-info',
        'value' => 'eg. Add title here',
        'pre' => 'Hallo world',
      ),
    );
    variable_set('autofill_id_field_combos', $fields_pre);
  }
}

/**
 * Implements hook_perm.
 */
function autofill_perm() {
  return array(
    'administer autofill',
  );
}

/**
 * Implements hook_menu().
 */
function autofill_menu() {
  $items = array();
  $path = drupal_get_path('module', 'autofill');
  $items[$path . '/js/autofill.js'] = array(
    'title' => 'Autofill JS',
    'description' => 'Add id and values to Autofill.',
    'page callback' => 'autofill_js',
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      TRUE,
    ),
  );
  $items['admin/settings/autofill'] = array(
    'title' => 'Autofill',
    'description' => 'Configure Autofill.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'autofill_form',
    ),
    'access arguments' => array(
      'administer autofill',
    ),
    'file' => 'autofill.admin.inc',
  );
  return $items;
}

/**
 * Generate custom Autofill JS file.
 */
function autofill_js() {
  $output = '';
  $output .= 'Drupal.behaviors.autofill = function(context){ ';
  $default_color = check_plain(variable_get('autofill_colour_default', '#ccc'));
  $active_color = check_plain(variable_get('autofill_colour_active', '#333'));
  $fields = variable_get('autofill_id_field_combos', array());
  foreach ($fields as $delta => $field) {
    $output .= "\n" . '$("' . check_plain($field['id']) . '").autofill( {
      value: "' . check_plain($field['value']) . '",
      prePopulate: "' . check_plain($field['pre']) . '",
      defaultTextColor: "' . $default_color . '",
      activeTextColor: "' . $active_color . '",
    });';
  }
  $output .= '}';
  header('Content-type: text/javascript;');
  echo $output;
  exit;
}

Functions

Namesort descending Description
autofill_help Implements hook_help().
autofill_init Implements hook_init().
autofill_js Generate custom Autofill JS file.
autofill_menu Implements hook_menu().
autofill_perm Implements hook_perm.