I'm developing a custom module with an image field.

Where can I set the following HTML attribute to my custom field?

<input type=file

I have created the image field and i'm able to process some OCR with an external library uploading the image but I need to use webcam instead and I can't find the way to set the "capture=enviroment" tag to my input form in the field widget file.

Thank you

  • Welcome to Drupal Answers! 👋 That can be done via hook_form_alter or theme suggestions and templating. What have you tried so far and where exactly are you stuck? Please update your question for clarification. Since we don't know what exactly you need. Maybe you even don't know yet how to implement a hook in your module or theme. Then please read the docs first. Many thanks
    – leymannx
    Apr 21, 2020 at 7:17
  • The module now upload an image QR file trough the field widget and the decodes the image into text. Then I have a module form alter function and i'm not able to at least put a css class there /** * Implements hook_form_alter. * * Alters the display of a form */ function qr_decode_form_alter(&$form, FormStateInterface $form_state, $form_id) { if($form_id == 'node_employee_check_form') { $form['field_check_in']['widget']['#attributes']['class'][] = 'another-class'; } On the Widget i have the callbacks and all is working fine but I need to insert that htmlattribute
    – Alvaro dM
    Apr 21, 2020 at 8:51
  • I finally solved it on the field widget formElement function $element['#accept'] = 'image/*;capture=camera'; it works on movile devices, just what i need. I'm new on stackexchange. How I mark this as solved? Thank you Thank you
    – Alvaro dM
    Apr 21, 2020 at 10:23

1 Answer 1


Solved with formElement, at least form mobile devices. Just was I was looking for.

   * {@inheritdoc}
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);

    $element['#accept'] = 'image/*;capture=camera';

    return $element;

Thank you!

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.