You are here

public function EditorFileDialog::buildForm in Editor File upload 8

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

\Drupal\filter\Entity\FilterFormat $filter_format: The filter format for which this dialog corresponds.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/EditorFileDialog.php, line 89

Class

EditorFileDialog
Provides a link dialog for text editors.

Namespace

Drupal\editor_file\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, FilterFormat $filter_format = NULL) {

  // This form is special, in that the default values do not come from the
  // server side, but from the client side, from a text editor. We must cache
  // this data in form state, because when the form is rebuilt, we will be
  // receiving values from the form, instead of the values from the text
  // editor. If we don't cache it, this data will be lost.
  if (isset($form_state
    ->getUserInput()['editor_object'])) {

    // By convention, the data that the text editor sends to any dialog is in
    // the 'editor_object' key. And the image dialog for text editors expects
    // that data to be the attributes for an <img> element.
    $file_element = $form_state
      ->getUserInput()['editor_object'];
    $form_state
      ->set('file_element', $file_element);
    $form_state
      ->setCached(TRUE);
  }
  else {

    // Retrieve the image element's attributes from form state.
    $file_element = $form_state
      ->get('file_element') ?: [];
  }
  $form['#tree'] = TRUE;
  $form['#attached']['library'][] = 'editor/drupal.editor.dialog';
  $form['#prefix'] = '<div id="editor-file-dialog-form">';
  $form['#suffix'] = '</div>';

  // Load dialog settings.
  $editor = editor_load($filter_format
    ->id());
  $file_upload = $editor
    ->getThirdPartySettings('editor_file');
  $max_filesize = isset($file_upload['max_size']) ? min(Bytes::toInt($file_upload['max_size']), Environment::getUploadMaxSize()) : Environment::getUploadMaxSize();
  $existing_file = isset($file_element['data-entity-uuid']) ? $this->entityRepository
    ->loadEntityByUuid('file', $file_element['data-entity-uuid']) : NULL;
  $fid = $existing_file ? $existing_file
    ->id() : NULL;
  $form['fid'] = [
    '#title' => $this
      ->t('File'),
    '#type' => 'managed_file',
    '#upload_location' => $file_upload['scheme'] . '://' . $file_upload['directory'],
    '#default_value' => $fid ? [
      $fid,
    ] : NULL,
    '#upload_validators' => [
      'file_validate_extensions' => !empty($file_upload['extensions']) ? [
        $file_upload['extensions'],
      ] : [
        'txt',
      ],
      'file_validate_size' => [
        $max_filesize,
      ],
    ],
    '#required' => TRUE,
  ];
  $form['attributes']['href'] = [
    '#title' => $this
      ->t('URL'),
    '#type' => 'textfield',
    '#default_value' => isset($file_element['href']) ? $file_element['href'] : '',
    '#maxlength' => 2048,
    '#required' => TRUE,
  ];
  if ($file_upload['status']) {
    $form['attributes']['href']['#access'] = FALSE;
    $form['attributes']['href']['#required'] = FALSE;
  }
  else {
    $form['fid']['#access'] = FALSE;
    $form['fid']['#required'] = FALSE;
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['save_modal'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    // No regular submit-handler. This form only works via JavaScript.
    '#submit' => [],
    '#ajax' => [
      'callback' => '::submitForm',
      'event' => 'click',
    ],
  ];
  return $form;
}