You are here

function simplenews_form_alter in Simplenews 6

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_form_alter()
  2. 6.2 simplenews.module \simplenews_form_alter()
  3. 7.2 simplenews.module \simplenews_form_alter()
  4. 7 simplenews.module \simplenews_form_alter()

Implementation of hook_form_alter().

File

./simplenews.module, line 493
Simplnews node handling, sent email, newsletter block and general hooks

Code

function simplenews_form_alter(&$form, $form_state, $form_id) {
  $vid = variable_get('simplenews_vid', '');

  // Newsletter vocabulary form
  if ($form_id == 'taxonomy_form_vocabulary' && isset($form['vid']) && $form['vid']['#value'] == $vid) {

    // Hide critical options from newsletter vocabulary.
    $form['help_simplenews_vocab'] = array(
      '#value' => t('This is the designated simplenews vocabulary.'),
      '#weight' => -1,
    );

    // We display the current content type settings in a disabled form element
    // to the user. The real value passed in the form separately because
    // disabled elements do not get saved at submit.
    $form['content_types']['display_only'] = $form['content_types']['nodes'];
    $form['content_types']['display_only']['#disabled'] = TRUE;
    $form['content_types']['display_only']['#description'] = t('These content type(s) are used as newsletter. They can be set in !simplenews_settings.', array(
      '!simplenews_settings' => l('Simplenews settings', 'admin/settings/simplenews'),
    ));
    $form['content_types']['nodes'] = array(
      '#type' => 'value',
      '#value' => $form['content_types']['nodes']['#default_value'],
    );

    // Free tagging can not be allowed see: simplenews_validate_taxonomy().
    $form['settings']['tags'] = array(
      '#type' => 'value',
      '#value' => FALSE,
    );

    // Multiple select does not work with simplenews.
    $form['settings']['multiple'] = array(
      '#type' => 'value',
      '#value' => FALSE,
    );
    $form['settings']['required'] = array(
      '#type' => 'value',
      '#value' => TRUE,
    );
  }
  elseif (isset($form['type']) && isset($form['#node']) && strpos($form_id, '_node_form')) {
    if (in_array($form['type']['#value'], variable_get('simplenews_content_types', array(
      'simplenews',
    )))) {

      // Available variables are based on user_mail_tokens().
      // But uses only those which can be used with uid = 0 since simplenews also sends to anonymous users.
      if (isset($form['body_field'])) {
        $form['body_field']['body']['#description'] = t("This will be the body of your newsletter. Available variables are: !site (the name of your website), !uri (a link to your homepage), !uri_brief (homepage link without the http://), !date (today's date), !login_uri (link to login page), !confirm_subscribe_url (subscription confirmation link), !confirm_unsubscribe_url (unsubscription link), !newsletter_url (link to this newsletter issue), !newsletter_name (name of this newsletter series).");
      }
      if (isset($form['#node']->simplenews)) {
        $simplenews_values = $form['#node']->simplenews;
      }
      $vocabulary = taxonomy_vocabulary_load(variable_get('simplenews_vid', ''));
      if (!empty($vocabulary) && !isset($vocabulary->nodes[$form['type']['#value']])) {
        drupal_set_message(t('Invalid vocabulary setting detected. Check and <strong>save</strong> the <a href="@settings">Simplenews general settings</a>.', array(
          '%name' => $vocabulary->name,
          '@settings' => url('admin/settings/simplenews'),
        )), 'error');
      }
      $form['simplenews'] = array(
        '#type' => 'fieldset',
        '#title' => t('Send newsletter'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#tree' => TRUE,
      );

      // Translations of newsletters don't have send and format options. Only the
      // translation source (and non translated) newsletters will get these options.
      if (module_exists('translation') && translation_supported_type($form['#node']->type) && (isset($form['#node']->translation_source) || $form['#node']->tnid && $form['#node']->tnid != $form['#node']->nid)) {
        $form['simplenews']['#description'] = t('This newsletter issue is part of a translation set. Sending this set is controlled from the <a href="@link">translation source newsletter</a>.', array(
          '@link' => url('node/' . $form['tnid']['#value']),
        ));

        // Send option of translated newsletters are not used, but a default is
        // required to prevent errors when data is stored in the database.
        $form['simplenews']['send'] = array(
          '#type' => 'hidden',
          '#value' => SIMPLENEWS_COMMAND_SEND_NONE,
        );
        $form['simplenews']['advanced']['s_format'] = array(
          '#type' => 'hidden',
          '#value' => variable_get('simplenews_format', 'plain'),
        );
        $form['simplenews']['advanced']['priority'] = array(
          '#type' => 'hidden',
          '#value' => variable_get('simplenews_priority', SIMPLENEWS_PRIORITY_NONE),
        );
        $form['simplenews']['advanced']['receipt'] = array(
          '#type' => 'hidden',
          '#value' => variable_get('simplenews_receipt', 0),
        );
      }
      else {

        // Show newsletter sending options if newsletter has not been send yet.
        // If send a nodification is shown.
        if (!isset($simplenews_values['s_status']) || isset($simplenews_values['s_status']) && $simplenews_values['s_status'] == SIMPLENEWS_STATUS_SEND_NOT) {

          // Add dynamic text for send button.
          drupal_add_js(drupal_get_path('module', 'simplenews') . '/simplenews.js', 'module');
          if (user_access('send newsletter')) {
            $options[SIMPLENEWS_COMMAND_SEND_NONE] = t("Don't send now");
            $options[SIMPLENEWS_COMMAND_SEND_TEST] = t('Send one test newsletter to the test address');
            $options[SIMPLENEWS_COMMAND_SEND_NOW] = t('Send newsletter');
            $form['simplenews']['send'] = array(
              '#type' => 'radios',
              '#title' => t('Send action'),
              '#default_value' => isset($simplenews_values['send']) ? $simplenews_values['send'] : variable_get('simplenews_send', SIMPLENEWS_COMMAND_SEND_NONE),
              '#options' => $options,
              '#attributes' => array(
                'class' => 'simplenews-command-send',
              ),
            );
          }
          else {
            $options[SIMPLENEWS_COMMAND_SEND_NONE] = t("Don't send now");
            $options[SIMPLENEWS_COMMAND_SEND_TEST] = t('Send one test newsletter to the test address');
            $form['simplenews']['send'] = array(
              '#type' => 'radios',
              '#title' => t('Sending'),
              '#default_value' => isset($simplenews_values['send']) ? $simplenews_values['send'] : SIMPLENEWS_COMMAND_SEND_NONE,
              '#options' => $options,
              '#description' => t('You have no privileges to send this newsletter'),
              '#attributes' => array(
                'class' => 'simplenews-command-send',
              ),
            );
          }
          $address_default = variable_get('site_mail', ini_get('sendmail_from'));
          if (variable_get('simplenews_test_address_override', 0)) {
            $form['simplenews']['test_address'] = array(
              '#type' => 'textfield',
              '#title' => t('Test email addresses'),
              '#description' => t('Supply a comma-separated list of email addresses to be used as test addresses.'),
              '#default_value' => isset($simplenews_values['test_address']) ? $simplenews_values['test_address'] : variable_get('simplenews_test_address', $address_default),
              '#size' => 60,
              '#maxlength' => 128,
            );
          }
          else {
            $form['simplenews']['test_address'] = array(
              '#type' => 'hidden',
              '#value' => variable_get('simplenews_test_address', $address_default),
            );
          }
          $form['simplenews']['advanced'] = array(
            '#type' => 'fieldset',
            '#title' => t('Email options'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
          );

          // Hide format selection if there is nothing to choose.
          // The default format is plain text.
          $format_options = _simplenews_format_options();
          if (count($format_options) > 1) {
            $form['simplenews']['advanced']['s_format'] = array(
              '#type' => 'select',
              '#title' => t('Email format'),
              '#default_value' => isset($simplenews_values['s_format']) ? $simplenews_values['s_format'] : variable_get('simplenews_format', 'plain'),
              '#options' => _simplenews_format_options(),
            );
          }
          else {
            $form['simplenews']['advanced']['s_format'] = array(
              '#type' => 'hidden',
              '#value' => key($format_options),
            );
          }
          $form['simplenews']['advanced']['priority'] = array(
            '#type' => 'select',
            '#title' => t('Email priority'),
            '#default_value' => isset($simplenews_values['priority']) ? $simplenews_values['priority'] : variable_get('simplenews_priority', SIMPLENEWS_PRIORITY_NONE),
            '#options' => array(
              SIMPLENEWS_PRIORITY_NONE => t('none'),
              SIMPLENEWS_PRIORITY_HIGHEST => t('highest'),
              SIMPLENEWS_PRIORITY_HIGH => t('high'),
              SIMPLENEWS_PRIORITY_NORMAL => t('normal'),
              SIMPLENEWS_PRIORITY_LOW => t('low'),
              SIMPLENEWS_PRIORITY_LOWEST => t('lowest'),
            ),
          );
          $form['simplenews']['advanced']['receipt'] = array(
            '#type' => 'checkbox',
            '#title' => t('Request receipt'),
            '#return_value' => 1,
            '#default_value' => isset($simplenews_values['receipt']) ? $simplenews_values['receipt'] : variable_get('simplenews_receipt', 0),
          );
        }
        else {
          $form['simplenews']['none'] = array(
            '#type' => 'checkbox',
            '#title' => t('This newsletter has been sent'),
            '#return_value' => 0,
            '#attributes' => array(
              'checked' => 'checked',
              'disabled' => 'disabled',
            ),
          );
          $form['simplenews']['advanced']['s_format'] = array(
            '#type' => 'hidden',
            '#value' => $simplenews_values['s_format'],
          );
          $form['simplenews']['advanced']['priority'] = array(
            '#type' => 'hidden',
            '#value' => $simplenews_values['priority'],
          );
          $form['simplenews']['advanced']['receipt'] = array(
            '#type' => 'hidden',
            '#value' => $simplenews_values['receipt'],
          );
        }
      }
      $form['simplenews']['s_status'] = array(
        '#type' => 'hidden',
        '#value' => isset($simplenews_values['s_status']) ? $simplenews_values['s_status'] : SIMPLENEWS_STATUS_SEND_NOT,
      );
    }
  }
}