I'm having 1 field in a taxonomy = 'features' of type image. Image storage location is set to s3.
I have written a hook_form_alter
for form_id = article (article is the content type) where I have created an image field like:
$form['image_dir'] = [
'#type' => 'managed_file',
'#upload_location' => 's3://s3-bucket',
'#multiple' => FALSE,
'#description' => t('Allowed extensions: gif png jpg jpeg'),
'#upload_validators' => [
'file_validate_is_image' => array(),
'file_validate_extensions' => array('gif png jpg jpeg'),
'file_validate_size' => array(25600000)
],
];
Now in my form submission, I'm trying to save/map this image into my term image field (field_vocab_image
) . For this, I'm trying something like:
$term = Term::create([
'name' => $term_name,
'vid' => 'my_vocab_name',
'field_vocab_image' => $img,
]);
$inserted = $term->save();
where $img = $form_state->getvalues()['image_dir'];
this image is coming from the custom field created in hook_form_alter
.
Above code is successfully saving term name but when I go to edit term , image field is empty. Image is not getting mapped.
field_vocab_image
allowed to save to an external file system rather than just private or public? I'm surprised that a field validation error isn't occurring. There is also drupal.org/project/s3fs, which will enable s3 storage saving for image fields.field_vocab_image
is already configured to store images in s3 bucket. so I go to add term page and upload image to this field, it is storing image successfully to s3 bucket. I just need to programmatically store images to this already configured s3field_vocab_image
field.