You are here

function _webform_render_addressfield in Addressfield Tokens 7

Render a Webform component to be part of a form.

Parameters

mixed $component: A Webform component array.

mixed $value: If editing an existing submission or resuming a draft, this will contain an array of values to be shown instead of the default in the component configuration. This value will always be an array, keyed numerically for each value saved in this field.

bool $filter: Whether or not to filter the contents of descriptions and values when rendering the component. Values need to be unfiltered to be editable by Form Builder.

Return value

array Form element.

See also

_webform_client_form_add_component()

File

./addressfield_tokens.components.inc, line 107
Webform Component information for an address field type.

Code

function _webform_render_addressfield($component, $value = NULL, $filter = TRUE) {
  $element = array(
    '#type' => 'fieldset',
    '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
    '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
    '#attributes' => $component['extra']['attributes'],
    '#theme_wrappers' => array(
      'webform_element',
    ),
    '#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
    '#required' => $component['required'],
    '#weight' => $component['weight'],
    '#translatable' => array(
      'title',
      'description',
    ),
  );
  $available = !empty($component['extra']['available_countries']) ? $component['extra']['available_countries'] : NULL;

  // Get the current address.
  $address = _addressfield_tokens_expand_value($value);
  if (empty($address)) {
    if (!empty($component['value'])) {
      $address = $component['value'];
    }
    else {
      $address = _webform_addressfield($component['cid']);
    }
  }
  if (empty($address)) {
    $address = _webform_addressfield_default_values($available, $component);
  }

  // Generate the address form.
  $context = array(
    'mode' => 'form',
    'instance' => array(
      'required' => $component['required'],
    ),
    'form_key' => $component['form_key'],
  );
  $handlers = !empty($component['extra']['format_handlers']) ? $component['extra']['format_handlers'] : array(
    'address',
  );
  $element += addressfield_generate($address, $handlers, $context);
  if (empty($address['country'])) {
    $element['street_block']['#access'] = FALSE;
    $element['locality_block']['#access'] = FALSE;
  }
  if (isset($element['country'])) {
    if (!empty($available)) {
      $element['country']['#options'] = array_intersect_key($element['country']['#options'], $available);

      // Hide the country element only if there is one option and the whole field
      // is required, otherwise there will always be an additional None option.
      // @see addressfield_format_address_hide_country()
      if (!empty($handlers['address-hide-country']) && count($element['country']['#options']) == 1 && $component['required']) {
        $element['country']['#access'] = FALSE;
      }
    }
    $element['country']['#default_value'] = $address['country'];
    $element['country']['#element_validate'] = array(
      '_webform_addressfield_country_validate',
    );
    $element['country']['#cid'] = $component['cid'];
    $element['country']['#limit_validation_errors'] = array();
  }
  $form_state = array();
  drupal_alter('field_widget_addressfield_standard_form', $element, $form_state, $context);
  return $element;
}