3

I'm trying to override a FieldType plugin provided by the Field Encrypt module:

class EncryptedFieldStorageItem extends FieldItemBase {

I created my own class, MyModuleEncryptedFieldStorageItem, and put it in my_module/src/Plugin/Field/FieldType/MyModuleEncryptedFieldStorageItem.php.

Now I need to inform Drupal that I want to override the Field Encrypt module's EncryptedFieldStorageItem class with my MyModuleEncryptedFieldStorageItem class, right? In the past, I've done this using MyModuleServiceProvider to set the class, but here, I don't want to override the field type plugin class; I want to override the class of one specific plugin. How can I do that?

1 Answer 1

6

The FieldType plugin manager provides an alter hook mymodule_field_info_alter().

Example from the core language module:

/**
 * Implements hook_field_info_alter().
 */
function language_field_info_alter(&$info) {
  // Change the default behavior of language field.
  $info['language']['class'] = '\Drupal\language\DefaultLanguageItem';
}
2
  • 1
    For anyone specifically trying to change the field Encrypt module's class, note that it isn't always the module's name. In this case, it's $info['encrypted_field_storage']['class']. Aug 20, 2023 at 19:10
  • 1
    Yes, this is a coincidence that the language module is changing the language field. You find the plugin's ID in the class annotations, in this example * @FieldType(id = "language" ... in /core/lib/Drupal/Core/Field/Plugin/Field/FieldType/LanguageItem.php.
    – 4uk4
    Aug 20, 2023 at 22:29

Your Answer

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

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