Giter VIP home page Giter VIP logo

generated_content's Introduction

Generated Content

Generated Content

GitHub Issues GitHub Pull Requests CircleCI GitHub release (latest by date) LICENSE Renovate

Drupal 9 Drupal 10

Drupal.org module page: https://www.drupal.org/project/generated_content

User stories

As a site owner
I want to see generated content before I have content
So that I can see how my site looks

As a Drupal developer
I want to control what is put into generated content
So that I could have control over what is being generated

As a Drupal developer
I want to have a list of pre-generated pages with URLs
So that I could use then for Visual Regression testing during site releases

Installation

composer require drupal/generated_content

How it works

  1. The module provides callbacks system to generate content entities within a code of a custom module sitting in generated_content/{entity_type}/{entity_bundle}.inc.

  2. The module provides a helper (singleton) class to generate random and static content. It also supports extending this class in your custom module to enhance with your site-specific generation helpers.

  3. Generated content entities are tracked in the Repository so that they could be referenced from other generated entities (e.g., generated Articles using generated Tags).

  4. Content can be generated from UI /admin/config/development/generated-content or through a Drush command drush generated-content:create-content {entity_type} {bundle}.

  5. Content can also be generated on module install if GENERATED_CONTENT_CREATE environment variable is set to 1. Generation can be further filtered by specified types in GENERATED_CONTENT_ITEMS environment variable as a comma-separated list of {entity_type}-{bundle} values:

    # Generate all items in my_module module when it is enabled.
    GENERATED_CONTENT_CREATE=1 drush pm-enable my_module
    
    # Generate only selected items in my_module module when it is enabled.
    GENERATED_CONTENT_CREATE=1 GENERATED_CONTENT_ITEMS=media-image,taxonomy_term-tags,node-page drush pm-enable my_module
    

See test example module 1 and test example module 2 for extensive examples.

See generated_content.api.php for API of callbacks system.

Example to generate Tags

<?php

/**
 * @file
 * Create generated Tags terms.
 */

use Drupal\Core\Link;
use Drupal\generated_content\Helpers\GeneratedContentHelper;
use Drupal\taxonomy\Entity\Term;

/**
 * Implements hook_generated_content_create_ENTITY_TYPE_BUNDLE_weight().
 */
function generated_content_example2_generated_content_create_taxonomy_term_tags_weight() {
  return 12;
}

/**
 * Implements hook_generated_content_create_ENTITY_TYPE_BUNDLE_tracking().
 */
function generated_content_example2_generated_content_create_taxonomy_term_tags_tracking() {
  return TRUE;
}

/**
 * Implements hook_generated_content_create_ENTITY_TYPE_BUNDLE().
 */
function generated_content_example2_generated_content_create_taxonomy_term_tags() {
  // Total number of terms to create.
  $total_terms_count = 10;

  /** @var \Drupal\generated_content\Helpers\GeneratedContentHelper $helper */
  $helper = GeneratedContentHelper::getInstance();

  $terms = [];

  for ($i = 0; $i < $total_terms_count; $i++) {
    // Create a term instance.
    $term = Term::create([
      'vid' => 'tags',
      'name' => 'Generated term ' . ($i + 1),
    ]);

    // Save term instance.
    $term->save();

    // Track saved term instance to return.
    $terms[] = $term;

    // Log creation of this entity.
    $helper::log(
      'Created "%s" term "%s" [ID: %s] %s',
      $term->bundle(),
      $term->toLink()->toString(),
      $term->id(),
      Link::createFromRoute('Edit', 'entity.taxonomy_term.edit_form', ['taxonomy_term' => $term->id()])->toString()
    );
  }

  // Return created term instances.
  return $terms;
}

Generation helper

Generation helper class GeneratedContentHelper is a Singleton class which provides:

  1. Random non-Drupal scalar values generation.
  2. Static non-Drupal scalar values generation.
  3. Random asset generator (files of different types).
  4. Static asset generator (files from pre-defined assets).
  5. Random Drupal entity values generation.
  6. Static Drupal entity values generation.

Extending generation helper

See example of class extension: modules/generated_content_example2/src/GeneratedContentExample2Helper.php

See example of class usage: modules/generated_content_example2/generated_content/node/article.inc

Random vs Static content

Sometimes, it is sufficient to simply populate entities with random content to make the site look "not empty". Depending on your deployment strategy (if you are enabling content generation modules on every deployment on top of the fresh database), this may change the content on every deployment.

However, there are times when all generated content can still be a "placeholder" content, but it should be "static" between deployments, so that all content and it's aliases would not change. This is specifically important for Visual Regression testing during a release: the tool can compare generated pages with known aliases in 2 environments and report differences, if any.

Roadmap

  1. Add more random and static generators.
  2. Add tests for existing random and static generators.
  3. Suggest yours.

Local development

Provided that you have PHP installed locally, you can develop an extension using the provided scripts.

Build

Run .devtools/build-codebase.sh (or ahoy build-codebase if Ahoy is installed) to start inbuilt PHP server locally and run the same commands as in CI, plus installing a site and your extension automatically.

Code linting

Run tools individually (or ahoy lint to run all tools if Ahoy is installed) to lint your code according to the Drupal coding standards.

cd build

vendor/bin/phpcs
vendor/bin/phpstan
vendor/bin/rector --clear-cache --dry-run
vendor/bin/phpmd . text phpmd.xml
vendor/bin/twigcs

Tests

Run .devtools/test.sh (or ahoy test if Ahoy is installed) to run all test for your extension.

Browsing SQLite database

To browse the contents of created SQLite database (located at /tmp/site_[EXTENSION_NAME].sqlite), use DB Browser for SQLite.

generated_content's People

Contributors

alexskrypnyk avatar renovate[bot] avatar tannguyen04 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

tannguyen04

generated_content's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

composer
composer.json
  • php >=8.1
github-actions
.github/workflows/assign-author.yml
  • toshimaru/auto-author-assign v2.1.0
.github/workflows/draft-release-notes.yml
  • release-drafter/release-drafter v6

  • Check this box to trigger a request for Renovate to run again on this repository

Update repo structure to https://github.com/AlexSkrypnyk/scaffold

Download the latest code from Scaffold repo (actual code, not the release).

  • Update composer.json with the structure from Scaffold. Preserve the values of the existing fields, but order the fields as per Scaffold's composer.json. Add missing fields.

  • Add Renovate config from Scaffold

  • Add/update all GitHub actions and configs to be per Scaffold. Note that the test GHA workflow should NOT be used as CircleCI is used for this repo and should be preserved.

  • Update readme to use the structure from Scaffold, including the generated logo (circle). Transfer the usage examples into the readme structure from Scaffold

  • Add PHPStan config and fix all newly found violations

  • Do not create docs folder and CI action - this project is too small to have extensive docs.

Make sure that CI passes.

Similar task for reference: AlexSkrypnyk/shellvar#17

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.