Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 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) { | |
| 31 | 10 | $this->campaign = $campaign; |
| 32 | } | |
| 33 | ||
| 34 | /** | |
| 35 | * {@inheritdoc} | |
| 36 | */ | |
| 37 | public static function create(ContainerInterface $container): CampaignForm { | |
| 38 | 10 | return new static( |
| 39 | 10 | $container->get('visitors.campaign'), |
| 40 | 10 | ); |
| 41 | } | |
| 42 | ||
| 43 | /** | |
| 44 | * {@inheritdoc} | |
| 45 | */ | |
| 46 | public function getFormId(): string { | |
| 47 | 1 | return 'visitors_campaign_form'; |
| 48 | } | |
| 49 | ||
| 50 | /** | |
| 51 | * {@inheritdoc} | |
| 52 | */ | |
| 53 | public function buildForm(array $form, FormStateInterface $form_state): array { | |
| 54 | 2 | $form['page_url'] = [ |
| 55 | 2 | '#type' => 'textfield', |
| 56 | 2 | '#title' => $this->t('Page URL'), |
| 57 | 2 | '#description' => $this->t("The URL of the page that this campaign goes to, for example 'http://example.org/offer.html'."), |
| 58 | 2 | '#required' => TRUE, |
| 59 | 2 | ]; |
| 60 | ||
| 61 | 2 | $form['campaign'] = [ |
| 62 | 2 | '#type' => 'textfield', |
| 63 | 2 | '#title' => $this->t('Campaign name'), |
| 64 | 2 | '#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'."), |
| 65 | 2 | '#required' => TRUE, |
| 66 | 2 | ]; |
| 67 | ||
| 68 | 2 | $form['keyword'] = [ |
| 69 | 2 | '#type' => 'textfield', |
| 70 | 2 | '#title' => $this->t('Campaign keyword'), |
| 71 | 2 | '#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.'), |
| 72 | 2 | ]; |
| 73 | ||
| 74 | 2 | $form['source'] = [ |
| 75 | 2 | '#type' => 'textfield', |
| 76 | 2 | '#title' => $this->t('Campaign source'), |
| 77 | 2 | '#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."), |
| 78 | 2 | ]; |
| 79 | ||
| 80 | 2 | $form['medium'] = [ |
| 81 | 2 | '#type' => 'textfield', |
| 82 | 2 | '#title' => $this->t('Campaign medium'), |
| 83 | 2 | '#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."), |
| 84 | 2 | ]; |
| 85 | ||
| 86 | 2 | $form['content'] = [ |
| 87 | 2 | '#type' => 'textfield', |
| 88 | 2 | '#title' => $this->t('Campaign content'), |
| 89 | 2 | '#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.'), |
| 90 | 2 | ]; |
| 91 | ||
| 92 | 2 | $form['campaign_id'] = [ |
| 93 | 2 | '#type' => 'textfield', |
| 94 | 2 | '#title' => $this->t('Campaign ID'), |
| 95 | 2 | '#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.'), |
| 96 | 2 | '#maxlength' => 100, |
| 97 | 2 | ]; |
| 98 | ||
| 99 | 2 | $form['group'] = [ |
| 100 | 2 | '#type' => 'textfield', |
| 101 | 2 | '#title' => $this->t('Campaign group'), |
| 102 | 2 | '#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.'), |
| 103 | 2 | ]; |
| 104 | ||
| 105 | 2 | $form['placement'] = [ |
| 106 | 2 | '#type' => 'textfield', |
| 107 | 2 | '#title' => $this->t('Campaign placement'), |
| 108 | 2 | '#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.'), |
| 109 | 2 | ]; |
| 110 | ||
| 111 | 2 | $generated_url = $form_state->get('generated_url'); |
| 112 | 2 | if (is_string($generated_url) && $generated_url !== '') { |
| 113 | 1 | $form['generated_url'] = [ |
| 114 | 1 | '#type' => 'textfield', |
| 115 | 1 | '#title' => $this->t('Generated URL'), |
| 116 | 1 | '#description' => $this->t('Generated URL that you can copy and paste in your Campaigns, Email newsletter, Facebook Ads or tweets.'), |
| 117 | 1 | '#default_value' => $generated_url, |
| 118 | 1 | '#attributes' => [ |
| 119 | 1 | 'readonly' => 'readonly', |
| 120 | 1 | ], |
| 121 | 1 | ]; |
| 122 | } | |
| 123 | ||
| 124 | 2 | $form['actions'] = [ |
| 125 | 2 | '#type' => 'actions', |
| 126 | 2 | ]; |
| 127 | 2 | $form['actions']['submit'] = [ |
| 128 | 2 | '#type' => 'submit', |
| 129 | 2 | '#button_type' => 'primary', |
| 130 | 2 | '#value' => $this->t('Generate URL'), |
| 131 | 2 | ]; |
| 132 | 2 | $form['actions']['clear'] = [ |
| 133 | 2 | '#type' => 'submit', |
| 134 | 2 | '#value' => $this->t('Clear'), |
| 135 | 2 | '#limit_validation_errors' => [], |
| 136 | 2 | '#submit' => ['::clearForm'], |
| 137 | 2 | ]; |
| 138 | ||
| 139 | 2 | return $form; |
| 140 | } | |
| 141 | ||
| 142 | /** | |
| 143 | * {@inheritdoc} | |
| 144 | */ | |
| 145 | public function validateForm(array &$form, FormStateInterface $form_state): void { | |
| 146 | 4 | $name = trim((string) $form_state->getValue('campaign')); |
| 147 | 4 | if ($name === '') { |
| 148 | 1 | $form_state->setErrorByName('campaign', $this->t('Campaign name is required.')); |
| 149 | } | |
| 150 | 4 | $form_state->setValue('campaign', $name); |
| 151 | ||
| 152 | 4 | $page_url = trim((string) $form_state->getValue('page_url')); |
| 153 | 4 | if (!str_starts_with($page_url, 'http')) { |
| 154 | 2 | $page_url = 'https://' . $page_url; |
| 155 | } | |
| 156 | 4 | if (filter_var($page_url, FILTER_VALIDATE_URL) === FALSE) { |
| 157 | 1 | $form_state->setErrorByName('page_url', $this->t('Enter a valid page URL.')); |
| 158 | } | |
| 159 | else { | |
| 160 | 3 | $form_state->setValue('page_url', $page_url); |
| 161 | } | |
| 162 | ||
| 163 | 4 | $campaign_id = trim((string) $form_state->getValue('campaign_id')); |
| 164 | 4 | if (mb_strlen($campaign_id) > 100) { |
| 165 | 1 | $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 { | |
| 173 | 1 | $url = $this->campaign->build( |
| 174 | 1 | (string) $form_state->getValue('page_url'), |
| 175 | 1 | [ |
| 176 | 1 | 'campaign' => $form_state->getValue('campaign'), |
| 177 | 1 | 'keyword' => $form_state->getValue('keyword'), |
| 178 | 1 | 'source' => $form_state->getValue('source'), |
| 179 | 1 | 'medium' => $form_state->getValue('medium'), |
| 180 | 1 | 'content' => $form_state->getValue('content'), |
| 181 | 1 | 'campaign_id' => $form_state->getValue('campaign_id'), |
| 182 | 1 | 'group' => $form_state->getValue('group'), |
| 183 | 1 | 'placement' => $form_state->getValue('placement'), |
| 184 | 1 | ], |
| 185 | 1 | ); |
| 186 | 1 | $form_state->set('generated_url', $url); |
| 187 | 1 | $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 { | |
| 199 | 1 | $form_state->setRebuild(FALSE); |
| 200 | 1 | $form_state->setRedirect('visitors.campaign'); |
| 201 | } | |
| 202 | ||
| 203 | } |