You are here

public function MailingListsAdminForm::buildForm in Mailgun 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 FormInterface::buildForm

File

modules/mailgun_mailing_lists/src/Form/MailingListsAdminForm.php, line 50

Class

MailingListsAdminForm
Provides admin form for Mailgun lists management.

Namespace

Drupal\mailgun_mailing_lists\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['create_new_list'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Create new list'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  ];
  $form['create_new_list']['list_address'] = [
    '#title' => $this
      ->t('List address'),
    '#type' => 'email',
    '#required' => TRUE,
    '#description' => $this
      ->t('Enter the new list address'),
  ];
  $form['create_new_list']['list_name'] = [
    '#title' => $this
      ->t('List name'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#description' => $this
      ->t('Enter the new list name'),
  ];
  $form['create_new_list']['description'] = [
    '#title' => $this
      ->t('Description'),
    '#type' => 'textarea',
    '#description' => $this
      ->t('Enter short description'),
  ];
  $form['create_new_list']['access_level'] = [
    '#title' => $this
      ->t('Access Level'),
    '#type' => 'select',
    '#description' => $this
      ->t('Access level for a list'),
    '#options' => [
      'readonly' => $this
        ->t('Read Only'),
      'members' => $this
        ->t('Members'),
      'everyone' => $this
        ->t('Everyone'),
    ],
    '#defaul_value' => 'readonly',
  ];
  $form['create_new_list']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Create'),
  ];
  $mailgun = $this->mailgunClient;
  $lists = $mailgun
    ->mailingList()
    ->pages()
    ->getLists();
  $rows = [];
  if (!empty($lists)) {
    foreach ($lists as $list) {
      $rows[] = [
        'address' => $list
          ->getAddress(),
        'name' => $list
          ->getName(),
        'members' => $list
          ->getMembersCount() > 0 ? $this
          ->t('@count (@list)', [
          '@count' => $list
            ->getMembersCount(),
          '@list' => Link::createFromRoute($this
            ->t('list'), 'mailgun_mailing_lists.list', [
            'list_address' => $list
              ->getAddress(),
          ])
            ->toString(),
        ]) : $list
          ->getMembersCount(),
        'description' => $list
          ->getDescription(),
        'access_level' => $list
          ->getAccessLevel(),
        'created' => $list
          ->getCreatedAt()
          ->format('d-m-Y H:i'),
      ];
    }
    $form['lists'] = [
      '#theme' => 'table',
      '#rows' => $rows,
      '#header' => [
        $this
          ->t('Address'),
        $this
          ->t('Name'),
        $this
          ->t('Members'),
        $this
          ->t('Description'),
        $this
          ->t('Access Level'),
        $this
          ->t('Created'),
      ],
    ];
  }
  else {
    $form['lists'] = [
      '#markup' => $this
        ->t('No Mailing lists found.'),
    ];
  }
  return $form;
}