You are here

public function PushNotificationForm::buildForm in Push Notifications 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/PushNotificationForm.php, line 57
Contains Drupal\push_notifications\Form\PushNotificationForm.

Class

PushNotificationForm
Form controller for the push_notification entity edit forms.

Namespace

Drupal\push_notifications\Form

Code

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

  /* @var $entity \Drupal\push_notifications\Entity\PushNotification */
  $form = parent::buildForm($form, $form_state);
  $entity = $this->entity;
  if (!$entity
    ->isPushed()) {
    $form['push_target'] = array(
      '#type' => 'radios',
      '#title' => $this
        ->t('Target'),
      '#required' => TRUE,
      '#options' => array(
        'networks' => $this
          ->t('Network'),
        'users' => $this
          ->t('User'),
      ),
      '#description' => $this
        ->t('Send a notification by network or to individual users'),
      '#weight' => 3,
    );
    $form['networks'] = array(
      '#type' => 'checkboxes',
      '#multiple' => TRUE,
      '#title' => $this
        ->t('Networks'),
      '#options' => array(
        'apns' => $this
          ->t('Apple'),
        'gcm' => $this
          ->t('Android'),
      ),
      '#description' => $this
        ->t('Select the target networks for this notification.'),
      '#states' => array(
        'visible' => array(
          ':input[name="push_target"]' => array(
            'value' => 'networks',
          ),
        ),
      ),
      '#weight' => 4,
    );
    $form['users'] = array(
      '#type' => 'entity_autocomplete',
      '#target_type' => 'user',
      '#tags' => TRUE,
      '#selection_settings' => [
        // We do not want to send to anonymous users because there may be
        // plenty and it will not be send to just one user
        'include_anonymous' => FALSE,
      ],
      '#states' => array(
        'visible' => array(
          ':input[name="push_target"]' => array(
            'value' => 'users',
          ),
        ),
      ),
      '#description' => $this
        ->t('Add the users you want to send the notification to separated by a comma.'),
      '#weight' => 4,
    );
  }
  $form['langcode'] = array(
    '#title' => $this
      ->t('Language'),
    '#type' => 'language_select',
    '#default_value' => $entity
      ->getUntranslated()
      ->language()
      ->getId(),
    '#languages' => Language::STATE_ALL,
  );
  $form['#entity_builders']['update_status'] = [
    $this,
    'updateStatus',
  ];
  return $form;
}