You are here

entityreference_autofill.install in Entity reference autofill 7

Install and update functions for the Entity reference autofill module.

File

entityreference_autofill.install
View source
<?php

/**
 * @file
 * Install and update functions for the Entity reference autofill module.
 */

/**
 * Implements hook_install().
 */
function entityreference_autofill_install() {

  // Set weight for module.
  entityreference_autofill_set_module_weight();
}

/**
 * Implements hook_modules_enabled().
 */
function entityreference_autofill_modules_enabled($modules) {
  if (in_array('entityreference_prepopulate', $modules)) {

    // Update module weight to let prioritize prepopulate.
    entityreference_autofill_set_module_weight();
  }
}

/**
 * Relative module weight setter.
 */
function entityreference_autofill_set_module_weight() {
  if (module_exists('entityreference_prepopulate')) {

    // Get weight of entityreference_prepopulate.
    $weight = db_select('system', 's')
      ->fields('s', array(
      'weight',
    ))
      ->condition('name', 'entityreference_prepopulate', '=')
      ->execute()
      ->fetchField();

    // Set this module's weight one higher.
    db_update('system')
      ->fields(array(
      'weight' => $weight + 1,
    ))
      ->condition('name', 'entityreference_autofill', '=')
      ->execute();
  }
}

/**
 * Update module weight to give prepopulate priority.
 */
function entityreference_autofill_update_7000() {
  entityreference_autofill_set_module_weight();
}

/**
 * Move widget settings to behavior class settings.
 */
function entityreference_autofill_update_7001() {

  // Rebuild settings.
  $field_map = field_info_field_map();
  $enabled_fields = array();
  foreach ($field_map as $field_name => $field) {
    if ($field['type'] !== 'entityreference') {
      continue;
    }
    foreach ($field['bundles'] as $entity_type => $bundles) {
      foreach ($bundles as $bundle) {
        $field_info = field_info_instance($entity_type, $field_name, $bundle);
        $field_widget = $field_info['widget'];
        $is_ac_widget = $field_widget['type'] === 'entityreference_autocomplete';
        $is_enabled = $is_ac_widget && $field_widget['settings']['autofill']['enabled'];
        if ($is_enabled) {

          // Remove enabled setting.
          $field_widget['settings']['autofill']['status'] = $field_widget['settings']['autofill']['enabled'];
          unset($field_widget['settings']['autofill']['enabled']);
          $field_info['settings']['behaviors']['autofill'] = $field_widget['settings']['autofill'];

          // Remove old settings.
          unset($field_widget['settings']['autofill']);
          field_update_instance($field_info);
        }
      }
    }
  }
  _entityreference_autofill_get_settings(TRUE);
}

Functions

Namesort descending Description
entityreference_autofill_install Implements hook_install().
entityreference_autofill_modules_enabled Implements hook_modules_enabled().
entityreference_autofill_set_module_weight Relative module weight setter.
entityreference_autofill_update_7000 Update module weight to give prepopulate priority.
entityreference_autofill_update_7001 Move widget settings to behavior class settings.