0

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.

  • Is 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. – mradcliffe Sep 13 '17 at 11:22
  • 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 s3 field_vocab_image field. – Sugandh Khanna Sep 13 '17 at 11:37
-1

An Image type field should contain a file fid of an image file. The following should resolve your issue:

$img = $form_state->getValue('image_dir');

if (!empty($img['fids'])) {
  $img = reset($img['fids']);
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.