0

I have a drupal.org module that has changed its fieldformatter ID in a later version and I need the update hook to update all isntance of the content to use the new formatter, but cant seem to figure out how to do this.

e.g.

/**
 * Plugin implementation of the 'field_example_simple_text' formatter.
 *
 * @FieldFormatter(
 *   id = "example_field_formatter",
 *   module = "example",
 *   label = @Translation("example"),
 *   field_types = {
 *     "example_field_type"
 *   }
 * )
 */

has changed to

/**
 * Plugin implementation of the 'field_example_simple_text' formatter.
 *
 * @FieldFormatter(
 *   id = "new_example_field_formatter",
 *   module = "example",
 *   label = @Translation("example"),
 *   field_types = {
 *     "new_example_field_type"
 *   }
 * )
 */

I need to update the content/fields to be using the new formatter as it overwrites the previous one.

0

The plugin definitions you've posted are for new fields to build the options when configuring a field formatter in UI. For existing fields you have to update the field formatter in config.

For example:

use Drupal\Core\Entity\Entity\EntityViewDisplay;

function mymodule_update_8001() {
  foreach (EntityViewDisplay::loadMultiple() as $display) {
    foreach ($display->getComponents() as $name => $options) {
      if (isset($options['type']) && $options['type'] == 'old_formatter') {
        $options['type'] = 'new_formatter';
        $display->setComponent($name, $options)
          ->save();
      }
    }
  }
}

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.