Commit 72a66d35 authored by Christoffer Johansson's avatar Christoffer Johansson
Browse files

Added function documentation. Moved formatter functions to cck_country_formatter.inc.

parent dcf71e75
Loading
Loading
Loading
Loading
+127 −93
Original line number Diff line number Diff line
<?php
// $Id$

function cck_country_field_info() {
  return array(
    'cck_country' => array('label' => 'Country'),
  );
}

function cck_country_widget_info() {
  return array(
    'cck_country_select' => array(
      'label' => 'Select List',
      'field types' => array('cck_country'),
      'multiple values' => CONTENT_HANDLE_CORE,
      'callbacks' => array(
        'default value' => CONTENT_CALLBACK_DEFAULT,
      ),
    ),
  );
}

function cck_country_select_process($element, $edit, &$form_state, $form) {
  $field_name = $element['#field_name'];
  $field = $form['#field_info'][$field_name];
  $field_key = $element['#columns'][0];

  $options = cck_country_allowed_values();

  if (!$field['required']) {
    $options = array('' =>  t('- None -')) + $options;
  }

  // For this specific widget, HTML should be filtered out and entities left unencoded.
  // See content_allowed_values / content_filter_xss / filter_xss.
  content_allowed_values_filter_html($options);

  $element[$field_key] = array(
    '#type' => 'select',
    '#title' => $element['#title'],
    '#description' => $element['#description'],
    '#required' => isset($element['#required']) ? $element['#required'] : $field['required'],
    '#multiple' => isset($element['#multiple']) ? $element['#multiple'] : $field['multiple'],
    '#options' => $options,
    '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
  );

  // Make sure field info will be available to the validator which
  // does not get the values in $form.

  // TODO for some reason putting the $field array into $form_state['storage']
  // causes the node's hook_form_alter to be invoked twice, garbling the
  // results. Need to investigate why that is happening (a core bug?), but
  // in the meantime avoid using $form_state['storage'] to store anything.
  $form_state['#field_info'][$field['field_name']] = $field;
  return $element;
}

function cck_country_elements() {
  return array(
    'cck_country_select' => array(
      '#input' => TRUE,
      '#columns' => array('value'), '#delta' => 0,
      '#process' => array('cck_country_select_process'),
      '#autocomplete_path' => FALSE,
    ),
  );
}

function theme_cck_country_select($element) {
  return $element['#children'];
}

/**
 * @file
 * Country selectlist for CCK.
 */

/**
 * Implementation of hook_theme().
 */
