You are here

public function InlineEntityFormMediaWidget::getForm in DropzoneJS 8

Same name and namespace in other branches
  1. 8.2 modules/eb_widget/src/Plugin/EntityBrowser/Widget/InlineEntityFormMediaWidget.php \Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget\InlineEntityFormMediaWidget::getForm()

Overrides DropzoneJsEbWidget::getForm

File

modules/eb_widget/src/Plugin/EntityBrowser/Widget/InlineEntityFormMediaWidget.php, line 101

Class

InlineEntityFormMediaWidget
Provides an Entity Browser widget that uploads and edit new files.

Namespace

Drupal\dropzonejs_eb_widget\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);

  // @todo Remove this when/if EB provides a way to define dependencies.
  if (!$this->moduleHandler
    ->moduleExists('inline_entity_form')) {
    return [
      '#type' => 'container',
      'error' => [
        '#markup' => $this
          ->t('Missing requirement: in order to use this widget you have to install Inline entity form module first'),
      ],
    ];
  }
  $form['#attached']['library'][] = 'dropzonejs_eb_widget/ief_edit';
  $form['edit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Edit'),
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
    '#ajax' => [
      'wrapper' => 'ief-dropzone-upload',
      'callback' => [
        static::class,
        'onEdit',
      ],
      'effect' => 'fade',
    ],
    '#submit' => [
      [
        $this,
        'submitEdit',
      ],
    ],
  ];
  $form['entities']['#prefix'] = '<div id="ief-dropzone-upload">';
  $form['entities']['#suffix'] = '</div>';
  $form += [
    'entities' => [],
  ];
  if ($entities = $form_state
    ->get('uploaded_entities')) {
    foreach ($entities as $entity) {

      /** @var \Drupal\Core\Entity\EntityInterface $entity */
      $form['entities'][$entity
        ->uuid()] = [
        '#type' => 'inline_entity_form',
        '#entity_type' => $entity
          ->getEntityTypeId(),
        '#bundle' => $entity
          ->bundle(),
        '#default_value' => $entity,
        '#form_mode' => $this->configuration['form_mode'],
      ];
    }
  }
  if (!empty(Element::children($form['entities']))) {

    // Make it possible to select those submitted entities.
    $pos = array_search('dropzonejs-disable-submit', $original_form['#attributes']['class']);
    if ($pos !== FALSE) {
      unset($original_form['#attributes']['class'][$pos]);
    }
  }
  $form['actions']['submit'] += [
    '#submit' => [],
  ];
  return $form;
}