You are here

function _focal_point_replace_media_library_preview in Focal Point 8

Process callback for the preview image of a new item in the media library.

1 string reference to '_focal_point_replace_media_library_preview'
focal_point_form_media_library_add_form_upload_alter in ./focal_point.module
Implements hook_form_FORM_ID_alter().

File

./focal_point.module, line 143
Allow users to specify a focal point on content images.

Code

function _focal_point_replace_media_library_preview(array $element, FormStateInterface $form_state, array &$form) {

  // We expect $element to be an image field widget with Focal Point enabled.
  if (!empty($element['preview'])) {

    // Temporarily override the preview access, which is normally set to FALSE
    // by the media library, in favor of its own static preview thumbnail. In
    // this case, though, Focal Point is using the preview to provide its
    // widget, so we want to be sure that's visible.
    $preview_access = $element['preview']['#access'];
    $element['preview']['#access'] = TRUE;

    // We expect the array parents to be something like
    // ['media', $delta, 'fields', $source_field, 'widget', 0]. Here, we
    // transform that to target the static preview thumbnail, which we expect to
    // be at ['media', $delta, 'preview', 'thumbnail'].
    $target = $element['#array_parents'];
    array_splice($target, -4, count($target), [
      'preview',
      'thumbnail',
    ]);
    NestedArray::setValue($form, $target, $element['preview']);

    // We've done what we needed to do, so restore the original preview access.
    $element['preview']['#access'] = $preview_access;
  }
  return $element;
}