You are here

public function NodeTabForm::buildForm in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Form/NodeTabForm.php \Drupal\simplenews\Form\NodeTabForm::buildForm()
  2. 3.x src/Form/NodeTabForm.php \Drupal\simplenews\Form\NodeTabForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/NodeTabForm.php, line 88

Class

NodeTabForm
Configure simplenews subscriptions of a user.

Namespace

Drupal\simplenews\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) {
  $config = $this
    ->config('simplenews.settings');
  $status = $node->simplenews_issue->status;
  $summary = $this->spoolStorage
    ->issueSummary($node);
  $form['#title'] = $this
    ->t('<em>Newsletter issue</em> @title', array(
    '@title' => $node
      ->getTitle(),
  ));

  // We will need the node.
  $form_state
    ->set('node', $node);

  // Show newsletter sending options if newsletter has not been send yet.
  // If send a notification is shown.
  if ($status == SIMPLENEWS_STATUS_SEND_NOT) {
    $form['test'] = array(
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => t('Test'),
    );
    $form['test']['test_address'] = array(
      '#type' => 'textfield',
      '#title' => t('Test email addresses'),
      '#description' => t('A comma-separated list of email addresses to be used as test addresses.'),
      '#default_value' => $this->currentUser
        ->getEmail(),
      '#size' => 60,
      '#maxlength' => 128,
    );
    $form['test']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Send test newsletter issue'),
      '#name' => 'send_test',
      '#submit' => array(
        '::submitTestMail',
      ),
      '#validate' => array(
        '::validateTestAddress',
      ),
    );
    $form['send'] = array(
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => t('Send'),
    );
    $default_handler = isset($form_state
      ->getValue('simplenews')['recipient_handler']) ? $form_state
      ->getValue('simplenews')['recipient_handler'] : $node->simplenews_issue->handler;
    $recipient_handler_manager = $this->recipientHandlerManager;
    $options = $recipient_handler_manager
      ->getOptions();
    $form['send']['recipient_handler'] = array(
      '#type' => 'select',
      '#title' => t('Recipients'),
      '#description' => t('Please select to configure who to send the email to.'),
      '#options' => $options,
      '#default_value' => $default_handler,
      '#access' => count($options) > 1,
      '#ajax' => array(
        'callback' => '::ajaxUpdateRecipientHandlerSettings',
        'wrapper' => 'recipient-handler-settings',
        'method' => 'replace',
        'effect' => 'fade',
      ),
    );

    // Get the handler class.
    $handler_definitions = $recipient_handler_manager
      ->getDefinitions();
    $handler = $handler_definitions[$default_handler];
    $class = $handler['class'];
    $settings = $node->simplenews_issue->handler_settings;
    if (method_exists($class, 'settingsForm')) {
      $element = array(
        '#parents' => array(
          'simplenews',
          'recipient_handler_settings',
        ),
        '#prefix' => '<div id="recipient-handler-settings">',
        '#suffix' => '</div>',
      );
      $form['send']['recipient_handler_settings'] = $class::settingsForm($element, $settings);
    }
    else {
      $form['send']['recipient_handler']['#suffix'] = '<div id="recipient-handler-settings"></div>';
    }

    // Add some text to describe the send situation.
    $form['send']['count'] = array(
      '#type' => 'item',
      '#markup' => t('Send newsletter issue to @count subscribers.', array(
        '@count' => $summary['count'],
      )),
    );
    if (!$config
      ->get('mail.use_cron')) {
      $send_text = t('Mails will be sent immediately.');
    }
    else {
      $send_text = t('Mails will be sent when cron runs.');
    }
    $form['send']['method'] = array(
      '#type' => 'item',
      '#markup' => $send_text,
    );
    if ($node
      ->isPublished()) {
      $form['send']['send_now'] = array(
        '#type' => 'submit',
        '#button_type' => 'primary',
        '#value' => t('Send now'),
        '#submit' => array(
          '::submitForm',
          '::submitSendNow',
        ),
      );
    }
    else {
      $form['send']['send_on_publish'] = array(
        '#type' => 'submit',
        '#button_type' => 'primary',
        '#value' => t('Send on publish'),
        '#submit' => array(
          '::submitForm',
          '::submitSendLater',
        ),
      );
    }
  }
  else {
    $form['status'] = array(
      '#type' => 'item',
      '#title' => $summary['description'],
    );
    if ($status != SIMPLENEWS_STATUS_SEND_READY) {
      $form['actions'] = array(
        '#type' => 'actions',
      );
      $form['actions']['stop'] = array(
        '#type' => 'submit',
        '#submit' => array(
          '::submitStop',
        ),
        '#value' => t('Stop sending'),
      );
    }
  }
  return $form;
}