You are here

public function MediaForm::form in Varbase Media 9.0.x

Same name and namespace in other branches
  1. 8.7 modules/entity_browser_generic_embed/src/Form/MediaForm.php \Drupal\entity_browser_generic_embed\Form\MediaForm::form()
  2. 8.5 modules/entity_browser_generic_embed/src/Form/MediaForm.php \Drupal\entity_browser_generic_embed\Form\MediaForm::form()
  3. 8.6 modules/entity_browser_generic_embed/src/Form/MediaForm.php \Drupal\entity_browser_generic_embed\Form\MediaForm::form()

Gets the actual form array to be built.

Overrides MediaForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

modules/entity_browser_generic_embed/src/Form/MediaForm.php, line 20

Class

MediaForm
Adds dynamic preview support to the media entity form.

Namespace

Drupal\entity_browser_generic_embed\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\media\MediaInterface $entity */
  $entity = $this
    ->getEntity();
  $field = MediaHelper::getSourceField($entity);
  if ($field && !$field
    ->isEmpty()) {

    // Get the source field widget element.
    $widget_keys = [
      $field
        ->getName(),
      'widget',
      0,
      $field
        ->first()
        ->mainPropertyName(),
    ];
    $widget =& NestedArray::getValue($form, $widget_keys);

    // Add an attribute to identify it.
    $widget['#attributes']['data-source-field'] = TRUE;
    if (MediaHelper::isPreviewable($entity)) {
      $widget['#ajax'] = [
        'callback' => [
          static::class,
          'onChange',
        ],
        'wrapper' => 'preview',
        'method' => 'html',
        'event' => 'change',
      ];
      $form['preview'] = [
        '#pre_render' => [
          [
            $this,
            'renderPreview',
          ],
        ],
        '#prefix' => '<div id="preview">',
        '#suffix' => '</div>',
      ];
    }
  }
  return $form;
}