You are here

public function ImageCropWidget::settingsForm in Image Widget Crop 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldWidget/ImageCropWidget.php \Drupal\image_widget_crop\Plugin\Field\FieldWidget\ImageCropWidget::settingsForm()

Returns a form to configure settings for the widget.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the widget. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

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

Return value

array The form definition for the widget settings.

Overrides ImageWidget::settingsForm

File

src/Plugin/Field/FieldWidget/ImageCropWidget.php, line 174

Class

ImageCropWidget
Plugin implementation of the 'image_widget_crop' widget.

Namespace

Drupal\image_widget_crop\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  if (!($crop_types_options = $this->imageWidgetCropManager
    ->getAvailableCropType(CropType::getCropTypeNames()))) {
    $element['message'] = [
      '#type' => 'container',
      '#markup' => $this
        ->t('No image style using the "manual crop" effect found. Please first go @link and attach the "manual crop" effect and then return to configure the field widget settings.', [
        '@link' => Link::createFromRoute('configure one here', 'entity.image_style.collection')
          ->toString(),
      ]),
      '#attributes' => [
        'class' => [
          'messages messages--error',
        ],
      ],
    ];

    // Stop process and display error message,
    // if any available Image Style is set.
    return $element;
  }
  $element = parent::settingsForm($form, $form_state);
  $element['crop_preview_image_style'] = [
    '#title' => $this
      ->t('Crop preview image style'),
    '#type' => 'select',
    '#options' => image_style_options(FALSE),
    '#default_value' => $this
      ->getSetting('crop_preview_image_style'),
    '#description' => $this
      ->t('The preview image will be shown while editing the content.'),
    '#weight' => 15,
  ];
  $element['crop_list'] = [
    '#title' => $this
      ->t('Crop Type'),
    '#type' => 'select',
    '#options' => $crop_types_options,
    '#default_value' => $this
      ->getSetting('crop_list'),
    '#multiple' => TRUE,
    '#required' => TRUE,
    '#description' => $this
      ->t('The type of crop to apply to your image. If your Crop Type not appear here, set an image style use your Crop Type'),
    '#weight' => 16,
    '#ajax' => [
      'callback' => [
        static::class,
        'updateCropTypeRequiredOptions',
      ],
      'event' => 'change',
    ],
  ];
  $element['crop_types_required'] = [
    '#title' => $this
      ->t('Required crop types'),
    '#type' => 'select',
    '#options' => $crop_types_options,
    '#default_value' => $this
      ->getSetting('crop_types_required'),
    '#multiple' => TRUE,
    '#description' => $this
      ->t('Crop types that should be required.'),
    '#weight' => 17,
  ];
  $element['show_crop_area'] = [
    '#title' => $this
      ->t('Always expand crop area'),
    '#type' => 'checkbox',
    '#default_value' => $this
      ->getSetting('show_crop_area'),
  ];
  $element['show_default_crop'] = [
    '#title' => $this
      ->t('Show default crop area'),
    '#type' => 'checkbox',
    '#default_value' => $this
      ->getSetting('show_default_crop'),
  ];
  $element['warn_multiple_usages'] = [
    '#title' => $this
      ->t('Warn the user if the crop is used more than once.'),
    '#type' => 'checkbox',
    '#default_value' => $this
      ->getSetting('warn_multiple_usages'),
  ];
  $element['crop_types_required']['#process'] = [
    // We mandatory to re-attach 'processSelect'.
    [
      Select::class,
      'processSelect',
    ],
    [
      static::class,
      'processCropTypesRequired',
    ],
  ];
  return $element;
}