You are here

public function ActivityListForm::buildForm in Activity 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

src/Form/ActivityListForm.php, line 25

Class

ActivityListForm
List activities form.

Namespace

Drupal\activity\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $results = [];
  $query = \Drupal::service('database')
    ->select('activity_events', 'act');
  $query
    ->fields('act', [
    'hook',
    'label',
    'event_id',
  ]);
  $results = $query
    ->execute();
  $countResults = $query
    ->countQuery()
    ->execute()
    ->fetchField();
  if ($countResults > 0) {
    $rows = [];

    // Create pagination.
    // Element per page.
    $limit = 15;

    // Current page.
    if (!empty($_REQUEST['page'])) {
      $page = $_REQUEST['page'];
      $start = $page * $limit;
      $end = ($page + 1) * $limit;
    }
    else {
      $start = 0;
      $end = $limit;
    }
    foreach ($results as $key => $value) {
      if ($key >= $start && $key < $end) {
        $row['label'] = $value->label;
        $row['hook'] = $value->hook;
        $configure_link = Link::fromTextAndUrl(t('configure'), URL::fromUri('internal:/admin/activity/configure/' . $value->event_id))
          ->toString();
        $delete_link = Link::fromTextAndUrl(t('delete'), URL::fromUri('internal:/admin/activity/delete/' . $value->event_id))
          ->toString();
        $mainLink = t('@configureLink | @deleteLink', [
          '@configureLink' => $configure_link,
          '@deleteLink' => $delete_link,
        ]);
        $row['operations'] = $mainLink;
        $rows[] = $row;
      }
    }

    // Initialize pager.
    pager_default_initialize($countResults, $limit);
    $form['activity_table'] = [
      '#type' => 'table',
      '#header' => [
        t('LABEL'),
        t('HOOK'),
        t('Operations'),
      ],
      '#attributes' => [
        'id' => 'activity_table',
        'class' => [
          'activity_table',
        ],
      ],
      '#rows' => $rows,
    ];
    $form['pager'] = [
      '#type' => 'pager',
    ];
  }
  else {
    $form['no_activities'] = [
      '#type' => 'markup',
      '#markup' => 'There are no Activity Templates created yet. ' . Link::fromTextAndUrl(t('Create one now.'), URL::fromUri('internal:/admin/activity/create'))
        ->toString(),
    ];
  }
  return $form;
}