You are here

public function SmsGatewayForm::buildForm in SMS Framework 8

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/SmsGatewayForm.php, line 97

Class

SmsGatewayForm
Form controller for SMS Gateways.

Namespace

Drupal\sms\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);

  /** @var \Drupal\sms\Entity\SmsGatewayInterface $sms_gateway */
  $sms_gateway = $this
    ->getEntity();
  if (!$sms_gateway
    ->isNew()) {
    $form['#title'] = $this
      ->t('Edit gateway %label', [
      '%label' => $sms_gateway
        ->label(),
    ]);
  }
  $form['gateway'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Gateway'),
    '#open' => TRUE,
  ];
  $form['gateway']['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $sms_gateway
      ->label(),
    '#required' => TRUE,
  ];
  $form['gateway']['id'] = [
    '#type' => 'machine_name',
    '#title' => $this
      ->t('Machine name'),
    '#default_value' => $sms_gateway
      ->id(),
    '#machine_name' => [
      'source' => [
        'gateway',
        'label',
      ],
      'exists' => [
        $this,
        'exists',
      ],
      'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
      'error' => 'The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores.',
    ],
    '#disabled' => !$sms_gateway
      ->isNew(),
  ];
  $form['gateway']['status'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable'),
    '#description' => $this
      ->t('Enable this gateway?'),
    '#default_value' => $sms_gateway
      ->status(),
  ];
  $plugins = [];
  foreach ($this->gatewayManager
    ->getDefinitions() as $plugin_id => $definition) {
    $plugins[$plugin_id] = $definition['label'];
  }
  $form['gateway']['plugin_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Gateway'),
    '#options' => $plugins,
    '#required' => TRUE,
    '#disabled' => !$sms_gateway
      ->isNew(),
    '#default_value' => !$sms_gateway
      ->isNew() ? $sms_gateway
      ->getPlugin()
      ->getPluginId() : '',
  ];
  $form['message_queue'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Message queue'),
    '#open' => TRUE,
  ];
  $form['message_queue']['skip_queue'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Skip queue'),
    '#description' => $this
      ->t('Whether messages sent with this gateway skip the load balancing queue and process immediately. Only turn on this setting when debugging, do not use it on production sites.'),
    '#default_value' => $sms_gateway
      ->getSkipQueue(),
  ];
  $form['message_queue']['retention_duration_incoming'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Incoming message retention'),
    '#description' => $this
      ->t('How many seconds to keep messages after they are received. Use -1 to never expire.'),
    '#field_suffix' => $this
      ->t('seconds'),
    '#default_value' => $sms_gateway
      ->getRetentionDuration(Direction::INCOMING),
    '#min' => -1,
  ];
  $form['message_queue']['retention_duration_outgoing'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Outgoing message retention'),
    '#description' => $this
      ->t('How many seconds to keep messages after they are sent. Use -1 to never expire.'),
    '#field_suffix' => $this
      ->t('seconds'),
    '#default_value' => $sms_gateway
      ->getRetentionDuration(Direction::OUTGOING),
    '#min' => -1,
  ];
  $form['incoming_messages'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Incoming messages'),
    '#open' => TRUE,
    '#optional' => TRUE,
    '#tree' => TRUE,
  ];
  $form['incoming_messages']['push_path'] = [
    '#type' => 'textfield',
    '#title' => t('Pushed messages url'),
    '#default_value' => $sms_gateway
      ->getPushIncomingPath(),
    '#description' => t('The path where incoming messages are received.'),
    '#size' => 60,
    '#field_prefix' => $this->requestContext
      ->getCompleteBaseUrl(),
    '#access' => !$sms_gateway
      ->isNew() ? $sms_gateway
      ->autoCreateIncomingRoute() : TRUE,
    '#group' => 'incoming_messages',
  ];

  // Don't check for incoming support yet, plugin type unknown.
  if (!$sms_gateway
    ->isNew()) {

    // Remove optional tag so the plain text element is shown.
    $form['incoming_messages']['#optional'] = FALSE;
    if (!$sms_gateway
      ->supportsIncoming()) {
      $form['incoming_messages']['unsupported']['#plain_text'] = $this
        ->t('This gateway does not support receiving messages.');
    }
  }
  $form['delivery_reports'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Delivery reports'),
    '#open' => TRUE,
    '#optional' => TRUE,
  ];
  $form['delivery_reports']['#tree'] = TRUE;
  $form['delivery_reports']['push_path'] = [
    '#type' => 'textfield',
    '#title' => t('Pushed delivery report Url'),
    '#default_value' => $sms_gateway
      ->getPushReportPath(),
    '#description' => t('The path where pushed delivery reports are received.'),
    '#size' => 60,
    '#field_prefix' => $this->requestContext
      ->getCompleteBaseUrl(),
    '#access' => !$sms_gateway
      ->isNew() ? $sms_gateway
      ->supportsReportsPush() : TRUE,
    '#group' => 'delivery_reports',
  ];
  if (!$sms_gateway
    ->isNew()) {
    $instance = $sms_gateway
      ->getPlugin();
    $form += $instance
      ->buildConfigurationForm($form, $form_state);
  }
  return $form;
}