You are here

public static function FocalPointImageWidget::process in Focal Point 8

Processes an image_focal_point field Widget.

Expands the image_focal_point Widget to include the focal_point field. This method is assigned as a #process callback in formElement() method.

@todo Implement https://www.drupal.org/node/2657592 Convert focal point selector tool into a standalone form element. @todo Implement https://www.drupal.org/node/2848511 Focal Point offsets not accessible by keyboard.

Overrides ImageWidget::process

File

src/Plugin/Field/FieldWidget/FocalPointImageWidget.php, line 120

Class

FocalPointImageWidget
Plugin implementation of the 'image_focal_point' widget.

Namespace

Drupal\focal_point\Plugin\Field\FieldWidget

Code

public static function process($element, FormStateInterface $form_state, $form) {
  $element = parent::process($element, $form_state, $form);
  $item = $element['#value'];
  $item['fids'] = $element['fids']['#value'];
  $element_selectors = [
    'focal_point' => 'focal-point-' . implode('-', $element['#parents']),
  ];
  $default_focal_point_value = isset($item['focal_point']) ? $item['focal_point'] : $element['#focal_point']['offsets'];

  // Override the default Image Widget template when using the Media Library
  // module so we can use the image field's preview rather than the preview
  // provided by Media Library.
  if ($form['#form_id'] == 'media_library_upload_form' || $form['#form_id'] == 'media_library_add_form') {
    $element['#theme'] = 'focal_point_media_library_image_widget';
    unset($form['media'][0]['preview']);
  }

  // Add the focal point indicator to preview.
  if (isset($element['preview'])) {
    $preview = [
      'indicator' => self::createFocalPointIndicator($element['#delta'], $element_selectors),
      'thumbnail' => $element['preview'],
    ];

    // Even for image fields with a cardinality higher than 1 the correct fid
    // can always be found in $item['fids'][0].
    $fid = isset($item['fids'][0]) ? $item['fids'][0] : '';
    if ($element['#focal_point']['preview_link'] && !empty($fid)) {
      $preview['preview_link'] = self::createPreviewLink($fid, $element['#field_name'], $element_selectors, $default_focal_point_value);
    }

    // Use the existing preview weight value so that the focal point indicator
    // and thumbnail appear in the correct order.
    $preview['#weight'] = isset($element['preview']['#weight']) ? $element['preview']['#weight'] : 0;
    unset($preview['thumbnail']['#weight']);
    $element['preview'] = $preview;
  }

  // Add the focal point field.
  $element['focal_point'] = self::createFocalPointField($element['#field_name'], $element_selectors, $default_focal_point_value);
  return $element;
}