I have a form that allows users to upload multiple pictures. These images are saved as media in admin/content/files. This upload process is managed by #managed_file type. Below is an example

$form['upload']['uploaded_file'] = [
    '#type' => 'managed_file',
    '#title' => t('Upload your file'),
    '#multiple' => true,
    '#upload_location' => 'public://my_files/',        
    '#upload_validators' => [
        'file_validate_extensions' => ['png gif jpg jpeg'],
        'file_validate_size' => [7500000],

After the files are uploaded, AJAX alters the form and it displays the images uploaded with a Title and Description field. Once all fields are filled out the user submit the changes. In the submitForm method, all information is saved as a image content type.

public function submitForm(array &$form, FormStateInterface $form_state)
    $values = $form_state->getValue(['upload','images']);

    for ($i = 0; $i < count($values); $i++) {

        $node = Node::create([
            'type'        => 'image',
            'title'       => $values[$i]['image']['title'],
            'field_image' => [
                'target_id' => $values[$i]['image']['fid'],
                'alt' => $values[$i]['image']['description'],
                'title' => $values[$i]['image']['title'],


The node is created and the title is passed, but the image is not. Is the target_id not the fid of the image?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.