0

寻求帮助。我想从 Drupal 表单中传递组 ID 以及所有其他 zendesk 票证信息。我已经尝试了所有方法,但无法让 group-id 工作。其他一切都在工作。这是我目前拥有的:

    function zendesk_forms_support_form() {
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Your name'),
    '#required' => TRUE,
  );
  $form['group_id'] = array(
  '#title' => t('Group ID'),
  '#type' => 'textfield',
  );
  $form['requester'] = array(
    '#type' => 'textfield',
    '#title' => t('Your e-mail address'),
    '#required' => TRUE,
  );
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Send'),
  );
  $form['#attributes'] = array('class' => 'zendesk-support-form');
  return $form;
}

function zendesk_forms_support_form_submit($form, &$form_state) {
  $ticket = array(
    'description' => $form_state['values']['description'],
    'requester' => array(
      'name' => $form_state['values']['name'],
      'email' => $form_state['values']['requester'],
      'group_id' => $form_state['values']['group_id'],
    ),
  );
  if (!empty($form_state['values']['subject'])) {
    $ticket['subject'] = $form_state['values']['subject'];
  }

这就是全部!

<?php
/**
 * @file
 * Main file for the Zendesk Forms module.
 */

/**
 * Implements hook_menu().
 */
function zendesk_forms_menu() {
  $items = array();
  $items['admin/config/services/zendesk-form'] = array(
    'title' => 'Zendesk forms',
    'description' => 'Configure Zendesk forms.',
    'access arguments' => array('administer zendesk form'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('zendesk_forms_settings_form'),
    'file' => 'zendesk_forms.admin.inc',
    'file path' => drupal_get_path('module', 'zendesk_forms'),
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function zendesk_forms_permission() {
  return array(
    'administer zendesk form' => array(
      'title' => t('Administer Zendesk Form'),
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function zendesk_forms_block_info() {
  $blocks = array();
  $blocks['zendesk_support_form'] = array(
    'info' => t('Zendesk support form'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function zendesk_forms_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'zendesk_support_form':
      $block['subject'] = t('Support');
      $block['content'] = drupal_get_form('zendesk_forms_support_form');
      break;
  }
  return $block;
}

/**
 * Form to create a ticket with Zendesk.
 *
 * @ingroup forms
 */
function zendesk_forms_support_form() {
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Your name'),
    '#required' => TRUE,
  );
  $form['group_id'] = array(
  '#title' => t('Group ID'),
  '#type' => 'textfield',
  );
  $form['requester'] = array(
    '#type' => 'textfield',
    '#title' => t('Your e-mail address'),
    '#required' => TRUE,
  );
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Send'),
  );
  $form['#attributes'] = array('class' => 'zendesk-support-form');
  return $form;
}

/**
 * Validate the create ticket form.
 *
 * @ingroup forms
 */
function zendesk_forms_support_form_validate($form, &$form_state) {
  if (!valid_email_address($form_state['values']['requester'])) {
    form_set_error('requester', t('Supplied email is not valid'));
  }
}

/**
 * Submit handler for the create ticket form.
 *
 * @ingroup forms
 */
function zendesk_forms_support_form_submit($form, &$form_state) {
  $ticket = array(
    'description' => $form_state['values']['description'],
    'requester' => array(
      'name' => $form_state['values']['name'],
      'email' => $form_state['values']['requester'],
      'group_id' => $form_state['values']['group_id'],
    ),
  );
  if (!empty($form_state['values']['subject'])) {
    $ticket['subject'] = $form_state['values']['subject'];
  }
  // Get extra fields that are prefixed with zendesk_. Right now the only way
  // to add extra fields is by using form alter though.
  $extra_fields = array_filter(array_keys($form_state['values']), create_function('$item', 'return strpos($item, "zendesk_") === 0;'));
  if (!empty($extra_fields)) {
    foreach ($extra_fields as $field_name) {
      $ticket[substr($field_name, 8)] = $form_state['values'][$field_name];
    }
  }
  // Wrap the ticket array in yet another array and call the API.
  $response = zendesk_forms_api_call(array('ticket' => $ticket), 'tickets.json');
  if (!isset($response->error)) {
    drupal_set_message(t('Your message has been sent'));
  }
  else {
    drupal_set_message(t('An error happened while sending the message.'), 'error');
  }
}

/**
 * Performs a call to the Zendesk API with the given data.
 *
 * @param array $data
 *   The data to be sent to the API endpoint.
 * @param string $command
 *   The command to call at the endpoint. Will be a filename like "tickets.json"
 * or similar.
 * See http://developer.zendesk.com/documentation/rest_api/tickets.html and
 * https://support.zendesk.com/entries/21562288-Creating-a-Custom-HTML-Form-for-Ticket-Submission
 *
 * @return string
 *   The json response string.
 */
function zendesk_forms_api_call($data, $command) {
  $json = json_encode($data);
  $zendesk_key = variable_get('zendesk_forms_api_key', '');
  $zendesk_url = variable_get('zendesk_forms_url', '');
  $zendesk_user = variable_get('zendesk_forms_user_name', '');
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  curl_setopt($ch, CURLOPT_URL, $zendesk_url . '/api/v2/' . $command);
  curl_setopt($ch, CURLOPT_USERPWD, "$zendesk_user/token:$zendesk_key");
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
  curl_setopt($ch, CURLOPT_USERAGENT, 'MozillaXYZ/1.0');
  curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  $output = curl_exec($ch);
  curl_close($ch);
  return json_decode($output);
}
4

1 回答 1

0
 $ticket = array(
    'description' => $form_state['values']['description'],
    'requester' => array(
      'name' => $form_state['values']['name'],
      'email' => $form_state['values']['requester'],
      'group_id' => $form_state['values']['group_id'],
    ),

变成

 $ticket = array(
    'description' => $form_state['values']['description'],
      'group_id' => $form_state['values']['group_id'],
    'requester' => array(
      'name' => $form_state['values']['name'],
      'email' => $form_state['values']['requester'],
    ),
于 2014-03-01T12:21:23.007 回答