I have a custom class TextWidget extends WidgetBase
Inside it I have a custom validation handler that looks like this:
public function validate($element, FormStateInterface $form_state) { $value = $element['#value']; $label = 'This field'; // What method is there to get the human facing field name? if (strlen($value) === 0) { $form_state->setError($element, $this->t('@label cannot be empty.', ['@label' => $label])); } }
It outputs the error "This field cannot be empty." if the field is empty.
I want to replace the static text ‘This field’ with the actual human facing name of the field.
Is there a public method that lets me do that?