The widget isn't working in Drupal 11—the image preview isn't showing. Here's the working code for FileEditableWidget. Check it out, maybe modify it, and include it in the module. Without this, the module is useless.
<?php
namespace Drupal\file_entity\Plugin\Field\FieldWidget;
use Drupal\Core\Field\Attribute\FieldWidget;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\image\Plugin\Field\FieldWidget\ImageWidget;
/**
* File widget with support for editing the referenced file inline.
*/
#[FieldWidget(
id: "file_editable",
label: new TranslatableMarkup("Editable file"),
field_types: ["image"],
)]
class FileEditableWidget extends ImageWidget {
/**
* {@inheritdoc}
*/
public static function process($element, FormStateInterface $form_state, $form) {
// Get full ImageWidget processing (preview, alt, title)
$element = parent::process($element, $form_state, $form);
// If no files - return element as is
if (empty($element['#files'])) {
return $element;
}
// Add Edit button for each file
foreach ($element['#files'] as $fid => $file) {
// Ensure $file is a FileInterface object
if (!is_object($file) || !method_exists($file, 'access')) {
continue;
}
// Create unique button key
$button_key = 'edit_button_' . $fid;
$element[$button_key] = [
'#type' => 'submit',
'#value' => t('Edit'),
'#name' => 'file_editable_' . $fid,
'#ajax' => [
'url' => Url::fromRoute('entity.file.inline_edit_form', ['file' => $fid]),
],
'#access' => $file->access('update'),
'#weight' => 20, // Place after preview (weight: -10) and alt/title fields
'#submit' => ['file_managed_file_submit'],
// Ensure button doesn't trigger full form validation
'#limit_validation_errors' => [],
'#validate' => [],
];
}
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
// Additional Editable widget specific settings can be added here if needed
return $element;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
// Add information about this being an editable widget
$summary[] = $this->t('Includes inline file editing');
return $summary;
}
}
Comments
Comment #3
joseph.olstadWhat version of Drupal 11 are you using?
11.3.0-beta or is this 11.2?
What you're observing could be due to one of the approximately 75 change made in 11.2.x and another approximately 75 change notices for 11.3.x.
Please provide us with some insight as to what version of Drupal 11 you're referring to?
Comment #4
joseph.olstadBy extending ImageWidget instead of FileWidget we lose support for a document file.
What should happen is drop image from FileEditableWidget and make a new ImageEditableWidget class for image.