1

Can someone explain or post an example of how to get a third party field settings on every content entity forms?

I'm looking for a generic code where a form can be customized (like calling a custom validator) if any field present in that form is having the third party settings that I have defined.

It could be a node form, a block form, a paragraph etc.

2

Implement hook_field_widget_form_alter() to alter form elements of any content entity form.

For example how to add a validate callback when you have third party settings stored in the field configuration:

/**
 * Implements hook_field_widget_form_alter().
 */
function mymodule_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
  $field_definition = $context['items']->getFieldDefinition();
  if ($field_definition instanceof \Drupal\field\FieldConfigInterface) {
    $mymodule_setting = $field_definition->getThirdPartySettings('mymodule');
    if (!empty($mymodule_setting)) {
      $element['#element_validate'][] = 'mymodule_element_validate';
    }
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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