You are here

public function ItemsForm::buildForm in bootstrap simple carousel 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/ItemsForm.php, line 68

Class

ItemsForm
Class ItemsForm.

Namespace

Drupal\bootstrap_simple_carousel\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $items = $this->carouselItemStorage
    ->getItems();
  $header = [
    'cio' => $this
      ->t('Item id'),
    'image_id' => $this
      ->t('Image'),
    'caption_title' => $this
      ->t('Caption title'),
    'caption_text' => $this
      ->t('Caption text'),
    'status' => $this
      ->t('Status'),
    'weight' => $this
      ->t('Weight'),
    'edit' => $this
      ->t('Edit'),
    'delete' => $this
      ->t('Delete'),
  ];
  $output = [];
  if (!empty($items)) {
    foreach ($items as $item) {
      $imageParams = [
        'alt' => $item->image_alt,
        'title' => $item->image_title,
      ];
      $output[$item->cid] = [
        'cio' => $item->cid,
        'image_id' => $this->carouselService
          ->renderImageById($item->image_id, 'thumbnail', $imageParams),
        'caption_title' => $item->caption_title,
        'caption_text' => $item->caption_text,
        'status' => $this->carouselService
          ->getStatuses()[(bool) $item->status],
        'weight' => $item->weight,
        'edit' => $this->carouselService
          ->renderLink(Url::fromRoute('bootstrap_simple_carousel.edit', [
          'id' => $item->cid,
        ]), $this
          ->t('edit')),
        'delete' => $this->carouselService
          ->renderLink(Url::fromRoute('bootstrap_simple_carousel.delete', [
          'id' => $item->cid,
        ]), $this
          ->t('delete')),
      ];
    }
  }
  $form['toolbar'] = [
    '#type' => 'fieldset',
    '#title' => '',
  ];
  $form['toolbar']['add_link'] = [
    '#title' => $this
      ->t('Add element'),
    '#type' => 'link',
    '#url' => Url::fromRoute('bootstrap_simple_carousel.add'),
    '#attributes' => [
      'class' => [
        'button button-action button--primary button--small',
      ],
    ],
  ];
  $form['toolbar']['settings_link'] = [
    '#title' => $this
      ->t('Settings'),
    '#type' => 'link',
    '#url' => Url::fromRoute('bootstrap_simple_carousel.admin_settings'),
    '#attributes' => [
      'class' => [
        'button button--primary button--small',
      ],
    ],
  ];
  $form['table'] = [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $output,
    '#empty' => $this
      ->t('No images found'),
  ];
  return $form;
}