Load Document rejects .md/.markdown when the document media field is missing at install
# New summary
## Description
The Load Document widget rejects `.md` / `.markdown` uploads because
`md` and `markdown` are never added to the standard Document media field
(`field.field.media.document.field_media_document`).
`ai_context_document_loader` already has an idempotent helper,
`ai_context_document_loader_extend_document_media_extensions()`, called
from `hook_install()`. That helper no-ops when the document field does
not exist yet. A later field-config import can also overwrite the
extensions after they were added.
This is **not** caused by config import or recipes skipping
`hook_install()`. Drupal still runs `hook_install()` when a module is
enabled that way. The same gap can happen on a normal install if the
document field is created after the module is enabled.
The extraction pipeline itself works (`drush document-loader:load` on a
`.md` file succeeds). The block is media field extension validation.
## Steps to reproduce
1. Go to `/admin/config/ai/context/items/add`.
2. In the Content field (MDX editor), click **Load Document**.
3. Select the **File** category → **Add media**.
4. Upload a `.md` file (for example `sample.md`).
## Expected behavior
The `.md` file is accepted. Its text is extracted and copied into the
Content editor, same as `.txt`.
## Actual behavior
The upload is rejected before Document Loader runs:
> Only files with the following extensions are allowed: txt rtf doc docx
> ppt pptx xls xlsx pdf odf odg odp ods odt fodt fods fodp fodg key
> numbers pages.
Confirmed in field config and `$media->validate()`.
## Proposed resolution
- Keep the existing idempotent helper and the `hook_install()` call.
- Add an update hook that calls the helper so already-enabled sites get
the extensions via `drush updb`.
- Also run the helper when the document field is created after module
install (FieldConfig insert or config-save for that field). A one-shot
`hook_update_N` is not enough: new installs skip it because schema is
set to the highest update number.
- Kernel test for the real race: enable the module with no document
field, create the field without `md`/`markdown`, assert the extensions
are added, and assert a second run is idempotent.
- Update `docs/features/importers.md`: install can miss the field;
existing sites need `drush updb`; sites using config management must
export the field or the next `drush cim` can remove the extensions.
---
# Original info
**Original title**
Uploading a .md/.markdown file via "Load Document" is rejected — extensions were never registered on the "document" media type
**Original summary**
## **Description:**
The `ai_context_document_loader` submodule ships a hook (`ai_context_document_loader_extend_document_media_extensions()`, invoked in `hook_install()`) whose purpose is to add the `md` and `markdown` extensions to the allowed-extensions list on the "document" media type's file field (`field.field.media.document.field_media_document`), since the module itself exists to let users import Markdown into the Context Item editor. In this install, that hook apparently never ran (common when a site is assembled via recipe/config-import rather than the standard "install module" flow), and the allowed-extensions list is still only `txt rtf doc docx ppt pptx xls xlsx pdf odf odg odp ods odt fodt fods fodp fodg key numbers pages` — no `md`/`markdown`. This blocks exactly the scenario the docs describe: "Import a small markdown or txt file."
## **Steps to reproduce:**
1. Go to `/admin/config/ai/context/items/add`.
2. In the Content field (MDX editor), click **Load Document**.
3. Select the **File** category → **Add media**.
4. Try to upload a `.md` file (e.g. `sample.md`).
**Expected behavior:** The `.md` file should be accepted, its text extracted and copied into the Content editor, ready for editing before saving as draft — same as with `.txt`.
**Actual behavior:** The `.md` file upload is rejected by the media field's extension validation before the Document Loader ever gets to process the content. Confirmed at two levels:
* Field config: allowed extensions do not include `md`/`markdown`.
* Entity validation (`$media->validate()`) on a `document`-bundle Media entity pointing at a `.md` file: returns a violation — _"Only files with the following extensions are allowed: txt rtf doc docx ppt pptx xls xlsx pdf odf odg odp ods odt fodt fods fodp fodg key numbers pages."_
{width=900 height=545}
Important: this is not a parser/loader failure — I tested the extraction pipeline directly (`drush document-loader:load`) with a real `.md` file and it extracted the content perfectly, with no PHP error. The block is purely at the media-type upload/extension-validation step.
**Evidence (CLI):**
```
$ drush cget field.field.media.document.field_media_document --format=yaml | grep extensionfile_extensions: 'txt rtf doc docx ppt pptx xls xlsx pdf odf odg odp ods odt fodt fods fodp fodg key numbers pages'
```
```
$ drush eval "... $media->validate() ..."Violations: 1 - field_media_document.0: Only files with the following extensions are allowed: txt rtf doc docx ppt pptx xls xlsx pdf odf odg odp ods odt fodt fods fodp fodg key numbers pages.
```
```
$ drush document-loader:load --input file_input=public://zoocha_sample.md --output-format=markdown# Zoocha QA Markdown SampleThis is a **markdown** file for Document Loader test case 10.- item one- item two(extraction succeeded, no error)
```
issue
GitLab AI Context
Project: project/ai_context
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/ai_context/-/raw/1.0.x/CONTRIBUTING.md — contribution guidelines
- https://git.drupalcode.org/project/ai_context/-/raw/1.0.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/ai_context
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD