@@ -15,10 +15,14 @@ use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
1515use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
1616use Drupal\Core\Field\BaseFieldDefinition;
1717use Drupal\Core\Field\FieldStorageDefinitionInterface;
18+ use Drupal\Core\Utility\UpdateException;
1819use Drupal\patternkit\BulkOperations\LayoutUpdateBatchManager;
1920use Drupal\patternkit\Entity\Pattern;
2021use Drupal\patternkit\Entity\PatternRouteProvider;
2122use Drupal\patternkit\Form\PatternLibraryJSONForm;
23+ use Drupal\patternkit\Hook\ReusableBlockHooks;
24+ use Drupal\patternkit\Plugin\Block\PatternkitBlock as BlockPatternkitBlock;
25+ use Drupal\patternkit\Plugin\Derivative\PatternkitBlock as DerivativePatternkitBlock;
2226use Drupal\user\Entity\Role;
2327use Drupal\user\RoleInterface;
2428use Drupal\views\EntityViewsData;
@@ -32,6 +36,20 @@ function patternkit_uninstall(bool $is_syncing): void {
3236 ->deleteAll();
3337}
3438
39+ /**
40+ * Implements hook_requirements().
41+ */
42+ function patternkit_requirements($phase): array {
43+ $requirements = [];
44+ if ($phase === 'runtime') {
45+ /** @var \Drupal\patternkit\Hook\ReusableBlockHooks $hooks */
46+ $hooks = \Drupal::classResolver(ReusableBlockHooks::class);
47+ $requirements = $hooks->requirements($phase);
48+ }
49+
50+ return $requirements;
51+ }
52+
3553/**
3654 * Allows upgrading from dev version automatically.
3755 *
@@ -524,3 +542,91 @@ function patternkit_update_10306(): string {
524542
525543 return $message;
526544}
545+
546+ /**
547+ * Update patternkit block records to correctly reference pattern ids.
548+ */
549+ function patternkit_update_10307(): string {
550+ $storage = \Drupal::entityTypeManager()->getStorage('patternkit_block');
551+ $query = $storage->getQuery();
552+ $matches = $query->condition('pattern_id', '@%', 'NOT LIKE')
553+ ->accessCheck()
554+ ->execute();
555+ /** @var \Drupal\patternkit\Entity\PatternkitBlock[] $entities */
556+ $entities = $storage->loadMultiple($matches);
557+
558+ $updated = 0;
559+ foreach ($entities as $block) {
560+ $derivative_id = $block->get('pattern_id')->value;
561+ $asset_id = DerivativePatternkitBlock::derivativeToAssetId($derivative_id);
562+ $block->set('pattern_id', $asset_id, FALSE);
563+ $block->save();
564+ $updated++;
565+
566+ \Drupal::logger('patternkit')->notice('Updated Patternkit block ID @id to correct pattern asset ID from "@old" to "@new".',
567+ [
568+ '@id' => $block->id(),
569+ '@old' => $derivative_id,
570+ '@new' => $asset_id,
571+ ]);
572+ }
573+
574+ if ($updated > 0) {
575+ return \Drupal::translation()
576+ ->formatPlural($updated, 'Fixed asset IDs for 1 block.', 'Fixed asset IDs for @count blocks.');
577+ }
578+ else {
579+ return t('No invalid asset IDs were found.');
580+ }
581+ }
582+
583+ /**
584+ * Capture unsaved changes in Patternkit blocks.
585+ */
586+ function patternkit_update_10308(): string {
587+ // Identify all patternkit blocks with serialized changes.
588+ $block_storage = \Drupal::entityTypeManager()->getStorage('block');
589+ $query = $block_storage->getQuery();
590+ $matches = $query->condition('settings.provider', 'patternkit')
591+ ->condition('settings.block_serialized', operator: 'IS NOT NULL')
592+ ->execute();
593+ $entities = $block_storage->loadMultiple($matches);
594+
595+ $message = \Drupal::translation()
596+ ->formatPlural(count($entities),
597+ 'Identified 1 Patternkit Block with unsaved changes.',
598+ 'Identified @count Patternkit blocks with unsaved changes.'
599+ );
600+ \Drupal::logger('patternkit')->notice($message);
601+
602+ // Save serialized content changes.
603+ $updated = 0;
604+ foreach ($entities as $block) {
605+ $plugin = $block->getPlugin();
606+
607+ if (!($plugin instanceof BlockPatternkitBlock)) {
608+ $message = 'Unable to successfully load Patternkit Block plugin for config block "@id". Looking for Patternkit Block ID "@block_id".';
609+ $context = [
610+ '@id' => $block->id(),
611+ '@block_id' => $block->getPluginId(),
612+ ];
613+ \Drupal::logger('patternkit')->error($message, $context);
614+ throw new UpdateException('Failed to load a block plugin for updating.');
615+ }
616+
617+ $plugin->cachePatternEntity();
618+ $plugin->saveBlockContent(TRUE);
619+ $post_save_configuration = $plugin->getConfiguration();
620+ $block->set('settings', $post_save_configuration);
621+ $block->save();
622+ $updated++;
623+ }
624+
625+ if ($updated > 0) {
626+ return \Drupal::translation()
627+ ->formatPlural($updated, 'Saved updated configuration for 1 block.', 'Saved updated configuration for @count blocks.');
628+ }
629+ else {
630+ return t('No invalid blocks were found.');
631+ }
632+ }
0 commit comments