You are here

public function ImageWidgetCropExamplesForm::buildForm in Image Widget Crop 8.2

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

modules/image_widget_crop_examples/src/Form/ImageWidgetCropExamplesForm.php, line 93

Class

ImageWidgetCropExamplesForm
Configure ImageWidgetCrop general settings for this site.

Namespace

Drupal\image_widget_crop_examples\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#default_value' => $this->settings
      ->get('settings.title'),
  ];
  $form['file'] = [
    '#title' => $this
      ->t('Background Pictures'),
    '#type' => 'managed_file',
    '#description' => $this
      ->t('The uploaded image will be displayed on this page using the image style chosen below.'),
    '#default_value' => $this->settings
      ->get('settings.file'),
    '#upload_location' => 'public://image_widget_crop_examples/pictures',
    '#multiple' => FALSE,
  ];

  // In this example we haven't an ajax form element to load it after upload,
  // we need to upload file, save and crop file to provide a more simple,
  // and explicit example.
  $fid = isset($this->settings
    ->get('settings.file')[0]) ? $this->settings
    ->get('settings.file')[0] : NULL;
  if ($fid) {

    /* @var \Drupal\file\FileInterface $file */
    $file = File::load($fid);

    // The key of element are hardcoded into buildCropToForm function,
    // ATM that is mandatory but can change easily.
    $form['image_crop'] = [
      '#type' => 'image_crop',
      '#file' => $file,
      '#crop_type_list' => [
        'crop_16_9',
      ],
      '#crop_preview_image_style' => 'crop_thumbnail',
      '#show_default_crop' => TRUE,
      '#show_crop_area' => FALSE,
      '#warn_mupltiple_usages' => TRUE,
    ];
  }
  return parent::buildForm($form, $form_state);
}