drupalorg-1.0.x-dev/src/Form/LeadForm.php

src/Form/LeadForm.php
<?php

declare(strict_types=1);

namespace Drupal\drupalorg\Form;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Drupal\user\Entity\User;
use GuzzleHttp\Client;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Provides a lead form.
 */
class LeadForm extends ControllerBase implements FormInterface {

  public function __construct(
    #[Autowire(service: 'current_user')]
    protected AccountInterface $current_user,
    protected RequestStack $request_stack,
    #[Autowire(service: 'http_client')]
    protected Client $http_client,
  ) {
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return 'drupalorg_lead_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {
    $form['oid'] = [
      '#type' => 'value',
      '#value' => '00DA0000000ARBs',
    ];
    $form['retURL'] = [
      '#type' => 'hidden',
      '#value' => Url::fromUserInput('/industries/thank-you', [
        'query' => [
          'utm_source' => Url::fromRoute('<current>')->toString(),
        ],
        'absolute' => TRUE,
      ])->toString(),
    ];
    $form['00NA00000067D9R'] = [
      '#type' => 'hidden',
      '#value' => Url::fromRoute('<current>', [], ['absolute' => TRUE])->toString(),
    ];
    $form['name'] = [
      '#theme_wrappers' => ['container'],
      '#attributes' => ['class' => ['container-inline']],
    ];
    $form['name']['first_name'] = [
      '#type' => 'textfield',
      '#title' => t('First name'),
      '#maxlength' => 40,
      '#size' => 20,
      '#required' => TRUE,
      '#wrapper_attributes' => ['class' => ['form-item--medium-width']],
    ];
    $form['name']['last_name'] = [
      '#type' => 'textfield',
      '#title' => t('Last name'),
      '#maxlength' => 80,
      '#size' => 20,
      '#required' => TRUE,
      '#wrapper_attributes' => ['class' => ['form-item--medium-width']],
    ];
    $form['contact'] = [
      '#theme_wrappers' => ['container'],
      '#attributes' => ['class' => ['container-inline']],
    ];
    $form['contact']['email'] = [
      '#type' => 'textfield',
      '#title' => t('Email'),
      '#maxlength' => 80,
      '#size' => 20,
      '#required' => TRUE,
      '#wrapper_attributes' => ['class' => ['form-item--medium-width']],
    ];
    $form['contact']['phone'] = [
      '#type' => 'textfield',
      '#title' => t('Phone'),
      '#maxlength' => 40,
      '#size' => 20,
      '#wrapper_attributes' => ['class' => ['form-item--medium-width']],
    ];
    $form['company'] = [
      '#type' => 'textfield',
      '#title' => t('Company'),
      '#maxlength' => 40,
      '#size' => 20,
      '#required' => TRUE,
      '#wrapper_attributes' => ['class' => ['form-item--medium-width']],
    ];
    $form['location'] = [
      '#theme_wrappers' => ['container'],
      '#attributes' => ['class' => ['container-inline']],
    ];
    $form['location']['00NA00000067Mm6'] = [
      '#type' => 'textfield',
      '#title' => t('Country'),
      '#maxlength' => 60,
      '#size' => 20,
      '#wrapper_attributes' => ['class' => ['form-item--medium-width']],
    ];
    if ($this->current_user->isAuthenticated()) {
      $user = User::load($this->current_user->id());
      $form['first_name']['#default_value'] = $user->get('field_first_name')->value;
      $form['last_name']['#default_value'] = $user->get('field_last_name')->value;
      $form['email']['#default_value'] = $user->getEmail();
      if ($country = $user->get('field_country')->value) {
        $country_allowed_values = options_allowed_values($user->get('field_country')->getFieldDefinition()->getFieldStorageDefinition());
        $form['00NA00000067Mm6']['#default_value'] = $country_allowed_values[$country] ?? '';
      }
    }
    $form['location']['00NA00000067Ahk'] = [
      '#type' => 'select',
      '#title' => t('Region'),
      '#options' => [
        'AMER' => t('Americas'),
        'EMEA' => t('Europe, Middle East, Africa'),
        'APAC' => t('Asia Pacific, Australia, New Zealand'),
      ],
    ];
    if ($geoip_continent = $this->request_stack->getCurrentRequest()->headers->get('GeoIP-continent')) {
      if (in_array($geoip_continent, ['NA', 'SA'])) {
        // North America & South America.
        $form['00NA00000067Ahk']['#default_value'] = 'AMER';
      }
      elseif (in_array($geoip_continent, ['AF', 'EU'])) {
        // Africa & Europe.
        $form['00NA00000067Ahk']['#default_value'] = 'EMEA';
      }
      elseif (in_array($geoip_continent, ['AS', 'OC'])) {
        // Asia & Oceania.
        $form['00NA00000067Ahk']['#default_value'] = 'APAC';
      }
    }
    $form['00NA00000067AhfMAE'] = [
      '#type' => 'textarea',
      '#title' => t('Project description'),
      '#rows' => 5,
      '#resizable' => FALSE,
      '#required' => TRUE,
    ];

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => t('Get connected'),
    ];

    $form['#cache']['contexts'] = ['url', 'user', 'headers:GeoIP-continent'];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $submit_to_salesforce = Settings::get('drupalorg_lead_form_submit_to_salesforce', TRUE);
    try {
      if ($submit_to_salesforce) {
        $this->http_client->post(Settings::get('salesforce_lead_url', 'https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8'), [
          'form_params' => $form_state->cleanValues()->getValues(),
          'timeout' => 15,
        ]);
      }
      else {
        $this->getLogger('drupalorg_lead_form')->notice('Form not submitted to Salesforce per "drupalorg_lead_form_submit_to_salesforce" setting. Data: <pre>@data</pre>', [
          '@data' => print_r($form_state->cleanValues()->getValues(), TRUE),
        ]);
      }

      $this->messenger()->addMessage(t('Thank you for your submission.'));
    }
    catch (\Exception $e) {
      $this->messenger()->addWarning(t('There was an error with your form submission. Please try again. If the issue persists, contact us at <a href="mailto:help@drupal.org">help@drupal.org</a>.'));
      $this->getLogger('drupalorg_lead_form')->warning('Error @code in lead form submission at @at: @message', [
        '@code' => $e->getCode(),
        '@at' => $e->getFile() . ':' . $e->getLine(),
        '@message' => $e->getMessage(),
      ]);
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc