You are here

public function EntityFormProxy::getForm in Lightning Media 8.2

Same name and namespace in other branches
  1. 8.4 src/Plugin/EntityBrowser/Widget/EntityFormProxy.php \Drupal\lightning_media\Plugin\EntityBrowser\Widget\EntityFormProxy::getForm()
  2. 8 src/Plugin/EntityBrowser/Widget/EntityFormProxy.php \Drupal\lightning_media\Plugin\EntityBrowser\Widget\EntityFormProxy::getForm()
  3. 8.3 src/Plugin/EntityBrowser/Widget/EntityFormProxy.php \Drupal\lightning_media\Plugin\EntityBrowser\Widget\EntityFormProxy::getForm()
2 calls to EntityFormProxy::getForm()
EmbedCode::getForm in src/Plugin/EntityBrowser/Widget/EmbedCode.php
FileUpload::getForm in src/Plugin/EntityBrowser/Widget/FileUpload.php
2 methods override EntityFormProxy::getForm()
EmbedCode::getForm in src/Plugin/EntityBrowser/Widget/EmbedCode.php
FileUpload::getForm in src/Plugin/EntityBrowser/Widget/FileUpload.php

File

src/Plugin/EntityBrowser/Widget/EntityFormProxy.php, line 94

Class

EntityFormProxy
Base class for EB widgets which wrap around an (inline) entity form.

Namespace

Drupal\lightning_media\Plugin\EntityBrowser\Widget

Code

public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
  $form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
  if (isset($form['actions'])) {
    $form['actions']['#weight'] = 100;
  }
  $form['bundle'] = [
    '#prefix' => '<div id="bundle">',
    '#suffix' => '</div>',
    '#weight' => 98,
  ];
  $form['entity'] = [
    '#prefix' => '<div id="entity">',
    '#suffix' => '</div>',
    '#weight' => 99,
  ];
  $value = $this
    ->getInputValue($form_state);
  if (empty($value)) {
    $form['entity']['#markup'] = NULL;
    return $form;
  }
  $allowed_bundles = $this
    ->getAllowedBundles($form_state);
  $applicable_bundles = $this->helper
    ->getBundlesFromInput($value, $allowed_bundles);

  // Show bundle select for ambiguous bundles before creating the entity.
  if (count($applicable_bundles) > 1 && !$allowed_bundles) {

    // Options array for the Bundle select.
    // @code
    //   $options = [
    //     '' => 'None',
    //     'image' => 'Image',
    //     'image_2' => 'Image 2',
    //   ];
    // @endcode
    $options = array_reduce($applicable_bundles, function (array $options, MediaTypeInterface $media_type) {
      $options[$media_type
        ->id()] = $media_type
        ->label();
      return $options;
    }, [
      '' => $this
        ->t('None'),
    ]);
    $form['bundle'] += [
      '#type' => 'select',
      '#title' => $this
        ->t('Bundle'),
      '#options' => $options,
      '#required' => TRUE,
      '#default_value' => '',
      '#ajax' => [
        'callback' => [
          static::class,
          'ajax',
        ],
      ],
    ];
    return $form;
  }
  try {
    $entity = $this->helper
      ->createFromInput($value, $allowed_bundles);
  } catch (IndeterminateBundleException $e) {
    return $form;
  }
  $form['entity'] += [
    '#type' => 'inline_entity_form',
    '#entity_type' => $entity
      ->getEntityTypeId(),
    '#bundle' => $entity
      ->bundle(),
    '#default_value' => $entity,
    '#form_mode' => $this->configuration['form_mode'],
  ];

  // Without this, IEF won't know where to hook into the widget. Don't pass
  // $original_form as the second argument to addCallback(), because it's not
  // just the entity browser part of the form, not the actual complete form.
  ElementSubmit::addCallback($form['actions']['submit'], $form_state
    ->getCompleteForm());
  return $form;
}