Score:0

How to change the label of an image's title field in a paragraph

um flag

In a paragraph I have an image field for which the title field is enabled and required. I need to alter the label of this title field to replace "Title" by "Caption"

I tried to use the hook_field_widget_single_element_WIDGET_TYPE_form_alter() to achieve this but all I see related to this title is

$element['subform']['image']['widget'][0]['#title_field'] = (bool) 1
$element['subform']['image']['widget'][0]['#title_field_required'] = (bool) 1

I am wondering how I can alter that label in this paragraph context. Any help would be appreciated.

Score:1
um flag

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;
  }

}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.