3 namespace Drupal\media_entity_image\Plugin\EntityBrowser\Widget;
5 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\entity_browser\Plugin\EntityBrowser\Widget\Upload as FileUpload;
8 use Drupal\media_entity\MediaInterface;
11 * Uses upload to create media entity images.
13 * @EntityBrowserWidget(
14 * id = "media_entity_image_upload",
15 * label = @Translation("Upload images"),
16 * description = @Translation("Upload widget that creates media entity images."),
20 class Upload extends FileUpload {
25 public function defaultConfiguration() {
27 'extensions' => 'jpg jpeg png gif',
28 'media_bundle' => NULL,
29 ] + parent::defaultConfiguration();
35 public function getForm(array &$original_form, FormStateInterface $form_state, array $aditional_widget_parameters) {
36 /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
37 if (!$this->configuration['media_bundle'] || !($bundle = $this->entityTypeManager->getStorage('media_bundle')->load($this->configuration['media_bundle']))) {
38 return ['#markup' => $this->t('The media bundle is not configured correctly.')];
41 if ($bundle->getType()->getPluginId() != 'image') {
42 return ['#markup' => $this->t('The configured bundle is not using image plugin.')];
45 $form = parent::getForm($original_form, $form_state, $aditional_widget_parameters);
46 $form['upload']['#upload_validators']['file_validate_extensions'] = [$this->configuration['extensions']];
54 protected function prepareEntities(array $form, FormStateInterface $form_state) {
55 $files = parent::prepareEntities($form, $form_state);
57 /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
58 $bundle = $this->entityTypeManager
59 ->getStorage('media_bundle')
60 ->load($this->configuration['media_bundle']);
63 foreach ($files as $file) {
64 /** @var \Drupal\media_entity\MediaInterface $image */
65 $image = $this->entityTypeManager->getStorage('media')->create([
66 'bundle' => $bundle->id(),
67 $bundle->getTypeConfiguration()['source_field'] => $file,
78 public function submit(array &$element, array &$form, FormStateInterface $form_state) {
79 if (!empty($form_state->getTriggeringElement()['#eb_widget_main_submit'])) {
80 $images = $this->prepareEntities($form, $form_state);
83 function (MediaInterface $media) {
88 $this->selectEntities($images, $form_state);
89 $this->clearFormValues($element, $form_state);
96 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
97 $form = parent::buildConfigurationForm($form, $form_state);
98 $form['extensions'] = [
99 '#type' => 'textfield',
100 '#title' => $this->t('Allowed extensions'),
101 '#default_value' => $this->configuration['extensions'],
105 $bundle_options = [];
108 ->getStorage('media_bundle')
109 ->loadByProperties(['type' => 'image']);
111 foreach ($bundles as $bundle) {
112 $bundle_options[$bundle->id()] = $bundle->label();
115 if (empty($bundle_options)) {
116 $url = Url::fromRoute('media.bundle_add')->toString();
117 $form['media_bundle'] = [
118 '#markup' => $this->t("You don't have media bundle of the Image type. You should <a href='!link'>create one</a>", ['!link' => $url]),
122 $form['media_bundle'] = [
124 '#title' => $this->t('Media bundle'),
125 '#default_value' => $this->configuration['media_bundle'],
126 '#options' => $bundle_options,