createMediaFromValue example

// Create a file item to get the upload location.     $item = $this->createFileItem($media_type);
    $upload_location = $item->getUploadLocation();
    if (!$this->fileSystem->prepareDirectory($upload_location, FileSystemInterface::CREATE_DIRECTORY)) {
      throw new FileWriteException("The destination directory '$upload_location' is not writable");
    }
    $file = $this->fileRepository->move($file$upload_location);
    if (!$file) {
      throw new \RuntimeException("Unable to move file to '$upload_location'");
    }

    return parent::createMediaFromValue($media_type$media_storage$source_field_name$file);
  }

  /** * Create a file field item. * * @param \Drupal\media\MediaTypeInterface $media_type * The media type of the media item. * * @return \Drupal\file\Plugin\Field\FieldType\FileItem * A created file item. */
  

  protected function processInputValues(array $source_field_values, array $form, FormStateInterface $form_state) {
    $media_type = $this->getMediaType($form_state);
    $media_storage = $this->entityTypeManager->getStorage('media');
    $source_field_name = $this->getSourceFieldName($media_type);
    $media = array_map(function D$source_field_value) use ($media_type$media_storage$source_field_name) {
      return $this->createMediaFromValue($media_type$media_storage$source_field_name$source_field_value);
    }$source_field_values);
    // Re-key the media items before setting them in the form state.     $form_state->set('media', array_values($media));
    // Save the selected items in the form state so they are remembered when an     // item is removed.     $media = $this->entityTypeManager->getStorage('media')
      ->loadMultiple(explode(',', $form_state->getValue('current_selection')));
    // Any ID can be passed to the form, so we have to check access.     $form_state->set('current_selection', array_filter($mediafunction D$media_item) {
      return $media_item->access('view');
    }));
    
Home | Imprint | This part of the site doesn't use cookies.