You are here

function thunder_update_8107 in Thunder 8.2

Installs the thunder_riddle module.

This replaces the riddle paragraphs by a new one, using media_entity as a base. After that you are able to select riddles out of the entity browser.

File

./thunder.install, line 405
Install, update and uninstall functions for the thunder installation profile.

Code

function thunder_update_8107() {

  /** @var \Drupal\update_helper\Updater $updater */
  $updater = \Drupal::service('update_helper.updater');
  $updateLogger = $updater
    ->logger();
  $entityTypeManager = \Drupal::entityTypeManager();
  $paragraphType = $entityTypeManager
    ->getStorage('paragraphs_type')
    ->load('paragraphs_riddle_marketplace');
  $successfulUpdate = TRUE;
  if (!empty($paragraphType)) {
    if (\Drupal::service('module_installer')
      ->install([
      'thunder_riddle',
    ])) {

      // Import Riddles.
      \Drupal::service('media_riddle_marketplace')
        ->createMediaEntities();

      /** @var \Drupal\paragraphs\Entity\Paragraph[] $paragraphs */
      $paragraphs = $entityTypeManager
        ->getStorage('paragraph')
        ->loadByProperties([
        'type' => 'paragraphs_riddle_marketplace',
      ]);
      foreach ($paragraphs as $oldParagraph) {
        $fieldName = $oldParagraph
          ->get('parent_field_name')
          ->get(0)
          ->getString();
        $host = $oldParagraph
          ->getParentEntity();
        if ($host) {
          foreach ($host
            ->get($fieldName)
            ->getValue() as $delta => $field) {
            if ($field['target_id'] == $oldParagraph
              ->id()) {

              /* @var \Drupal\media_entity\Entity\Media */
              $media = $entityTypeManager
                ->getStorage('media')
                ->loadByProperties([
                'bundle' => 'riddle',
                'field_riddle_id' => pathinfo(parse_url($oldParagraph
                  ->get('field_link')->uri)['path'])['filename'],
              ]);
              $media = current($media);

              // Create new Riddle paragraph for media.
              try {
                $paragraph = Paragraph::create([
                  'type' => 'riddle',
                  'field_riddle' => $media
                    ->id(),
                ]);
                $paragraph
                  ->save();
              } catch (EntityStorageException $storageException) {
                $updateLogger
                  ->warning(t('Could not create paragraph for @riddle in @name.', [
                  '@riddle' => $media
                    ->label(),
                  '@name' => $host
                    ->label(),
                ]));
                $successfulUpdate = FALSE;

                // Since paragraph is not created - skip further execution.
                continue;
              }

              // Add new paragraph to parent entity.
              $host
                ->get($fieldName)
                ->set($delta, [
                'target_id' => $paragraph
                  ->id(),
                'target_revision_id' => $paragraph
                  ->getRevisionId(),
              ]);
              try {
                $host
                  ->save();
                $updateLogger
                  ->info(t('Converted @riddle in @name.', [
                  '@riddle' => $media
                    ->label(),
                  '@name' => $host
                    ->label(),
                ]));

                // After successful saving -> remove old paragraph.
                try {
                  $oldParagraph
                    ->delete();
                } catch (EntityStorageException $storageException) {
                  $updateLogger
                    ->warning(t('Could not delete @riddle in @name.', [
                    '@riddle' => $media
                      ->label(),
                    '@name' => $host
                      ->label(),
                  ]));
                  $successfulUpdate = FALSE;
                }
              } catch (EntityStorageException $storageException) {
                $updateLogger
                  ->warning(t('Could not convert @riddle in @name.', [
                  '@riddle' => $media
                    ->label(),
                  '@name' => $host
                    ->label(),
                ]));
                $successfulUpdate = FALSE;
              }
            }
          }
        }
      }
      if ($successfulUpdate) {
        try {
          $fieldConfigs = $entityTypeManager
            ->getStorage('field_config')
            ->loadByProperties([
            'field_type' => 'entity_reference_revisions',
          ]);
          foreach ($fieldConfigs as $field) {
            $settings = $field
              ->getSetting('handler_settings');
            unset($settings['target_bundles']['paragraphs_riddle_marketplace']);
            unset($settings['target_bundles_drag_drop']['paragraphs_riddle_marketplace']);
            $field
              ->setSetting('handler_settings', $settings);
            $field
              ->save();
          }
          $paragraphType
            ->delete();
          $updateLogger
            ->info(t('Old riddle paragraph type is successfully removed.'));
        } catch (\Exception $e) {
          $successfulUpdate = FALSE;
          $updateLogger
            ->warning(t('Removing of old riddle paragraph type has failed.'));
        }
      }
    }
    else {
      $successfulUpdate = FALSE;
      $updateLogger
        ->warning(t('Riddle module is not available.'));
    }
  }
  _thunder_mark_update_checklist('v1_1__thunder_riddle', $successfulUpdate, $updateLogger);
  return $updateLogger
    ->output();
}