You are here

public static function ImageCropWidget::updateCropTypeRequiredOptions in Image Widget Crop 8.2

Ajax callback for 'crop_list' select element.

This ajax callback takes care of the following things:

  • Fetching selected options on the 'crop_list' element.

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

\Drupal\Core\Ajax\AjaxResponse The Ajax response.

File

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

Class

ImageCropWidget
Plugin implementation of the 'image_widget_crop' widget.

Namespace

Drupal\image_widget_crop\Plugin\Field\FieldWidget

Code

public static function updateCropTypeRequiredOptions(array $form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $triggering_element = $form_state
    ->getTriggeringElement();
  if (isset($triggering_element['#value'])) {
    $crop_type_required_form = self::getImageCropWidgetElement($form_state, 'crop_types_required');
    $crop_type_required_form['#options'] = array_intersect_key($triggering_element['#options'], $triggering_element['#value']);

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $output = $renderer
      ->renderRoot($crop_type_required_form);

    // Transform field name onto field name class.
    $field_name_class = str_replace('_', '-', $triggering_element['#parents'][1]);

    // Re-construct triggered crop required form element class.
    $element_fragments = [
      'form-item-',
      'fields-',
      $field_name_class,
      '-settings-edit-form-settings-',
      'crop-types-required',
    ];

    // Replace existing element with selected `crop_list` options.
    $response
      ->addCommand(new ReplaceCommand('.' . implode($element_fragments), $output));
  }
  return $response;
}