You are here

public static function FocalPointImageWidget::value in Focal Point 8

Form API callback. Retrieves the value for the file_generic field element.

This method is assigned as a #value_callback in formElement() method.

Overrides FileWidget::value

File

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

Class

FocalPointImageWidget
Plugin implementation of the 'image_focal_point' widget.

Namespace

Drupal\focal_point\Plugin\Field\FieldWidget

Code

public static function value($element, $input, FormStateInterface $form_state) {
  $return = parent::value($element, $input, $form_state);

  // When an element is loaded, focal_point needs to be set. During a form
  // submission the value will already be there.
  if (isset($return['target_id']) && !isset($return['focal_point'])) {

    /** @var \Drupal\file\FileInterface $file */
    $file = \Drupal::service('entity_type.manager')
      ->getStorage('file')
      ->load($return['target_id']);
    if ($file) {
      $crop_type = \Drupal::config('focal_point.settings')
        ->get('crop_type');
      $crop = Crop::findCrop($file
        ->getFileUri(), $crop_type);
      if ($crop) {
        $anchor = \Drupal::service('focal_point.manager')
          ->absoluteToRelative($crop->x->value, $crop->y->value, $return['width'], $return['height']);
        $return['focal_point'] = "{$anchor['x']},{$anchor['y']}";
      }
    }
    else {
      \Drupal::logger('focal_point')
        ->notice("Attempted to get a focal point value for an invalid or temporary file.");
      $return['focal_point'] = $element['#focal_point']['offsets'];
    }
  }
  return $return;
}