Coverage index

src/Form/CampaignForm.php

Lines: 100.00%

LineHitsSource
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\visitors\Form;
6
7
use Drupal\Core\Form\FormBase;
8
use Drupal\Core\Form\FormStateInterface;
9
use Drupal\visitors\VisitorsCampaignInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
/**
13
 * Form that builds a campaign tracking URL.
14
 */
15
final class CampaignForm extends FormBase {
16
17
  /**
18
   * The campaign service.
19
   *
20
   * @var \Drupal\visitors\VisitorsCampaignInterface
21
   */
22
  protected VisitorsCampaignInterface $campaign;
23
24
  /**
25
   * Constructs a campaign URL builder form.
26
   *
27
   * @param \Drupal\visitors\VisitorsCampaignInterface $campaign
28
   *   The campaign service.
29
   */
30
  public function __construct(VisitorsCampaignInterface $campaign) {
3110
    $this->campaign = $campaign;
32
  }
33
34
  /**
35
   * {@inheritdoc}
36
   */
37
  public static function create(ContainerInterface $container): CampaignForm {
3810
    return new static(
3910
      $container->get('visitors.campaign'),
4010
    );
41
  }
42
43
  /**
44
   * {@inheritdoc}
45
   */
46
  public function getFormId(): string {
471
    return 'visitors_campaign_form';
48
  }
49
50
  /**
51
   * {@inheritdoc}
52
   */
53
  public function buildForm(array $form, FormStateInterface $form_state): array {
542
    $form['page_url'] = [
552
      '#type' => 'textfield',
562
      '#title' => $this->t('Page URL'),
572
      '#description' => $this->t("The URL of the page that this campaign goes to, for example 'http://example.org/offer.html'."),
582
      '#required' => TRUE,
592
    ];
60
612
    $form['campaign'] = [
622
      '#type' => 'textfield',
632
      '#title' => $this->t('Campaign name'),
642
      '#description' => $this->t("Choose a name that describes what the campaign is created for and that distinguishes your campaign from your other campaigns. For example 'Email-SummerDeals' or 'PaidAds-SummerDeals'."),
652
      '#required' => TRUE,
662
    ];
67
682
    $form['keyword'] = [
692
      '#type' => 'textfield',
702
      '#title' => $this->t('Campaign keyword'),
712
      '#description' => $this->t('If you have multiple campaigns with the same name, you can distinguish these campaigns by specifying a keyword or a sub-category.'),
722
    ];
73
742
    $form['source'] = [
752
      '#type' => 'textfield',
762
      '#title' => $this->t('Campaign source'),
772
      '#description' => $this->t("Used to track the source of the campaign such as 'newsletter' for your email marketing, 'affiliate', or the name of the website displaying your ads."),
782
    ];
79
802
    $form['medium'] = [
812
      '#type' => 'textfield',
822
      '#title' => $this->t('Campaign medium'),
832
      '#description' => $this->t("Used to describe the marketing activity, for example 'PPC' for a pay-per-click campaign, or 'SEM' for a paid search ads, or 'review' to track a product review on an affiliate site."),
842
    ];
85
862
    $form['content'] = [
872
      '#type' => 'textfield',
882
      '#title' => $this->t('Campaign content'),
892
      '#description' => $this->t('This parameter is often used when you are testing multiple ads, and would include the name of each ad to see which was most effective for driving traffic.'),
902
    ];
91
922
    $form['campaign_id'] = [
932
      '#type' => 'textfield',
942
      '#title' => $this->t('Campaign ID'),
952
      '#description' => $this->t('This parameter is used to track a Campaign ID: a unique identifier for the campaign consisting of at most 100 numbers, letters or symbols.'),
962
      '#maxlength' => 100,
972
    ];
98
992
    $form['group'] = [
1002
      '#type' => 'textfield',
1012
      '#title' => $this->t('Campaign group'),
1022
      '#description' => $this->t('This parameter is used to track a Campaign Group to identify a group or audience. Campaign groups are helpful for tracking the overall performance of multiple campaigns with similar goals.'),
1032
    ];
104
1052
    $form['placement'] = [
1062
      '#type' => 'textfield',
1072
      '#title' => $this->t('Campaign placement'),
1082
      '#description' => $this->t('Used to describe the advertising placement or position, for example an entire website, specific pages from a website, an individual ad unit positioned on a single page, a video, a mobile phone app, and more.'),
1092
    ];
110
1112
    $generated_url = $form_state->get('generated_url');
1122
    if (is_string($generated_url) && $generated_url !== '') {
1131
      $form['generated_url'] = [
1141
        '#type' => 'textfield',
1151
        '#title' => $this->t('Generated URL'),
1161
        '#description' => $this->t('Generated URL that you can copy and paste in your Campaigns, Email newsletter, Facebook Ads or tweets.'),
1171
        '#default_value' => $generated_url,
1181
        '#attributes' => [
1191
          'readonly' => 'readonly',
1201
        ],
1211
      ];
122
    }
123
1242
    $form['actions'] = [
1252
      '#type' => 'actions',
1262
    ];
1272
    $form['actions']['submit'] = [
1282
      '#type' => 'submit',
1292
      '#button_type' => 'primary',
1302
      '#value' => $this->t('Generate URL'),
1312
    ];
1322
    $form['actions']['clear'] = [
1332
      '#type' => 'submit',
1342
      '#value' => $this->t('Clear'),
1352
      '#limit_validation_errors' => [],
1362
      '#submit' => ['::clearForm'],
1372
    ];
138
1392
    return $form;
140
  }
141
142
  /**
143
   * {@inheritdoc}
144
   */
145
  public function validateForm(array &$form, FormStateInterface $form_state): void {
1464
    $name = trim((string) $form_state->getValue('campaign'));
1474
    if ($name === '') {
1481
      $form_state->setErrorByName('campaign', $this->t('Campaign name is required.'));
149
    }
1504
    $form_state->setValue('campaign', $name);
151
1524
    $page_url = trim((string) $form_state->getValue('page_url'));
1534
    if (!str_starts_with($page_url, 'http')) {
1542
      $page_url = 'https://' . $page_url;
155
    }
1564
    if (filter_var($page_url, FILTER_VALIDATE_URL) === FALSE) {
1571
      $form_state->setErrorByName('page_url', $this->t('Enter a valid page URL.'));
158
    }
159
    else {
1603
      $form_state->setValue('page_url', $page_url);
161
    }
162
1634
    $campaign_id = trim((string) $form_state->getValue('campaign_id'));
1644
    if (mb_strlen($campaign_id) > 100) {
1651
      $form_state->setErrorByName('campaign_id', $this->t('Campaign ID must be at most 100 characters.'));
166
    }
167
  }
168
169
  /**
170
   * {@inheritdoc}
171
   */
172
  public function submitForm(array &$form, FormStateInterface $form_state): void {
1731
    $url = $this->campaign->build(
1741
      (string) $form_state->getValue('page_url'),
1751
      [
1761
        'campaign' => $form_state->getValue('campaign'),
1771
        'keyword' => $form_state->getValue('keyword'),
1781
        'source' => $form_state->getValue('source'),
1791
        'medium' => $form_state->getValue('medium'),
1801
        'content' => $form_state->getValue('content'),
1811
        'campaign_id' => $form_state->getValue('campaign_id'),
1821
        'group' => $form_state->getValue('group'),
1831
        'placement' => $form_state->getValue('placement'),
1841
      ],
1851
    );
1861
    $form_state->set('generated_url', $url);
1871
    $form_state->setRebuild(TRUE);
188
  }
189
190
  /**
191
   * Clears the form and returns to an empty builder.
192
   *
193
   * @param array $form
194
   *   The form.
195
   * @param \Drupal\Core\Form\FormStateInterface $form_state
196
   *   The form state.
197
   */
198
  public function clearForm(array &$form, FormStateInterface $form_state): void {
1991
    $form_state->setRebuild(FALSE);
2001
    $form_state->setRedirect('visitors.campaign');
201
  }
202
203
}