protected function MediaEditForm::buildEntityFormElement in Media Directories 3.x
Same name and namespace in other branches
- 8 modules/media_directories_ui/src/Form/MediaEditForm.php \Drupal\media_directories_ui\Form\MediaEditForm::buildEntityFormElement()
- 2.x modules/media_directories_ui/src/Form/MediaEditForm.php \Drupal\media_directories_ui\Form\MediaEditForm::buildEntityFormElement()
Builds the sub-form for setting required fields on a new media item.
Parameters
\Drupal\media\MediaInterface $media: A new, unsaved media item.
array $form: The complete form.
\Drupal\Core\Form\FormStateInterface $form_state: The current form state.
int $delta: The delta of the media item.
Return value
array The element containing the required fields sub-form.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
1 call to MediaEditForm::buildEntityFormElement()
- MediaEditForm::buildForm in modules/
media_directories_ui/ src/ Form/ MediaEditForm.php - Form constructor.
File
- modules/
media_directories_ui/ src/ Form/ MediaEditForm.php, line 212
Class
- MediaEditForm
- Class MediaEditForm
Namespace
Drupal\media_directories_ui\FormCode
protected function buildEntityFormElement(MediaInterface $media, array $form, FormStateInterface $form_state, $delta) {
$element = [
'#type' => 'container',
'#attributes' => [
'class' => [
'media-library-add-form__media',
],
'aria-label' => $media
->getName(),
'role' => 'listitem',
// Add the tabindex '-1' to allow the focus to be shifted to the next
// media item when an item is removed. We set focus to the container
// because a media item does not necessarily have required fields and we
// do not want to set focus to the remove button automatically.
// @see ::updateFormCallback()
'tabindex' => '-1',
// Add a data attribute containing the delta to allow us to easily shift
// the focus to a specific media item.
// @see ::updateFormCallback()
'data-media-library-added-delta' => $delta,
],
'preview' => [
'#type' => 'container',
'#weight' => 10,
'#attributes' => [
'class' => [
'media-library-add-form__preview',
],
],
],
'fields' => [
'#type' => 'container',
'#weight' => 20,
'#attributes' => [
'class' => [
'media-library-add-form__fields',
],
],
// The '#parents' are set here because the entity form display needs it
// to build the entity form fields.
'#parents' => [
'media',
$delta,
'fields',
],
],
];
// @todo Make the image style configurable in
// https://www.drupal.org/node/2988223
$source = $media
->getSource();
$plugin_definition = $source
->getPluginDefinition();
if ($thumbnail_uri = $source
->getMetadata($media, $plugin_definition['thumbnail_uri_metadata_attribute'])) {
$element['preview']['thumbnail'] = [
'#theme' => 'image_style',
'#style_name' => 'media_library',
'#uri' => $thumbnail_uri,
];
}
$form_display = EntityFormDisplay::collectRenderDisplay($media, 'media_library');
// When the name is not added to the form as an editable field, output
// the name as a fixed element to confirm the right file was uploaded.
if (!$form_display
->getComponent('name')) {
$element['fields']['name'] = [
'#type' => 'item',
'#title' => $this
->t('Name'),
'#markup' => $media
->getName(),
];
}
$form_display
->buildForm($media, $element['fields'], $form_state);
// We hide the preview of the uploaded file in the image widget with CSS.
// @todo Improve hiding file widget elements in
// https://www.drupal.org/project/drupal/issues/2987921
$bundle = $this->entityTypeManager
->getStorage('media_type')
->load($media
->bundle());
$source_field_name = $this
->getSourceFieldName($bundle);
if (isset($element['fields'][$source_field_name])) {
$element['fields'][$source_field_name]['#attributes']['class'][] = 'media-library-add-form__source-field';
}
// The revision log field is currently not configurable from the form
// display, so hide it by changing the access.
// @todo Make the revision_log_message field configurable in
// https://www.drupal.org/project/drupal/issues/2696555
if (isset($element['fields']['revision_log_message'])) {
$element['fields']['revision_log_message']['#access'] = FALSE;
}
return $element;
}