You are here

public function DeleteForm::buildForm in Filebrowser 8.2

Same name and namespace in other branches
  1. 3.x src/Form/DeleteForm.php \Drupal\filebrowser\Form\DeleteForm::buildForm()

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 ConfirmFormBase::buildForm

File

src/Form/DeleteForm.php, line 76

Class

DeleteForm

Namespace

Drupal\filebrowser\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $nid = null, $query_fid = 0, $fids_str = null, $ajax = null) {
  $this->node = Node::load($nid);
  $this->queryFid = $query_fid;
  $this->filebrowser = $this->node->filebrowser;
  $fids = explode(',', $fids_str);

  // $this->error = false;
  // This flag indicates that a folder has been selected for deletion.
  $folder_selected = false;
  $files = $this->common
    ->nodeContentLoadMultiple($fids);
  foreach ($files as $fid => $file) {

    // Additional data.
    $file['type'] = unserialize($file['file_data'])->type;
    $file['full_path'] = $this->validator
      ->encodingToFs($this->filebrowser->encoding, $this->validator
      ->getNodeRoot($this->filebrowser->folderPath . $file['path']));
    $file['display_name'] = $this->validator
      ->safeBaseName($file['full_path']);

    // Store item data.
    $this->itemsToDelete[$fid] = $file;
  }

  // Compose the list of files being deleted.
  $list = '<ul>';
  foreach ($this->itemsToDelete as $item) {
    $list .= '<li>';
    if ($item['type'] == 'dir') {
      $folder_selected = true;
      $list .= '<b>' . $item['display_name'] . '</b>';
    }
    else {
      $list .= $item['display_name'];
    }
    $list .= '</li>';
  }
  $list .= '</ul>';
  if ($ajax) {
    $form['#attributes'] = [
      'class' => [
        'form-in-slide-down',
      ],
    ];

    // Add a close slide-down-form button
    $form['close_button'] = $this->common
      ->closeButtonMarkup();
  }
  $form['items'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('Items being deleted'),
    '#markup' => $list,
  ];

  // If at least a folder has been selected, add a confirmation checkbox.
  if ($folder_selected) {
    $form['confirmation'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Confirm deletion of selected <b>folders</b> and all of their content.'),
      '#default_value' => false,
    ];
  }
  else {

    // No confirmation needed, we'll add a "fake" field.
    $form['confirmation'] = [
      '#type' => 'value',
      '#value' => TRUE,
    ];
  }
  $form = parent::buildForm($form, $form_state);
  $form['actions']['cancel']['#attributes']['class'][] = 'button btn btn-default';
  if ($ajax) {
    $form['actions']['submit']['#attributes']['class'][] = 'use-ajax-submit';
    $this->ajax = true;
  }
  return $form;
}