You are here

public function SubscribeForm::buildForm in SendinBlue 8

Same name and namespace in other branches
  1. 8.2 src/Form/SubscribeForm.php \Drupal\sendinblue\Form\SubscribeForm::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.

int $mcsId: The ID of signupForm.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/SubscribeForm.php, line 94

Class

SubscribeForm
Subscribe form to signup SendinBlue newsletter.

Namespace

Drupal\sendinblue\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $mcsId = NULL) {
  if ($mcsId) {
    $this->signupIp = $mcsId;
  }
  $signup = $this->entityTypeManager
    ->getStorage(SendinblueManager::SENDINBLUE_SIGNUP_ENTITY)
    ->load($this->signupIp);
  $settings = !$signup->settings
    ->first() ? [] : $signup->settings
    ->first()
    ->getValue();
  $form['#attributes'] = [
    'class' => [
      'sendinblue-signup-subscribe-form',
    ],
  ];
  $form['description'] = [
    '#markup' => $settings['description']['value'],
  ];
  $form['fields'] = [
    '#prefix' => '<div id="sendinblue-newsletter-' . $settings['subscription']['settings']['list'] . '-mergefields" class="sendinblue-newsletter-mergefields">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  ];
  if (isset($settings['fields']['mergefields'])) {
    $merge_fields = $settings['fields']['mergefields'];
    $attributes = $this->sendinblueManager
      ->getAttributeLists();
    if (is_array($merge_fields)) {
      foreach ($merge_fields as $key => $value) {
        if ($key === 'EMAIL') {
          $form['fields'][$key] = [
            '#type' => 'textfield',
            '#title' => $value['label'],
            '#attributes' => [
              'style' => 'width:100%;box-sizing:border-box;',
            ],
            '#required' => TRUE,
          ];
        }
        else {
          if (isset($value['check']) && $value['required']) {
            $enumerations = [];
            $type = '';
            foreach ($attributes as $attribute) {
              if ($attribute
                ->getName() === $key) {
                $type = $attribute
                  ->getType();
                if ($type === 'category') {
                  $enumerations = $attribute
                    ->getEnumeration();
                }
                break;
              }
            }
            if ($type !== 'category') {
              $form['fields'][$key] = [
                '#type' => 'textfield',
                '#title' => $value['label'],
                '#attributes' => [
                  'style' => 'width:100%;box-sizing:border-box;',
                ],
                '#required' => isset($value['required']) && $value['required'] ? TRUE : FALSE,
              ];
            }
            else {
              $options = [];
              foreach ($enumerations as $enumeration) {
                $options[$enumeration
                  ->getValue()] = $enumeration
                  ->getLabel();
              }
              $form['fields'][$key] = [
                '#type' => 'select',
                '#title' => $value['label'],
                '#options' => $options,
                '#attributes' => [
                  'style' => 'width:100%;box-sizing:border-box;',
                ],
                '#required' => isset($value['required']) ? TRUE : FALSE,
              ];
            }
          }
        }
      }
    }
  }
  $form['submit'] = [
    '#type' => 'submit',
    '#weight' => 10,
    '#value' => $settings['fields']['submit_button'],
  ];
  return $form;
}