function AddFormBase::buildEntityFormElement
Same name in other branches
- 9 core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::buildEntityFormElement()
- 10 core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::buildEntityFormElement()
- 11.x core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::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.
2 calls to AddFormBase::buildEntityFormElement()
- AddFormBase::buildForm in core/
modules/ media_library/ src/ Form/ AddFormBase.php - Form constructor.
- FileUploadForm::buildEntityFormElement in core/
modules/ media_library/ src/ Form/ FileUploadForm.php - Builds the sub-form for setting required fields on a new media item.
1 method overrides AddFormBase::buildEntityFormElement()
- FileUploadForm::buildEntityFormElement in core/
modules/ media_library/ src/ Form/ FileUploadForm.php - Builds the sub-form for setting required fields on a new media item.
File
-
core/
modules/ media_library/ src/ Form/ AddFormBase.php, line 255
Class
- AddFormBase
- Provides a base class for creating media items from within the media library.
Namespace
Drupal\media_library\FormCode
protected function buildEntityFormElement(MediaInterface $media, array $form, FormStateInterface $form_state, $delta) {
// We need to make sure each button has a unique name attribute. The default
// name for button elements is 'op'. If the name is not unique, the
// triggering element is not set correctly and the wrong media item is
// removed.
// @see ::removeButtonSubmit()
$parents = isset($form['#parents']) ? $form['#parents'] : [];
$id_suffix = $parents ? '-' . implode('-', $parents) : '';
$element = [
'#wrapper_attributes' => [
'aria-label' => $media->getName(),
// 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,
],
'fields' => [
'#type' => 'container',
'#weight' => 20,
// The '#parents' are set here because the entity form display needs it
// to build the entity form fields.
'#parents' => [
'media',
$delta,
'fields',
],
],
'remove_button' => [
'#type' => 'submit',
'#value' => $this->t('Remove'),
'#name' => 'media-' . $delta . '-remove-button' . $id_suffix,
'#weight' => 30,
'#attributes' => [
'aria-label' => $this->t('Remove @label', [
'@label' => $media->getName(),
]),
],
'#ajax' => [
'callback' => '::updateFormCallback',
'wrapper' => 'media-library-add-form-wrapper',
'message' => $this->t('Removing @label.', [
'@label' => $media->getName(),
]),
],
'#submit' => [
'::removeButtonSubmit',
],
// Ensure errors in other media items do not prevent removal.
'#limit_validation_errors' => [],
],
];
// @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);
// Add source field name so that it can be identified in form alter and
// widget alter hooks.
$element['fields']['#source_field_name'] = $this->getSourceFieldName($media->bundle->entity);
// 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;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.