You are here

public function MimeDetectSettingsForm::buildForm in MimeDetect 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 ConfigFormBase::buildForm

File

src/Form/MimeDetectSettingsForm.php, line 31

Class

MimeDetectSettingsForm
Configure MimeDetect settings for this site.

Namespace

Drupal\mimedetect\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('mimedetect.settings');

  // Mime detection engines.
  $form['engines'] = [
    '#type' => 'fieldgroup',
    '#title' => $this
      ->t('MIME detection engines'),
  ];

  // PHP Fileinfo.
  $form['engines']['fileinfo_enable'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('PHP fileinfo'),
    '#default_value' => $config
      ->get('fileinfo.enable'),
    '#description' => $this
      ->t('Use the <a href="@url">PHP file information extension</a>. This is the preferred method.', [
      '@url' => 'http://php.net/manual/en/book.fileinfo.php',
    ]),
  ];

  // UNIX file command.
  $form['engines']['unixfile_enable'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('UNIX file'),
    '#default_value' => $config
      ->get('unixfile.enable'),
    '#description' => $this
      ->t('System call to the file command. Used when PHP fileinfo fails or is not available.'),
  ];
  $form['engines']['unixfile_binary'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Path to file command executable'),
    '#default_value' => $config
      ->get('unixfile.binary') ?: '/usr/bin/file',
    '#description' => $this
      ->t("The path to the executable 'file' binary."),
    '#states' => [
      'visible' => [
        ':input[name="unixfile_enable"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Default file name extension mapping.
  $form['engines']['fileextension_enable'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('File extension'),
    '#default_value' => TRUE,
    '#disabled' => TRUE,
    '#description' => $this
      ->t('MIME detection based on filename extension. This is the system default method, used as fall back when all others fail or are not available.'),
  ];

  // Custom MIME 'magic' file.
  $form['magicfile'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t("Custom 'magic' file path"),
    '#default_value' => $config
      ->get('magicfile'),
    '#description' => $this
      ->t('Used by any magic based engine. Leave blank to rely on system magic file or PHP internal info.'),
    '#states' => [
      'enable' => [
        [
          ':input[name="fileinfo_enable"]' => [
            'checked' => TRUE,
          ],
        ],
        [
          ':input[name="unixfile_enable"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ],
  ];
  return parent::buildForm($form, $form_state);
}