function cck_country_theme() {
  $themes = array(
    'cck_country_select' => array(
@@ -78,12 +16,15 @@ function cck_country_theme() {
    ),
    'cck_country_formatter_default' => array(
      'arguments' => array('element' => NULL),
      'file' => 'cck_country_formatter.inc',
    ),
    'cck_country_formatter_code2' => array(
      'arguments' => array('element' => NULL),
      'file' => 'cck_country_formatter.inc',
    ),
    'cck_country_formatter_code3' => array(
      'arguments' => array('element' => NULL),
      'file' => 'cck_country_formatter.inc',
    ),
  );

@@ -92,10 +33,12 @@ function cck_country_theme() {
      $themes['cck_country_formatter_' . $iconsets['name'] . '_image'] = array(
        'arguments' => array('element' => NULL),
        'function' => 'theme_cck_country_formatter_countryicons_image',
        'file' => 'cck_country_formatter.inc',
      );
      $themes['cck_country_formatter_' . $iconsets['name'] . '_sprite'] = array(
        'arguments' => array('element' => NULL),
        'function' => 'theme_cck_country_formatter_countryicons_sprite',
        'file' => 'cck_country_formatter.inc',
      );
    }
  }
@@ -103,13 +46,33 @@ function cck_country_theme() {
  return $themes;
}

function cck_country_content_is_empty($item, $field) {
  if (empty($item['value'])) {
    return TRUE;
/**
 * Implementation of hook_elements().
 */
function cck_country_elements() {
  return array(
    'cck_country_select' => array(
      '#input' => TRUE,
      '#columns' => array('value'), '#delta' => 0,
      '#process' => array('cck_country_select_process'),
      '#autocomplete_path' => FALSE,
      '#element_validate' => array('cck_country_select_validate'),
    ),
  );
}
  return FALSE;

/**
 * Implementation of hook_field_info().
 */
function cck_country_field_info() {
  return array(
    'cck_country' => array('label' => 'Country'),
  );
}

/**
 * Implementation of hook_field_formatter_info().
 */
function cck_country_field_formatter_info() {
  $formatters = array(
    'default' => array(
@@ -147,12 +110,15 @@ function cck_country_field_formatter_info() {
  return $formatters;
}

/**
 * Implementation of hook_field_settings().
 */
function cck_country_field_settings($op, $field) {
  switch ($op) {
    case 'database columns':
    if ($field['type'] == 'cck_country') {
        $columns = array(
          'value' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE),
          'value' => array('type' => 'varchar', 'length' => 2, 'not null' => FALSE),
        );
      }
    return $columns;
@@ -160,6 +126,25 @@ function cck_country_field_settings($op, $field) {
  }
}

/**
 * Implementation of hook_widget_info().
 */
function cck_country_widget_info() {
  return array(
    'cck_country_select' => array(
      'label' => 'Select List',
      'field types' => array('cck_country'),
      'multiple values' => CONTENT_HANDLE_CORE,
      'callbacks' => array(
        'default value' => CONTENT_CALLBACK_DEFAULT,
      ),
    ),
  );
}

/**
 * Implementation of hook_widget().
 */
function cck_country_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  $element = array(
    '#type' => $field['widget']['type'],
@@ -173,31 +158,80 @@ function cck_country_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  return $element;
}

/**
 * Implementation of hook_content_is_empty().
 */
function cck_country_content_is_empty($item, $field) {
  if (empty($item['value'])) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Allowed values for cck_country_select.
 *
 * @return
 *   array with 2-digit country code as KEY and name as VALUE.
 */
function cck_country_allowed_values() {
  return countries_api_get_array();
}

function theme_cck_country_formatter_default($element) {
  $country = countries_api_get_country($element['#item']['value']);
  return $country['printable_name'];
/**
 * Procces the element cck_country_select.
 */
function cck_country_select_process($element, $edit, &$form_state, $form) {
  $field_name = $element['#field_name'];
  $field = $form['#field_info'][$field_name];
  $field_key = $element['#columns'][0];

  $options = cck_country_allowed_values();

  if (!$field['required']) {
    $options = array('' =>  t('- None -')) + $options;
  }

function theme_cck_country_formatter_code2($element) {
  return $element['#item']['value'];
  $element[$field_key] = array(
    '#type' => 'select',
    '#title' => $element['#title'],
    '#description' => $element['#description'],
    '#required' => isset($element['#required']) ? $element['#required'] : $field['required'],
    '#multiple' => isset($element['#multiple']) ? $element['#multiple'] : $field['multiple'],
    '#options' => $options,
    '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
  );

  // Make sure field info will be available to the validator which
  // does not get the values in $form.

  // TODO for some reason putting the $field array into $form_state['storage']
  // causes the node's hook_form_alter to be invoked twice, garbling the
  // results. Need to investigate why that is happening (a core bug?), but
  // in the meantime avoid using $form_state['storage'] to store anything.
  $form_state['#field_info'][$field['field_name']] = $field;

  return $element;
}

function theme_cck_country_formatter_code3($element) {
  $country = countries_api_get_country($element['#item']['value']);
  return $country['iso3'];
/**
 * Validate the element cck_country_select.
 */
function cck_country_select_validate(&$element, &$form_state) {
	$allowed = cck_country_allowed_values();

	if (!$element['required']) {
		$allowed = array('' => '') + $allowed;
	}

function theme_cck_country_formatter_countryicons_image($element) {
  $iconset = drupal_substr($element['#formatter'], 0, -6);
  $country = countries_api_get_country($element['#item']['value']);
  return theme('countryicons_icon', $element['#item']['value'], $iconset, $country['printable_name'], $country['printable_name']);
	if (!array_key_exists($element['#value']['value'], $allowed)) {
		form_error($element, t('The value you have selected is not valid!'));
	}	
}

function theme_cck_country_formatter_countryicons_sprite($element) {
  $iconset = drupal_substr($element['#formatter'], 0, -7);
  return theme('countryicons_icon_sprite', $element['#item']['value'], $iconset);
/**
 * Theme the element cck_country_select.
 */
function theme_cck_country_select($element) {
  return $element['#children'];
}
+49 −0
Original line number Diff line number Diff line
<?php
// $Id$

/**
 * @file
 * This file contains CCK formatter related functionality.
 */

/**
 * Theme function for the 'default' cck_country formatter.
 */
function theme_cck_country_formatter_default($element) {
  $country = countries_api_get_country($element['#item']['value']);
  return $country['printable_name'];
}

/**
 * Theme function for the 'code2' cck_country formatter.
 */

function theme_cck_country_formatter_code2($element) {
  return $element['#item']['value'];
}

/**
 * Theme function for the 'code3' cck_country formatter.
 */

function theme_cck_country_formatter_code3($element) {
  $country = countries_api_get_country($element['#item']['value']);
  return $country['iso3'];
}

/**
 * Theme function for the 'countryicons_image' cck_country formatter.
 */
function theme_cck_country_formatter_countryicons_image($element) {
  $iconset = drupal_substr($element['#formatter'], 0, -6);
  $country = countries_api_get_country($element['#item']['value']);
  return theme('countryicons_icon', $element['#item']['value'], $iconset, $country['printable_name'], $country['printable_name']);
}

/**
 * Theme function for the 'countryicons_sprite' cck_country formatter.
 */
function theme_cck_country_formatter_countryicons_sprite($element) {
  $iconset = drupal_substr($element['#formatter'], 0, -7);
  return theme('countryicons_icon_sprite', $element['#item']['value'], $iconset);
}