You are here

public function PhotosDirectoryImportForm::buildForm in Album Photos 8.5

Same name and namespace in other branches
  1. 8.4 src/Form/PhotosDirectoryImportForm.php \Drupal\photos\Form\PhotosDirectoryImportForm::buildForm()
  2. 6.0.x src/Form/PhotosDirectoryImportForm.php \Drupal\photos\Form\PhotosDirectoryImportForm::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 FormInterface::buildForm

File

src/Form/PhotosDirectoryImportForm.php, line 97

Class

PhotosDirectoryImportForm
Defines a form to upload photos to this site.

Namespace

Drupal\photos\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $submit_text = $this
    ->t('Move images');
  $show_submit = TRUE;

  // Add warning that images will be moved to the album's directory.
  $instructions = $this
    ->t('Add photos to an album from a directory that is already on the server. First choose a user.
                     Then select an album. Then enter the directory where the photos are located. Note that the photos
                     will be moved to the selected albums directory. Warning: large zip files could fail depending on
                     server processing power. If it does fail, try unzipping the folders and running the batch again.');
  $form['instructions'] = [
    '#markup' => '<div>' . $instructions . '</div>',
  ];
  if ($uid = $form_state
    ->getValue('user')) {

    // Look up user albums and generate options for select list.
    $albums = $this->connection
      ->query("SELECT nid, title FROM {node_field_data} WHERE uid = :uid AND type = 'photos'", [
      ':uid' => $uid,
    ]);
    $options = [];
    foreach ($albums as $album) {
      $options[$album->nid] = '[nid:' . $album->nid . '] ' . $album->title;
    }
    if (empty($options)) {

      // No albums found for selected user.
      $add_album_link = Link::fromTextAndUrl($this
        ->t('Add new album.'), Url::fromUri('base:node/add/photos'))
        ->toString();
      $form['add_album'] = [
        '#markup' => '<div>' . $this
          ->t('No albums found.') . ' ' . $add_album_link . '</div>',
      ];
      $show_submit = FALSE;
    }
    else {

      // Select album.
      $form['uid'] = [
        '#type' => 'hidden',
        '#value' => $uid,
      ];
      $form['album'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Select album'),
        '#options' => $options,
      ];

      // Directory.
      $form['directory'] = [
        '#title' => $this
          ->t('Directory'),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#default_value' => '',
        '#description' => $this
          ->t('Directory containing images. Include / for absolute path. Include
            public:// or private:// to scan a directory in the public or private filesystem.'),
      ];

      // Copy.
      $form['copy'] = [
        '#title' => $this
          ->t('Copy files instead of moving them.'),
        '#type' => 'checkbox',
        '#default_value' => 0,
      ];
    }
  }
  else {

    // User autocomplete.
    $form['user'] = [
      '#type' => 'entity_autocomplete',
      '#title' => $this
        ->t('Username'),
      '#description' => $this
        ->t('Enter a user name.'),
      '#target_type' => 'user',
      '#tags' => FALSE,
      '#required' => TRUE,
      '#default_value' => '',
      '#process_default_value' => FALSE,
    ];
    $submit_text = $this
      ->t('Select user');
  }
  if ($show_submit) {
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $submit_text,
      '#weight' => 10,
    ];
  }
  return $form;
}