You are here

name.install in Name Field 7

Same filename and directory in other branches
  1. 8 name.install
  2. 6 name.install

Standard installation functions for name.

File

name.install
View source
<?php

/**
 * @file
 * Standard installation functions for name.
 */

/**
 * Implements hook_field_schema().
 *
 * There is no path backwards in Drupal 7 when it comes to dropping columns or
 * reducing the size of the columns (via coresponence mid 2010). So as a varchar
 * is just the data + a terminator byte, all fields get all columns with vc(255).
 */
function name_field_schema($field) {
  module_load_include('module', 'name');
  $columns = array();
  foreach (_name_translations() as $key => $title) {
    $columns[$key] = array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => FALSE,
    );
  }
  return array(
    'columns' => $columns,
    'indexes' => array(
      'given' => array(
        'given',
      ),
      'family' => array(
        'family',
      ),
    ),
  );
}

/**
 * Implements hook_schema().
 */
function name_schema() {
  $schema = array();
  $schema['name_custom_format'] = array(
    'fields' => array(
      'ncfid' => array(
        'description' => 'The primary identifier for a custom format.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'The name to identify the custom format to a user.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'machine_name' => array(
        'description' => 'The machine name to identify the custom format to the system.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'format' => array(
        'description' => 'The format string to apply to names.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'ncfid',
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 *
 * Inserts some common name formats into the database.
 */
function name_install() {
  name_install_default_formats();
}

/**
 * Implements hook_uninstall().
 */
function name_uninstall() {
  variable_del('name_settings');
  variable_del('name_user_preferred');

  // There is no UI for this setting.
  variable_del('name_example_names');
  variable_del('name_inline_styles');
  variable_del('name_inline_styles_rtl');
  variable_del('name_element_wrapper');
  variable_del('name_component_required_marker');
}

/**
 * This adds the formats that would otherwise be inserted during installation.
 */
function name_update_6000() {
  $ret = array();

  // Create the table.
  $schema = array(
    'fields' => array(
      'ncfid' => array(
        'description' => t('The primary identifier for a custom format.'),
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => t('The name to identify the custom format to a user.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'machine_name' => array(
        'description' => t('The machine name to identify the custom format to the system.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'format' => array(
        'description' => t('The format string to apply to names.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'ncfid',
    ),
  );
  db_create_table('name_custom_format', $schema);

  // Install some default values.
  $t = get_t();
  name_install_default_formats();

  // Clear the caches.
  cache_clear_all('*', 'cache', TRUE);
  cache_clear_all('*', 'cache_content', TRUE);
  if (module_exists('views')) {

    // Needed because this can be called from .install files.
    module_load_include('module', 'views');
    views_invalidate_cache();
  }
  return t('Created name formats table {name_custom_format}.') . '<br/>' . $t('The default set of custom name format strings was generated and saved. These are listed !here.', array(
    '!here' => l($t('here', 'admin/config/content/name')),
  ));
}

/**
 * Move the user field selected realname into a variable to avoid doing lookups
 * on all user fields / instances during hook_user_load().
 */
function name_update_7000() {
  foreach (field_info_instances('user', 'user') as $instance) {
    $field = field_info_field($instance['field_name']);
    if ($field['type'] == 'name' && !empty($instance['settings']['override_username'])) {
      variable_set('name_user_preferred', $instance['field_name']);
      break;
    }
  }
}

/**
 * Updates the field formatters to the new singular formatter type.
 */
function name_update_7002() {
  foreach (field_info_fields() as $field_name => $field) {
    if ($field['type'] == 'name' && !empty($field['bundles'])) {
      foreach ($field['bundles'] as $entity_type => $bundles) {
        foreach ($bundles as $bundle) {
          $changed = FALSE;
          $instance = field_info_instance($entity_type, $field_name, $bundle);
          if (!empty($instance['display'])) {
            foreach ($instance['display'] as $view_mode => &$display) {
              list($name, $output, $format) = explode('_', $display['type'] . '__');
              if ($name == 'name') {
                switch ($output) {
                  case 'default':
                  case 'plain':
                  case 'raw':
                    $display['type'] = 'name_formatter';
                    $display['settings']['output'] = $output;
                    if (empty($format)) {
                      $format = 'default';
                    }
                    $display['settings']['format'] = $format;

                    // Ensure that the display settings for the two new fields
                    // are set while we are updating the other display settings.
                    $defaults = array(
                      'markup' => 0,
                      'multiple' => 'default',
                      'multiple_delimiter' => ', ',
                      // And or symbol.
                      'multiple_and' => 'text',
                      // contextual, always, never.
                      'multiple_delimiter_precedes_last' => 'never',
                      'multiple_el_al_min' => 3,
                      'multiple_el_al_first' => 1,
                    );
                    foreach ($defaults as $key => $value) {
                      if (!isset($display['settings'][$key])) {
                        $display['settings'][$key] = $value;
                      }
                    }
                    $changed = TRUE;
                    break;
                  default:
                }
              }
            }
          }
          if ($changed) {
            field_update_instance($instance);
          }
        }
      }
    }
  }

  // The new formatter settings will not save unless we clear the caches.
  field_cache_clear();
  cache_clear_all();
}

/**
 * Inserts some common formats.
 */
function name_install_default_formats() {
  $t = get_t();
  $formats = array(
    array(
      'format' => '((((t+ig)+im)+if)+is)+jc',
      'name' => $t('Full'),
      'machine_name' => 'full',
    ),
    array(
      'format' => 'g',
      'name' => $t('Given'),
      'machine_name' => 'given',
    ),
    array(
      'format' => 'f',
      'name' => $t('Family'),
      'machine_name' => 'family',
    ),
    array(
      'format' => 't+if',
      'name' => $t('Title Family'),
      'machine_name' => 'formal',
    ),
    array(
      'format' => 'g+if',
      'name' => $t('Given Family'),
      'machine_name' => 'short_full',
    ),
  );
  foreach ($formats as $format) {
    db_insert('name_custom_format')
      ->fields(array(
      'name' => $format['name'],
      'machine_name' => $format['machine_name'],
      'format' => $format['format'],
    ))
      ->execute();
  }
}

Functions

Namesort descending Description
name_field_schema Implements hook_field_schema().
name_install Implements hook_install().
name_install_default_formats Inserts some common formats.
name_schema Implements hook_schema().
name_uninstall Implements hook_uninstall().
name_update_6000 This adds the formats that would otherwise be inserted during installation.
name_update_7000 Move the user field selected realname into a variable to avoid doing lookups on all user fields / instances during hook_user_load().
name_update_7002 Updates the field formatters to the new singular formatter type.