You are here

function simpleads_campaigns_form_alter in SimpleAds 7

Implements hook_form_alter().

File

modules/simpleads_campaigns/simpleads_campaigns.module, line 82

Code

function simpleads_campaigns_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'simpleads_node_form') {
    $lang = array_key_exists('#value', $form['language']) ? $form['language']['#value'] : $form['language']['#default_value'];
    $campaigns = array(
      '_none' => t('- None -'),
    );
    $results = db_select('node', 'n')
      ->fields('n', array(
      'title',
      'nid',
    ))
      ->condition('n.status', 1)
      ->condition('n.type', 'simpleads_campaign')
      ->orderBy('n.title')
      ->execute();
    foreach ($results as $row) {
      $campaigns[$row->nid] = $row->title;
    }
    $node = FALSE;
    if (isset($form['#node']) && is_object($form['#node'])) {
      $node = $form['#node'];
    }
    if (isset($form['field_adcamp_list'][$lang])) {
      $form['field_adcamp_list'][$lang]['#prefix'] = '<div style="display:none;">';
      $form['field_adcamp_list'][$lang]['#suffix'] = '</div>';
      $title = $form['field_adcamp_list'][$lang][0]['#title'];
      $description = $form['field_adcamp_list'][$lang][0]['#description'];
      $default = "";
      if ($node && isset($node->field_adcamp_list[$node->language]) && !empty($node->field_adcamp_list[$node->language][0]['safe_value'])) {
        $default = $node->field_adcamp_list[$node->language][0]['safe_value'];
      }
      $form['campaign'] = array(
        '#type' => 'select',
        '#title' => check_plain($title),
        '#description' => check_plain($description),
        '#weight' => 9,
        '#options' => $campaigns,
        '#default_value' => $default,
      );
    }
    else {
      if (isset($form['field_adcamp_list'][LANGUAGE_NONE])) {
        $form['field_adcamp_list'][LANGUAGE_NONE]['#prefix'] = '<div style="display:none;">';
        $form['field_adcamp_list'][LANGUAGE_NONE]['#suffix'] = '</div>';
        $title = $form['field_adcamp_list'][LANGUAGE_NONE][0]['#title'];
        $description = $form['field_adcamp_list'][LANGUAGE_NONE][0]['#description'];
        $default = "";
        if ($node && isset($node->field_adcamp_list[LANGUAGE_NONE]) && !empty($node->field_adcamp_list[LANGUAGE_NONE][0]['safe_value'])) {
          $default = $node->field_adcamp_list[LANGUAGE_NONE][0]['safe_value'];
        }
        $form['campaign'] = array(
          '#type' => 'select',
          '#title' => check_plain($title),
          '#description' => check_plain($description),
          '#weight' => 9,
          '#options' => $campaigns,
          '#default_value' => $default,
        );
      }
    }
  }
}