Skip to content

The AI disclosure panel of a media form

Each media form gets one AI disclosure panel. It is the details element of the disclosure widget. A module can add a preview to it and can add a section to it. See The panel on a media form for what an editor sees.

Adding a preview

Implement hook_ai_disclosure_media_preview_alter():

function my_module_ai_disclosure_media_preview_alter(array &$preview, MediaPanelContext $context): void {
  $file = $context->getFile();
  if ($file !== NULL && str_starts_with((string) $file->getMimeType(), 'image/')) {
    $preview['image'] = \Drupal::service('ai_disclosure.media_preview_builder')
      ->build('image', \Drupal::service('file_url_generator')->generateString($file->getFileUri()));
  }
}

The context says what the panel knows about the file and its disclosure. The file may be an upload that is not saved yet. The disclosure is the one chosen in the widget. The hook runs when the form is built and each time the panel is refreshed by AJAX. Put one render array per kind of preview under its own key. Leave $preview alone for a file you do not preview.

Adding a section

Call MediaPanel::attach() from a form alter. The service is ai_disclosure.media_panel.

$attached = $panel->attach($form, $field_names, 'my_section', $section, 120);

The arguments are the form, the names of the disclosure fields of the media bundle, the key of the section, the section, and its weight. The widget's own elements weigh less than 100. Pass TRUE as the sixth argument to hide the section while the widget says the work is artistic. An artistic work gets only the basic icon.

The method returns TRUE when the panel took the section. It returns FALSE when the form has no panel. Keep the section as a details element of its own then. The elements keep their #parents, so the values keep their names.