-1

I'm trying to change the text from Remove button in my file and image fields.
For the field collection items I used this code.

function MY_MODULE_field_widget_form_alter(&$element, &$form_state, $context) 
{
  $element['actions']['remove_button']['#value'] = t('x');
}

It works fine. For file and image fields I tried this code.

function MY_MODULE_preprocess_file_widget_multiple(&$variables) {
  if( strcmp($variables['element']['#type'], 'details') == 0 ){
    $variables['element'][0]['remove_button']['#value'] = t('x');
  }
}

It doesn't work. With MY_MODULE_field_widget_form_alter() I can't get the Remove button.

3
  • Are you willing to edit it front end? Jun 27, 2017 at 22:53
  • You mean using javascript? In general is something that i want to avoid but if I don't find a way I won't have any other option.
    – Ismini
    Jun 28, 2017 at 8:00
  • 1
    Thank you Chris. I can do it using javascript and CSS but I want to avoid it.
    – Ismini
    Jun 29, 2017 at 7:59

1 Answer 1

0

The solution for me was the custom add another module.

This is a very simple module that allows you to customise the text of the 'Add another item' button on multi-valued fields.

If your field supports 'unlimited' values then you are offered the option of customising the 'Add another item' button text. This setting is stored per 'bundle', so you can have different button text if you use the same field in two places.

UPDATE:

If you don't want to use unlimited upload you can just write your own module or add some piece of code in your custom module. (A big changed code from custom add another module)

In the hook_widget_form_alter:

$field = $context['items']->getFieldDefinition();
  if ($field instanceof FieldConfigInterface) {

    if (isset($element['remove_button']['#value'])) {
      $element['remove_button']['#value'] = t('x');
    }
    if (!empty($element['#type']) && $element['#type'] == 'managed_file') {
      $element['#process'][] = 'custom_file_buttons_labels';
    }
  }

And

function custom_file_buttons_labels($element, FormStateInterface $form_state, $form) {

  if (isset($element['remove_button']['#value'])) {
    $element['remove_button']['#value'] = t('x');
  }
  return $element;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.