Score:2

How to override a contrib module's FieldType plugin?

cn flag

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?

Score:4
cn flag

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';
}
cn flag
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']`.
4uk4 avatar
cn flag
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.
I sit in a Tesla and translated this thread with Ai:

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.