-
-
Notifications
You must be signed in to change notification settings - Fork 0
Added 'do_generated_content' module with provision script for non-production content generation. #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added 'do_generated_content' module with provision script for non-production content generation. #154
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/usr/bin/env bash | ||
| ## | ||
| # Generate content for non-production environments. | ||
| # | ||
| # shellcheck disable=SC2086 | ||
|
|
||
| set -eu | ||
| [ "${VORTEX_DEBUG-}" = "1" ] && set -x | ||
|
|
||
| # ------------------------------------------------------------------------------ | ||
|
|
||
| info() { printf " ==> %s\n" "${1}"; } | ||
| task() { printf " > %s\n" "${1}"; } | ||
| note() { printf " %s\n" "${1}"; } | ||
|
|
||
| drush() { php -d memory_limit=2G vendor/bin/drush.php -y "$@"; } | ||
|
|
||
| GENERATED_CONTENT_SKIP="${GENERATED_CONTENT_SKIP:-0}" | ||
|
|
||
| info "Started generated content operations." | ||
|
|
||
| environment="$(drush php:eval "print \Drupal\core\Site\Settings::get('environment');")" | ||
| note "Environment: ${environment}" | ||
|
|
||
| # Perform operations based on the current environment. | ||
| if echo "${environment}" | grep -q -e dev -e ci -e local; then | ||
| if [ "${VORTEX_PROVISION_OVERRIDE_DB:-0}" = "1" ]; then | ||
|
|
||
| if [ "${GENERATED_CONTENT_SKIP}" = "1" ]; then | ||
| note "Skipping generation of content." | ||
| else | ||
| task "Enabling generated content module." | ||
| export GENERATED_CONTENT_CREATE=1 | ||
| drush pm:enable -y do_generated_content | ||
| note "Generated content module enabled." | ||
| fi | ||
| else | ||
| note "Using existing database with existing content." | ||
| fi | ||
| else | ||
| note "Skipping generated content operations in production environment." | ||
| fi | ||
|
|
||
| info "Finished generated content operations." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| name: DrevOps Website Generated Content | ||
| type: module | ||
| description: 'Provides generated content for non-production environments.' | ||
| package: DrevOps | ||
| core_version_requirement: ^10.3 || ^11 | ||
| dependencies: | ||
| - generated_content:generated_content | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,51 @@ | ||||||||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| declare(strict_types=1); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| namespace Drupal\do_generated_content\Plugin\GeneratedContent; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| use Drupal\generated_content\Attribute\GeneratedContent; | ||||||||||||||||||||||||||||||||||||||
| use Drupal\generated_content\Helpers\GeneratedContentAssetGenerator; | ||||||||||||||||||||||||||||||||||||||
| use Drupal\generated_content\Plugin\GeneratedContent\GeneratedContentPluginBase; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||
| * Generated files. | ||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||
| #[GeneratedContent( | ||||||||||||||||||||||||||||||||||||||
| id: 'do_generated_content_file_file', | ||||||||||||||||||||||||||||||||||||||
| entity_type: 'file', | ||||||||||||||||||||||||||||||||||||||
| bundle: 'file', | ||||||||||||||||||||||||||||||||||||||
| weight: -10, | ||||||||||||||||||||||||||||||||||||||
| tracking: TRUE, | ||||||||||||||||||||||||||||||||||||||
| )] | ||||||||||||||||||||||||||||||||||||||
| class FileFile extends GeneratedContentPluginBase { | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||
| * {@inheritdoc} | ||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||
| public function generate(): array { | ||||||||||||||||||||||||||||||||||||||
| $entities = []; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| $types = [ | ||||||||||||||||||||||||||||||||||||||
| GeneratedContentAssetGenerator::ASSET_TYPE_JPG, | ||||||||||||||||||||||||||||||||||||||
| GeneratedContentAssetGenerator::ASSET_TYPE_PNG, | ||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| for ($i = 0; $i < 20; $i++) { | ||||||||||||||||||||||||||||||||||||||
| $type = $this->helper::randomArrayItem($types); | ||||||||||||||||||||||||||||||||||||||
| $width = $this->helper::randomBool() ? 1200 : 800; | ||||||||||||||||||||||||||||||||||||||
| $height = $this->helper::randomBool() ? 600 : 400; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| $file = $this->helper::createFile($type, [ | ||||||||||||||||||||||||||||||||||||||
| 'width' => $width, | ||||||||||||||||||||||||||||||||||||||
| 'height' => $height, | ||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| $entities[] = $file; | ||||||||||||||||||||||||||||||||||||||
| $this->helper::log('Created file: %s', $file->getFilename()); | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+41
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add null check before using If 🛡️ Proposed fix to add validation $file = $this->helper::createFile($type, [
'width' => $width,
'height' => $height,
]);
+ if ($file === NULL) {
+ continue;
+ }
+
$entities[] = $file;
$this->helper::log('Created file: %s', $file->getFilename());📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| return $entities; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Drupal\do_generated_content\Plugin\GeneratedContent; | ||
|
|
||
| use Drupal\file\FileInterface; | ||
| use Drupal\generated_content\Attribute\GeneratedContent; | ||
| use Drupal\generated_content\Plugin\GeneratedContent\GeneratedContentPluginBase; | ||
| use Drupal\media\Entity\Media; | ||
|
|
||
| /** | ||
| * Generated image media entities. | ||
| */ | ||
| #[GeneratedContent( | ||
| id: 'do_generated_content_media_civictheme_image', | ||
| entity_type: 'media', | ||
| bundle: 'civictheme_image', | ||
| weight: 1, | ||
| tracking: TRUE, | ||
| )] | ||
| class MediaCivicthemeImage extends GeneratedContentPluginBase { | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function generate(): array { | ||
| $entities = []; | ||
|
|
||
| for ($i = 0; $i < 10; $i++) { | ||
| $file = $this->helper::randomFile('jpg'); | ||
|
|
||
| if (!$file instanceof FileInterface) { | ||
| $file = $this->helper::randomFile('png'); | ||
| } | ||
|
|
||
| if (!$file instanceof FileInterface) { | ||
| continue; | ||
| } | ||
|
|
||
| $name = sprintf('Generated image %s', $i + 1); | ||
|
|
||
| $media = Media::create([ | ||
| 'bundle' => 'civictheme_image', | ||
| 'name' => $name, | ||
| 'field_c_m_image' => [ | ||
| 'target_id' => $file->id(), | ||
| 'alt' => $this->helper::staticSentence(3), | ||
| ], | ||
| ]); | ||
|
|
||
| $media->save(); | ||
| $entities[] = $media; | ||
|
|
||
| $this->helper::log('Created media: %s', $name); | ||
| } | ||
|
|
||
| return $entities; | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add missing
do_basedependency declaration.The module metadata currently omits
do_base, even though this feature set assumes base content model availability. Without explicit dependency, enable/install order can break and generators can fail at runtime.🔧 Proposed fix
dependencies: - generated_content:generated_content + - do_base:do_base📝 Committable suggestion
🤖 Prompt for AI Agents