You are here

public function SignupForm::buildForm in SendinBlue 8

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

File

src/Form/SignupForm.php, line 72

Class

SignupForm
Form controller for the content_entity_example entity edit forms.

Namespace

Drupal\sendinblue\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  /* @var $entity Signup */
  $form = parent::buildForm($form, $form_state);
  $signup = $this->entity;
  $settings = !$signup->settings
    ->first() ? [] : $signup->settings
    ->first()
    ->getValue();
  $form_state
    ->set('signup', $signup);

  // Form Field for organization.
  $form['#attributes'] = [
    'class' => [
      'container-fluid',
    ],
    'id' => 'wrap',
  ];
  $form['wrap_left'] = [
    '#prefix' => '<div id="wrap-left" class="col-md-9">',
    '#suffix' => '</div>',
  ];
  $form['wrap_left']['form'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Form'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];
  $form['wrap_left']['form']['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#description' => $this
      ->t('The title for this signup form.'),
    '#size' => 35,
    '#maxlength' => 32,
    '#default_value' => $signup->title->value,
    '#required' => TRUE,
    '#attributes' => [
      'style' => 'width:200px;',
    ],
  ];

  // Machine-readable list name.
  $status = isset($signup->status->value) && $signup->mcsId->value && ($signup->status->value || $signup->status->value);
  $form['wrap_left']['form']['name'] = [
    '#type' => 'machine_name',
    '#default_value' => $signup->name->value,
    '#maxlength' => 32,
    '#disabled' => $status,
    '#description' => $this
      ->t('A unique machine-readable name for this list. It must only contain lowercase letters, numbers, and underscores.'),
    '#attributes' => [
      'style' => 'width:200px;',
    ],
  ];
  $form['wrap_left']['form']['description'] = [
    '#type' => 'text_format',
    '#title' => $this
      ->t('Description'),
    '#default_value' => isset($settings['description']['value']) ? $settings['description']['value'] : '',
    '#rows' => 2,
    '#maxlength' => 500,
    '#description' => $this
      ->t('This description will be shown on the signup form below the title. (500 characters or less)'),
  ];
  if (isset($settings['description']['format'])) {
    $form['wrap_left']['form']['description']['#format'] = $settings['description']['format'];
  }
  $mode_defaults = [
    SendinblueManager::SENDINBLUE_SIGNUP_BLOCK => [
      SendinblueManager::SENDINBLUE_SIGNUP_BLOCK,
    ],
    SendinblueManager::SENDINBLUE_SIGNUP_PAGE => [
      SendinblueManager::SENDINBLUE_SIGNUP_PAGE,
    ],
    SendinblueManager::SENDINBLUE_SIGNUP_BOTH => [
      SendinblueManager::SENDINBLUE_SIGNUP_BLOCK,
      SendinblueManager::SENDINBLUE_SIGNUP_PAGE,
    ],
  ];
  $form['wrap_left']['form']['mode'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Display Mode') . $signup->mode->value,
    '#required' => TRUE,
    '#options' => [
      SendinblueManager::SENDINBLUE_SIGNUP_BLOCK => $this
        ->t('Block'),
      SendinblueManager::SENDINBLUE_SIGNUP_PAGE => $this
        ->t('Page'),
    ],
    '#default_value' => !empty($signup->mode->value) ? $mode_defaults[$signup->mode->value] : [],
  ];
  $form['wrap_left']['form']['path'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Page URL'),
    '#description' => $this
      ->t('Path to the signup page. ie "newsletter/signup".'),
    '#default_value' => isset($settings['path']) ? $settings['path'] : NULL,
    '#states' => [
      // Hide unless needed.
      'visible' => [
        ':input[name="mode[' . SendinblueManager::SENDINBLUE_SIGNUP_PAGE . ']"]' => [
          'checked' => TRUE,
        ],
      ],
      'required' => [
        ':input[name="mode[' . SendinblueManager::SENDINBLUE_SIGNUP_PAGE . ']"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    '#attributes' => [
      'style' => 'width:400px;',
    ],
  ];

  // Fields for organization.
  $form['wrap_left']['fields'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Fields'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];
  $attributes = $this->sendinblueManager
    ->getAttributeLists();
  $form['wrap_left']['fields']['mergefields']['EMAIL']['check'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Email'),
    '#default_value' => TRUE,
    '#disabled' => TRUE,
  ];
  $form['wrap_left']['fields']['mergefields']['EMAIL']['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#tree' => TRUE,
    '#default_value' => isset($settings['fields']['mergefields']['EMAIL']['label']) ? $settings['fields']['mergefields']['EMAIL']['label'] : $this
      ->t('Email'),
    '#required' => TRUE,
    '#attributes' => [
      'style' => 'width:200px;',
    ],
    '#prefix' => '<div class="sendinblue_sub_field">',
    '#suffix' => '</div>',
  ];
  $form['wrap_left']['fields']['mergefields']['EMAIL']['required'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Required?'),
    '#tree' => TRUE,
    '#default_value' => TRUE,
    '#disabled' => TRUE,
    '#prefix' => '<div class="sendinblue_sub_field">',
    '#suffix' => '</div>',
  ];
  foreach ($attributes as $attribute) {
    $settings_attribute = $settings['fields']['mergefields'][$attribute
      ->getName()] ?? NULL;
    $form['wrap_left']['fields']['mergefields'][$attribute
      ->getName()]['check'] = [
      '#type' => 'checkbox',
      '#title' => $attribute
        ->getName(),
      '#default_value' => isset($settings_attribute['check']) ? $settings_attribute['check'] : FALSE,
    ];
    $form['wrap_left']['fields']['mergefields'][$attribute
      ->getName()]['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#tree' => TRUE,
      '#default_value' => isset($settings_attribute['label']) ? $settings_attribute['label'] : $attribute
        ->getName(),
      '#required' => TRUE,
      '#states' => [
        // Hide unless needed.
        'visible' => [
          ':input[name="fields[mergefields][' . $attribute
            ->getName() . '][check]"]' => [
            'checked' => TRUE,
          ],
        ],
        'required' => [
          ':input[name="mode[' . $attribute
            ->getName() . '][check]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#attributes' => [
        'style' => 'width:200px;',
      ],
      '#prefix' => '<div class="sendinblue_sub_field">',
      '#suffix' => '</div>',
    ];
    $form['wrap_left']['fields']['mergefields'][$attribute
      ->getName()]['required'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Required?'),
      '#tree' => TRUE,
      '#default_value' => isset($settings['fields']['mergefields'][$attribute
        ->getName()]['required']) ? $settings_attribute['required'] : FALSE,
      '#states' => [
        // Hide unless needed.
        'visible' => [
          ':input[name="fields[mergefields][' . $attribute
            ->getName() . '][check]"]' => [
            'checked' => TRUE,
          ],
        ],
        'required' => [
          ':input[name="mode[' . $attribute
            ->getName() . '][check]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#prefix' => '<div class="sendinblue_sub_field">',
      '#suffix' => '</div>',
    ];
  }
  $form['wrap_left']['fields']['submit_button'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Submit Button Label'),
    '#required' => 'TRUE',
    '#default_value' => isset($settings['fields']['submit_button']) ? $settings['fields']['submit_button'] : $this
      ->t('Submit'),
    '#attributes' => [
      'style' => 'width:200px;',
    ],
    '#tree' => TRUE,
  ];

  // Fields Field for organization.
  $form['wrap_left']['subscription'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Subscription'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];
  $form['wrap_left']['subscription']['settings'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Settings'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];
  $sendinblue_lists = $this->sendinblueManager
    ->getLists();
  $options = [];
  foreach ($sendinblue_lists as $mc_list) {
    $options[$mc_list['id']] = $mc_list['name'];
  }
  $form['wrap_left']['subscription']['settings']['list'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('List where subscribers are saved'),
    '#options' => $options,
    '#default_value' => isset($settings['subscription']['settings']['list']) ? $settings['subscription']['settings']['list'] : '',
    '#description' => $this
      ->t('Select the list where you want to add your new subscribers'),
    '#attributes' => [
      'style' => 'width:200px;',
    ],
  ];
  $form['wrap_left']['subscription']['settings']['redirect_url'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('URL redirection'),
    '#required' => FALSE,
    '#default_value' => isset($settings['subscription']['settings']['redirect_url']) ? $settings['subscription']['settings']['redirect_url'] : '',
    '#description' => $this
      ->t('Redirect to this URL after subscription'),
    '#attributes' => [
      'style' => 'width:400px;',
    ],
  ];
  $form['wrap_left']['subscription']['settings']['email_confirmation'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Email confirmation'),
    '#required' => FALSE,
    '#default_value' => isset($settings['subscription']['settings']['email_confirmation']) ? $settings['subscription']['settings']['email_confirmation'] : '',
    '#description' => $this
      ->t('You can choose to send a confirmation email. You will be able to set up the template & sender that will be sent to your new subscribers'),
  ];
  $sendinblue_templates = $this->sendinblueManager
    ->getTemplateList();
  $options = [];
  if ($sendinblue_templates) {
    foreach ($sendinblue_templates
      ->getTemplates() as $mc_template) {
      $options[$mc_template
        ->getId()] = $mc_template
        ->getName();
    }
  }
  $form['wrap_left']['subscription']['settings']['template'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Select Template'),
    '#options' => $options,
    '#default_value' => isset($settings['subscription']['settings']['template']) ? $settings['subscription']['settings']['template'] : '-1',
    '#description' => $this
      ->t('Select the template that will be sent to your new subscribers. You can create new template at @SendinBlue.', [
      '@SendinBlue' => Link::fromTextAndUrl($this
        ->t('SendinBlue'), Url::fromUri('https://my.sendinblue.com/camp/listing/?utm_source=drupal_plugin&utm_medium=plugin&utm_campaign=module_link#temp_active_m'))
        ->toString(),
    ]),
    '#states' => [
      // Hide unless needed.
      'visible' => [
        ':input[name="subscription[settings][email_confirmation]"]' => [
          'checked' => TRUE,
        ],
      ],
      'required' => [
        ':input[name="subscription[settings][email_confirmation]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    '#attributes' => [
      'style' => 'width:200px;',
    ],
  ];
  $form['wrap_left']['subscription']['messages'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Messages'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];
  $form['wrap_left']['subscription']['messages']['success'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Success message'),
    '#required' => FALSE,
    '#default_value' => isset($settings['subscription']['messages']['success']) ? $settings['subscription']['messages']['success'] : $this
      ->t('Thank you, you have successfully registered!'),
    '#description' => $this
      ->t('Set up the success message that will appear when one of your visitors successfully signs up'),
    '#attributes' => [
      'style' => 'width:400px;',
    ],
  ];
  $form['wrap_left']['subscription']['messages']['general'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('General error message'),
    '#required' => FALSE,
    '#default_value' => isset($settings['subscription']['messages']['general']) ? $settings['subscription']['messages']['general'] : $this
      ->t('Something wrong occured'),
    '#description' => $this
      ->t('Set up the message that will appear when an error occurs during the subscription process'),
    '#attributes' => [
      'style' => 'width:400px;',
    ],
  ];
  $form['wrap_left']['subscription']['messages']['existing'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Existing subscribers'),
    '#required' => FALSE,
    '#default_value' => isset($settings['subscription']['messages']['existing']) ? $settings['subscription']['messages']['existing'] : 'You have already registered',
    '#description' => $this
      ->t('Set up the message that will appear when a subscriber is already in your database'),
    '#attributes' => [
      'style' => 'width:400px;',
    ],
  ];
  $form['wrap_left']['subscription']['messages']['invalid'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Existing subscribers'),
    '#required' => FALSE,
    '#default_value' => isset($settings['subscription']['messages']['invalid']) ? $settings['subscription']['messages']['invalid'] : 'Your email address is invalid',
    '#description' => $this
      ->t('Set up the message that will appear when the email address used to sign up is not valid'),
    '#attributes' => [
      'style' => 'width:400px;',
    ],
  ];

  // $markup = SendinblueManager::generateSidebar();
  $form['wrap_right'] = [
    '#type' => 'markup',
    '#prefix' => '<div id="wrap-right-side" class="col-md-3">',
    // '#markup' => $markup,.
    '#suffix' => '</div><div class="clearfix"></div>',
    '#tree' => TRUE,
  ];
  return $form;
}