diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 2b53114..b111f41 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -1853,88 +1853,63 @@ function system_update_8014($sandbox) { // to new format_plural_string configuration. Also update associated // formatters and widgets. + $config_factory = \Drupal::configFactory(); $views_fields_to_update = []; + $types = ['float', 'decimal', 'integer']; + + foreach ($config_factory->listAll('field.field.') as $field_config_name) { + $field_config = $config_factory->getEditable($field_config_name); + if (in_array($field_config->get('field_type'), $types)) { + $settings = $field_config->get('settings'); + $prefix = isset($settings['prefix']) ? $settings['prefix'] : ''; + $suffix = isset($settings['suffix']) ? $settings['suffix'] : ''; + if (($prefix || $suffix) && (!isset($settings['format_plural_string']) || !$settings['format_plural_string'])) { + // In case they were using the previous system to separate + // plural values by |, make this into a proper format_plural + // object. + $prefixes = explode('|', $prefix); + $suffixes = explode('|', $suffix); + $length = max(count($prefixes), count($suffixes)); + $strings = []; + for ($i = 0; $i < $length; $i++) { + $prefix = isset($prefixes[$i]) ? $prefixes[$i] : $prefixes[count($prefixes) - 1]; + $suffix = isset($suffixes[$i]) ? $suffixes[$i] : $suffixes[count($suffixes) - 1]; + $strings[] = $prefix . '@count' . $suffix; + } + $settings['format_plural_string'] = implode(PluralTranslatableMarkup::DELIMITER, $strings); + } + else { + $settings['format_plural_string'] = ''; + } - // Locate all the numeric field storage config items. - foreach (['float', 'decimal', 'integer'] as $type) { - $field_storage_configs = \Drupal::entityManager()->getStorage('field_storage_config')->loadByProperties(array('type' => $type)); - if (!$field_storage_configs) { - continue; - } - foreach ($field_storage_configs as $field_storage) { + unset($settings['prefix']); + unset($settings['suffix']); - // For each field storage item, locate the field config items. - $field_name = $field_storage->getName(); - $field_configs = \Drupal::entityManager()->getStorage('field_config')->loadByProperties(array('field_name' => $field_name)); - if (!$field_configs) { - continue; - } + $field_config->set('settings', $settings); + $field_config->save(TRUE); + + $field_name = $field_config->get('field_name'); - // Save the field name to a list to update it in views outside this loop. $views_fields_to_update[] = $field_name; - foreach ($field_configs as $field) { - - // Update the prefix/suffix settings on the field config to the new - // format_plural_string setting. - $settings = $field->get('settings'); - $prefix = isset($settings['prefix']) ? $settings['prefix'] : ''; - $suffix = isset($settings['suffix']) ? $settings['suffix'] : ''; - if (($prefix || $suffix) && (!isset($settings['format_plural_string']) || !$settings['format_plural_string'])) { - // In case they were using the previous system to separate - // plural values by |, make this into a proper format_plural - // object. - $prefixes = explode('|', $prefix); - $suffixes = explode('|', $suffix); - $length = max(count($prefixes), count($suffixes)); - $strings = []; - for ($i = 0; $i < $length; $i++) { - $prefix = isset($prefixes[$i]) ? $prefixes[$i] : $prefixes[count($prefixes) - 1]; - $suffix = isset($suffixes[$i]) ? $suffixes[$i] : $suffixes[count($suffixes) - 1]; - $strings[] = $prefix . '@count' . $suffix; - } - $settings['format_plural_string'] = implode(PluralTranslatableMarkup::DELIMITER, $strings); - } - else { + foreach ($config_factory->listAll('core.entity_view_display.') as $view_display_name) { + $view_display = $config_factory->getEditable($view_display_name); + if ($settings = $view_display->get('content.' . $field_name . '.settings')) { + $settings['format_plural'] = (isset($settings['prefix_suffix']) && $settings['prefix_suffix']) ? 'field' : 'none'; + unset($settings['prefix_suffix']); $settings['format_plural_string'] = ''; - } - - unset($settings['prefix']); - unset($settings['suffix']); - $field->set('settings', $settings); - $field->save(); - // Locate formatters on entity view displays for each field. - $properties = array( - 'targetEntityType' => $field->getTargetEntityTypeId(), - 'bundle' => $field->getTargetBundle() - ); - if ($view_displays = \Drupal::entityManager()->getStorage('entity_view_display')->loadByProperties($properties)) { - foreach ($view_displays as $view_display) { - - // Update the formatter setting prefix_suffix (Boolean) to be - // format_plural (values: 'field' (TRUE) or 'none' (FALSE)), - // and set format_plural_string to an empty string. - if ($component = $view_display->getComponent($field_name)) { - $settings = $component['settings']; - $settings['format_plural'] = (isset($settings['prefix_suffix']) && $settings['prefix_suffix']) ? 'field' : 'none'; - unset($settings['prefix_suffix']); - $settings['format_plural_string'] = ''; - $component['settings'] = $settings; - $view_display->setComponent($field_name, $component)->save(); - } - } + $view_display->set('content.' . $field_name . '.settings', $settings); + $view_display->save(TRUE); } + } - // Locate widgets on entity form displays for each field. - if ($form_displays = \Drupal::entityManager()->getStorage('entity_form_display')->loadByProperties($properties)) { - foreach ($form_displays as $form_display) { - // Add a prefix_suffix setting, set to TRUE. - if ($component = $form_display->getComponent($field_name)) { - $component['settings']['format_plural'] = TRUE; - $form_display->setComponent($field_name, $component)->save(); - } - } + foreach ($config_factory->listAll('core.entity_form_display.') as $form_display_name) { + $form_display = $config_factory->getEditable($form_display_name); + if ($settings = $form_display->get('content.' . $field_name . '.settings')) { + $settings['format_plural'] = TRUE; + $form_display->set('content.' . $field_name . '.settings', $settings); + $form_display->save(TRUE); } } } @@ -1944,7 +1919,6 @@ function system_update_8014($sandbox) { // part of views. There isn't a way to query for which views might have // them, so we have to go through all the displays on all the views. if (count($views_fields_to_update)) { - $config_factory = \Drupal::configFactory(); foreach ($config_factory->listAll('views.view.') as $view_config_name) { $view = $config_factory->getEditable($view_config_name); $displays = $view->get('display'); @@ -1965,7 +1939,7 @@ function system_update_8014($sandbox) { } if ($changed) { $view->set('display', $displays); - $view->save(); + $view->save(TRUE); } } }