You are here

public function ActionsForm::buildForm in S3 File System 8.3

Same name and namespace in other branches
  1. 4.0.x src/Form/ActionsForm.php \Drupal\s3fs\Form\ActionsForm::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/ActionsForm.php, line 27

Class

ActionsForm
Defines an actions form.

Namespace

Drupal\s3fs\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['#attached']['library'][] = 's3fs/s3fs.actionform';
  $form['validate_configuration'] = [
    '#type' => 'fieldset',
    '#description' => $this
      ->t("To validate current S3fs configuration include configuration inside settings.php file."),
    '#title' => $this
      ->t('Validate configuration'),
  ];
  $form['validate_configuration']['validate'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Validate'),
    '#validate' => [
      [
        $this,
        'validateConfigValidateForm',
      ],
    ],
    '#submit' => [
      [
        $this,
        'validateConfigSubmitForm',
      ],
    ],
  ];
  $form['refresh_cache'] = [
    '#type' => 'fieldset',
    '#description' => $this
      ->t("The file metadata cache keeps track of every file that S3 File System writes to (and deletes from) the S3 bucket,\n      so that queries for data about those files (checks for existence, filetype, etc.) don't have to hit S3.\n      This speeds up many operations, most noticeably anything related to images and their derivatives."),
    '#title' => $this
      ->t('File Metadata Cache'),
  ];
  $refresh_description = $this
    ->t("This button queries S3 for the metadata of <i><b>all</b></i> the files in your site's bucket (unless you use the\n    Root Folder option), and saves it to the database. This may take a while for buckets with many thousands of files. <br>\n    It should only be necessary to use this button if you've just installed S3 File System and you need to cache all the\n    pre-existing files in your bucket, or if you need to restore your metadata cache from scratch for some other reason.");
  $form['refresh_cache']['refresh'] = [
    '#type' => 'submit',
    '#suffix' => '<div class="refresh">' . $refresh_description . '</div>',
    '#value' => $this
      ->t("Refresh file metadata cache"),
    /*
     * @todo Now we can't attach css inline with #attached, when core
     * implements, we implement too
     * @see https://www.drupal.org/node/2391025
     * '#attached' => [
     *   'css' => [
     *     // Push the button closer to its own description, and push
     *     // the disable checkbox away from the description.
     *     '#edit-refresh {margin-bottom: 0; margin-top: 1em;}
     *       div.refresh {margin-bottom: 1em;}' => ['type' => 'inline']
     *   ],
     * ],
     */
    '#validate' => [
      [
        $this,
        'refreshCacheValidateForm',
      ],
    ],
    '#submit' => [
      [
        $this,
        'refreshCacheSubmitForm',
      ],
    ],
  ];
  $form['copy_local'] = [
    '#type' => 'fieldset',
    '#description' => $this
      ->t("<b>Important: This feature is for sites that have configured or going to have configured to take\n      over for the public and/or private file systems. Example: You should have\n      \$settings['s3fs.use_s3_for_public'] = TRUE; or \$settings['s3fs.use_s3_for_private'] = TRUE; after\n      or before use this actions.</b> You may wish to copy any files which were previously uploaded to\n      your site into your S3 bucket. <br> If you have a lot of files, or very large files, you'll want to\n      use <i>drush s3fs-copy-local</i> instead of this form, as the limitations imposed by browsers may\n      break very long copy operations."),
    '#title' => $this
      ->t('Copy Local Files to S3'),
  ];
  $form['copy_local']['upload_condition'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Upload Condition'),
    '#description' => $this
      ->t("When to upload files"),
    '#options' => [
      'always' => $this
        ->t('Always'),
      'newer' => $this
        ->t('Modified time is newer'),
      'size' => $this
        ->t('File size differs'),
      'newer_size' => $this
        ->t('Modified time or file size'),
    ],
    '#default_value' => 'always',
    '#required' => TRUE,
  ];
  $form['copy_local']['submitbuttons'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'copysubmitbuttons',
      ],
    ],
  ];
  $form['copy_local']['submitbuttons']['public'] = [
    '#type' => 'submit',
    '#prefix' => '<br>',
    '#name' => 'public',
    '#value' => $this
      ->t('Copy local public files to S3'),
    '#validate' => [
      [
        $this,
        'copyLocalValidateForm',
      ],
    ],
    '#submit' => [
      [
        $this,
        'copyLocalSubmitForm',
      ],
    ],
  ];
  if (Settings::get('file_private_path')) {
    $form['copy_local']['submitbuttons']['private'] = [
      '#type' => 'submit',
      '#prefix' => '<br>',
      '#name' => 'private',
      '#value' => $this
        ->t('Copy local private files to S3'),
      '#validate' => [
        [
          $this,
          'copyLocalValidateForm',
        ],
      ],
      '#submit' => [
        [
          $this,
          'copyLocalSubmitForm',
        ],
      ],
    ];
  }
  $config = \Drupal::config('s3fs.settings')
    ->get();
  $doctrineInstalled = class_exists('\\Doctrine\\Common\\Cache\\FilesystemCache');
  if (!empty($config['use_credentials_cache']) && !empty($config['credentials_cache_dir']) && $doctrineInstalled) {
    $form['credentials_cache'] = [
      '#type' => 'fieldset',
      '#description' => $this
        ->t("The credential cache persists a copy of obtained credentials to\n          avoid repeated lookups to AWS servers."),
      '#title' => $this
        ->t('Credentials Cache'),
    ];
    $credentialRefreshDescription = $this
      ->t("This button purges the directory that stores copies of obtained\n         credentials that have been cached locally.  It should not be necessary\n         to purge credentials unless an invalid set of credentials has been\n         stored in the cache.");
    $form['credentials_cache']['refresh'] = [
      '#type' => 'submit',
      '#suffix' => '<div class="refresh">' . $credentialRefreshDescription . '</div>',
      '#value' => $this
        ->t("Delete credentials cache directory"),
      '#validate' => [
        [
          $this,
          'credentialsCacheValidateForm',
        ],
      ],
      '#submit' => [
        [
          $this,
          'credentialsCacheSubmitForm',
        ],
      ],
    ];
  }
  return $form;
}