Is there a way for me to make company street and country required but not zip code?

Thanks. I've been trying to figure it out for a bit.

Comments

RedEight’s picture

This can be accomplished with a custom ctools format plugin.

YOUR_MODULE/plugins/format/address_custom_required.inc

/**
 * Requires Street and City but not ZIP.
 */

$plugin = array(
  'title' => t('Street and City are required, ZIP is optional'),
  'format callback' => 'addressfield_alter_format_address_custom_required',
  'type' => 'address',
  'weight' => 100,
);

/**
 * Format callback.
 *
 * @see CALLBACK_addressfield_format_callback()
 */
function addressfield_alter_format_address_custom_required(&$format, $address, $context = array()) {
  // Require Street address
  $format['street_block']['thoroughfare']['#required'] = TRUE;
  // Require City
  $format['locality_block']['locality']['#required'] = TRUE;
  // Make ZIP Optional
  $format['locality_block']['postal_code']['#required'] = FALSE;
}

This will need to be placed in a folder that ctools has been told is a format plugin directory. You can accomplish that with the following in your YOUR_MODULE/YOUR_MODULE.module

function HOOK_ctools_plugin_directory($module, $plugin) {
  if ($module == 'addressfield') {
    return 'plugins/' . $plugin;
  }
}

/**
 * Implements hook_ctools_plugin_type().
 */
function HOOK_ctools_plugin_type() {
  $plugins['format'] = array(
    'load themes' => TRUE,
  );
  return $plugins;
}

You can adjust the weight as needed to get it to apply in the correct order relative to the other formatters.

bojanz’s picture

Status: Active » Fixed

Thank you, RedEight!
Feel free to copy your answer to a new documentation page (click "add child page" on https://www.drupal.org/node/1267280), for extra karma ;)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.