2

I'm trying to customize text for a file upload widget in Drupal 8.

Here is a screenshot of the widget:

File upload widget

I just want to display the first 15 MB which I added as file field description text in the content type.

The trouble is the last 3 lines of text have no HTML tags so I can't selectively hide it with display:none;. Here is what I mean:

HTML for file upload

I tried this CSS but it doesn't work:

#edit-field-dataset-file-0--description * {
    display:none;
}

#edit-field-dataset-file-0--description p {
    display:inline !important;
}

Does anyone have any suggestions?

  • 2
    There's a chance that a form alter might help in this case but I am not sure, I'll see if I have some time to test it out. – Danny Englander Dec 28 '19 at 17:12
  • Yeah, try to hook into the form using hook_form_alter and unset the description. – leymannx Dec 28 '19 at 17:24
  • Thanks @DannyEnglander @leymannx! I used hook_field_widget_form_alter. Feel free to post your own code which I can accept as an answer but in the meantime I'm going to post a temporary solution. – Parag Dec 28 '19 at 17:48
1

I used hook_field_widget_form_alter as suggested by DannyEnglander and leymannx.

8.8.x hook_field_widget_form_alter

Replace MYMODULE with your custom module's name and alter the element array to suit your needs.

/**
 * Implements hook_field_widget_form_alter().
 */
function MYMODULE_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
  if (get_class($context['widget']) === 'Drupal\file\Plugin\Field\FieldWidget\FileWidget' && !empty($context['form']['#attributes'])) {
    $element['#description'] = '<p>File must be less than <strong>15 MB</strong>.<br/>Allowed types: <code>txt csv tsv xls xlsx sav sas7bdat</code></p>';
  }
}
|improve this answer|||||
  • Looks like you've got it! I think you can accept this as the answer. – Danny Englander Dec 28 '19 at 19:11
  • 1
    SE says I can accept the answer in two days. I hope I don't forget. – Parag Dec 28 '19 at 21:28

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.