You are here

public function MediaForm::renderPreview in Lightning Media 8.2

Same name and namespace in other branches
  1. 8.4 src/Form/MediaForm.php \Drupal\lightning_media\Form\MediaForm::renderPreview()
  2. 8 src/Form/MediaForm.php \Drupal\lightning_media\Form\MediaForm::renderPreview()
  3. 8.3 src/Form/MediaForm.php \Drupal\lightning_media\Form\MediaForm::renderPreview()

Pre-render callback for the preview element.

You might wonder why this rinky-dink bit of logic cannot be done in ::form(). The reason is that, under some circumstances, the renderable preview element will contain unserializable dependencies (like such as the database connection), which will produce a 500 error when trying to cache the form for AJAX purposes.

By putting this logic in a pre-render callback, we ensure that the unserializable preview element will only exist during the rendering stage, and thus never be serialized for caching.

Parameters

array $element: The preview element.

Return value

array The renderable preview element.

File

src/Form/MediaForm.php, line 79

Class

MediaForm
Adds dynamic preview support to the media entity form.

Namespace

Drupal\lightning_media\Form

Code

public function renderPreview(array $element) {
  $entity = $this
    ->getEntity();
  return $element + Helper::getSourceField($entity)
    ->view('default');
}