You are here

public function FileTypeForm::form in File Entity (fieldable files) 8.2

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/FileTypeForm.php, line 20

Class

FileTypeForm
Form controller for file type forms.

Namespace

Drupal\file_entity\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /* @var FileType $type */
  $type = $this->entity;
  $form['label'] = array(
    '#title' => t('Label'),
    '#type' => 'textfield',
    '#default_value' => $type
      ->label(),
    '#description' => t('The human-readable name of the file type.'),
    '#required' => TRUE,
    '#size' => 30,
  );
  $form['id'] = array(
    '#type' => 'machine_name',
    '#default_value' => $type
      ->id(),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#machine_name' => array(
      'exists' => 'Drupal\\file_entity\\Entity\\FileType::load',
      'source' => array(
        'label',
      ),
    ),
    '#description' => t('A unique machine-readable name for this file type. It must only contain lowercase letters, numbers, and underscores.'),
  );
  $form['description'] = array(
    '#title' => t('Description'),
    '#type' => 'textarea',
    '#default_value' => $type
      ->getDescription(),
    '#description' => t('A brief description of this file type.'),
  );
  $form['mimetypes'] = array(
    '#type' => 'textarea',
    '#title' => t('MIME types'),
    '#description' => t('Enter one MIME type per line.'),
    '#default_value' => implode("\n", $type
      ->getMimeTypes()),
  );
  $mimetypes = new Mimetypes(\Drupal::moduleHandler());
  $form['mimetype_list'] = array(
    '#type' => 'details',
    '#title' => t('Known MIME types'),
    '#collapsed' => TRUE,
  );
  $form['mimetype_list']['list'] = array(
    '#theme' => 'item_list',
    '#items' => $mimetypes
      ->get(),
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  // Arbitrary expressions in empty() allowed in PHP 5.5 only.
  $id = $type
    ->id();
  if (!empty($id)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );
  }
  return $form;
}