You are here

function brazilianids_field in Brazilian IDs 6

Implementation of hook_field().

File

./brazilianids_cck.inc, line 49
brazilianids_cck.inc Deal with brazilian IDs like CPF and CNPJ as CCK fields.

Code

function brazilianids_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      if (is_array($items)) {
        module_load_include('check.inc', 'brazilianids', 'brazilianids');
        foreach ($items as $delta => $item) {
          $error_element = isset($item['_error_element']) ? $item['_error_element'] : '';
          if (is_array($item) && isset($item['_error_element'])) {
            unset($item['_error_element']);
          }
          if (!empty($item['value'])) {

            // Validate the CPF, if any
            $error_element = isset($item['_error_element']) ? $item['_error_element'] : '';
            if ($field['type'] == 'brazilianids_cpf' && !_brazilianids_validate_cpf($item['value'])) {
              form_set_error($field['field_name'], t('"%value" is not a valid CPF', array(
                '%value' => $item['value'],
              )));
            }

            // Validate the CNPJ, if any
            if ($field['type'] == 'brazilianids_cnpj' && !_brazilianids_validate_cnpj($item['value'])) {
              form_set_error($field['field_name'], t('"%value" is not a valid CNPJ', array(
                '%value' => $item['value'],
              )));
            }

            //  Validate the CPF OR CNPJ, if any
            if ($field['type'] == 'brazilianids_cpfcnpj' && !_brazilianids_validate_cpf($item['value']) && !_brazilianids_validate_cnpj($item['value'])) {
              form_set_error($field['field_name'], t('"%value" is not a valid CPF neither CNPJ', array(
                '%value' => $item['value'],
              )));
            }
          }
        }
      }
      return $items;
    case 'sanitize':
      if (is_array($items)) {
        foreach ($items as $delta => $item) {
          $brazilianids = check_plain($item['value']);
          $items[$delta]['safe'] = $brazilianids;
        }
      }
    case 'view':
      if (is_array($items)) {

        // Include the validation/format functions
        module_load_include('check.inc', 'brazilianids', 'brazilianids');
        foreach ($items as $delta => $item) {
          if (is_array($item) && !empty($item['value'])) {

            // Format CPF OR CNPJ, if any
            $items[$delta]['view'] = _brazilianids_is_cpf_cnpj($item['value']) == 'CPF' ? _brazilianids_format_cpf($item['value']) : _brazilianids_format_cnpj($item['value']);
          }
        }
      }
  }
}