I finally achieved what I wanted doing the following
mymodule/mymodule.module
use Drupal\Core\Form\FormStateInterface;
use Drupal\mymodule\Alter\FieldWidget\BlockImagesParagraph;
/**
* Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter().
*/
function mymodule_field_widget_single_element_entity_reference_paragraphs_form_alter(&$element, FormStateInterface $form_state, $context) {
if ($element['#paragraph_type'] == 'block_images') {
\Drupal::service('class_resolver')
->getInstanceFromDefinition(BlockImagesParagraph::class)
->alterFieldWidget($element, $form_state, $context);
}
}
mymodule/src/Alter/FieldWidget
namespace Drupal\mymodule\Alter\FieldWidget;
use Drupal\Core\Form\FormStateInterface;
/**
* Alter block_images paragraph.
*/
final class BlockImagesParagraph {
/**
* Alter the field widget.
*
* @param array $element
* The field widget form element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
* @param array $context
* An associative array representing the context.
*/
public function alterFieldWidget(array &$element, FormStateInterface $form_state, array $context) {
$widget = &$element['subform']['image']['widget'];
foreach ($widget as $key => &$value) {
if (!is_int($key)) {
continue;
}
$value['#process'][] = [
'Drupal\mymodule\Alter\FieldWidget\BlockImagesParagraph',
'processImageWidget',
];
}
}
/**
* Process the image widget.
*
* @param array $element
* The field widget form element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
* @param array $form
* An associative array representing the form.
*/
public static function processImageWidget(array $element, FormStateInterface $form_state, array $form) {
if (isset($element['title'])) {
$element['title']['#title'] = t('Caption');
}
return $element;
}
}