You are here

function panopoly_wysiwyg_update_8204 in Panopoly WYSIWYG 8.2

Rename the text formats on existing entities.

File

./panopoly_wysiwyg.install, line 85
Install hooks for Panopoly WYSIWYG.

Code

function panopoly_wysiwyg_update_8204(&$sandbox) {

  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
  $entity_field_manager = \Drupal::service('entity_field.manager');

  /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  $entity_type_manager = \Drupal::service('entity_type.manager');
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['max'] = 0;
    $sandbox['entities'] = [];

    // Find all fields and entity types that are affected.
    foreach ($entity_field_manager
      ->getFieldMap() as $entity_type_id => $entity_field_map) {
      $sandbox['entities'][$entity_type_id] = [
        'fields' => [],
        'ids' => [],
      ];
      foreach ($entity_field_manager
        ->getFieldStorageDefinitions($entity_type_id) as $field_storage_definition) {
        $field_name = $field_storage_definition
          ->getName();
        if (!isset($entity_field_map[$field_name])) {
          continue;
        }
        if (in_array($field_storage_definition
          ->getType(), [
          'text_with_summary',
          'text_long',
          'text',
        ])) {
          $sandbox['entities'][$entity_type_id]['fields'][] = $field_name;
        }
      }
      if (!empty($sandbox['entities'][$entity_type_id]['fields'])) {
        $sandbox['entities'][$entity_type_id]['ids'] = \Drupal::entityQuery($entity_type_id)
          ->execute();
        $sandbox['max'] += count($sandbox['entities'][$entity_type_id]['ids']);
      }
      if (empty($sandbox['entities'][$entity_type_id]['fields']) || empty($sandbox['entities'][$entity_type_id]['ids'])) {
        unset($sandbox['entities'][$entity_type_id]);
      }
    }
  }
  else {
    $batch_count = 0;
    foreach ($sandbox['entities'] as $entity_type_id => &$entity_info) {
      $storage = $entity_type_manager
        ->getStorage($entity_type_id);
      while (count($entity_info['ids']) > 0) {
        $id = array_shift($entity_info['ids']);
        $entity = $storage
          ->load($id);
        if (!$entity) {
          $sandbox['progress']++;
          continue;
        }
        foreach ($entity_info['fields'] as $field_name) {
          $field = $entity->{$field_name};
          if ($field && !$field
            ->isEmpty()) {
            $value = $entity->{$field_name}
              ->getValue();
            foreach ($value as &$item) {
              if ($item['format'] == 'basic_html') {
                $item['format'] = 'panopoly_wysiwyg_basic';
              }
              elseif ($item['format'] == 'full_html') {
                $item['format'] = 'panopoly_wysiwyg_full';
              }
            }
            $entity->{$field_name}
              ->setValue($value);
          }
        }
        $entity
          ->save();
        $batch_count++;
        $sandbox['progress']++;
        if ($batch_count > 50) {
          break 2;
        }
      }
    }
  }
  $sandbox['#finished'] = $sandbox['progress'] >= $sandbox['max'] ? TRUE : $sandbox['progress'] / $sandbox['max'];
  if ($sandbox['#finished']) {

    // Delete the old formats.
    $old_formats = [
      'basic_html',
      'full_html',
    ];
    foreach ($old_formats as $old_format) {
      if ($format = FilterFormat::load($old_format)) {
        $format
          ->delete();
      }
    }
  }
}