function text_post_update_allowed_formats

Add allowed_formats setting to existing text fields.

File

core/modules/text/text.post_update.php, line 25

Code

function text_post_update_allowed_formats(&$sandbox = NULL) {
  \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'field_config', function (FieldConfigInterface $field_config) {
    $class = get_class($field_config);
    // Deal only with text fields and descendants.
    if (is_a($class, TextItemBase::class, TRUE)) {
      // Get the existing allowed_formats setting.
      $allowed_formats = $field_config->get('settings.allowed_formats');
      if (!is_array($allowed_formats) && empty($allowed_formats)) {
        // Save default value if existing value not present.
        $field_config->set('settings.allowed_formats', []);
        return TRUE;
      }
    }
    return FALSE;
  });
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.