You are here

public function EditForm::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/EditForm.php, line 95

Class

EditForm
Class EditForm.

Namespace

Drupal\bootstrap_simple_carousel\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $id = NULL) {
  $this->entity = $this->carouselItemStorage
    ->create();
  if (is_numeric($id)) {
    $this->entity = $this->carouselItemStorage
      ->load($id);
  }
  if (!$this->entity
    ->isNew()) {
    $form['cid'] = [
      '#type' => 'hidden',
      '#required' => FALSE,
      '#default_value' => $this->entity
        ->id(),
    ];
    $form['image_preview'] = [
      '#markup' => $this->carouselService
        ->renderImageById($this->entity
        ->getImageId()),
      '#suffix' => '<br><b>NOTE: You can\'t change image, just remove\\set inactive the item and create new one</b>',
    ];
  }
  else {
    $form['image_id'] = [
      '#type' => 'managed_file',
      '#title' => $this
        ->t('Image'),
      '#upload_validators' => [
        'file_validate_extensions' => [
          'gif png jpg jpeg',
        ],
        'file_validate_size' => [
          25600000,
        ],
      ],
      '#upload_location' => 'public://bootstrap_simple_carousel/',
      '#required' => !$this->entity
        ->isNew(),
      '#default_value' => !$this->entity
        ->isNew() ? $this->entity
        ->getImageId() : '',
    ];
  }
  $form['image_title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Image title'),
    '#required' => FALSE,
    '#default_value' => !$this->entity
      ->isNew() ? $this->entity
      ->getImageTitle() : '',
  ];
  $form['image_alt'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Image alt'),
    '#required' => FALSE,
    '#default_value' => !$this->entity
      ->isNew() ? $this->entity
      ->getImageAlt() : '',
  ];
  $form['image_link'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Image link'),
    '#description' => $this
      ->t('For instance: `https://example.com`, `node/1`, `about`'),
    '#required' => FALSE,
    '#default_value' => !$this->entity
      ->isNew() ? $this->entity
      ->getImageLink() : '',
  ];
  $form['caption_title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Caption title'),
    '#required' => FALSE,
    '#default_value' => !$this->entity
      ->isNew() ? $this->entity
      ->getCaptionTitle() : '',
  ];
  $form['caption_text'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Caption text'),
    '#required' => FALSE,
    '#default_value' => !$this->entity
      ->isNew() ? $this->entity
      ->getCaptionText() : '',
  ];
  $form['status'] = [
    '#type' => 'select',
    '#title' => 'Status',
    '#options' => $this->carouselService
      ->getStatuses(),
    '#default_value' => !$this->entity
      ->isNew() ? $this->entity
      ->getStatus() : '',
  ];
  $form['weight'] = [
    '#type' => 'number',
    '#title' => 'Weight',
    '#required' => FALSE,
    '#default_value' => !$this->entity
      ->isNew() ? $this->entity
      ->getWeight() : 0,
  ];
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#button_type' => 'primary',
  ];
  return $form;
}