PHP code example of m10c / content-elements-bundle

1. Go to this page and download the library: Download m10c/content-elements-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

m10c / content-elements-bundle example snippets


// src/Entity/Author.php
#[Identity(variantClass: AuthorVariant::class)]
class Author
{
    public string $id;
    public string $email;  // Shared across all locales

    #[OneToMany(targetEntity: AuthorVariant::class, mappedBy: 'identity')]
    public Collection $variants;

    public AuthorVariant $variant;  // Hydrated by Provider
}

// src/Entity/AuthorVariant.php
class AuthorVariant
{
    use LocaleDimensionTrait;  // Adds $locale

    #[ManyToOne(inversedBy: 'variants')]
    public Author $identity;

    public string $bio;  // Locale-specific
}

#[Identity(
    variantClass: AuthorVariant::class,
    variantsProperty: 'translations',    // Collection property name (default: 'variants')
    identityProperty: 'author',          // Back-reference property name (default: 'identity')
    variantProperty: 'translation',      // Hydrated variant property name (default: 'variant')
)]
class Author
{
    #[OneToMany(targetEntity: AuthorVariant::class, mappedBy: 'author')]
    public Collection $translations;

    public AuthorVariant $translation;  // Hydrated by Provider
}

class AuthorVariant
{
    #[ManyToOne(inversedBy: 'translations')]
    public Author $author;  // Matches identityProperty
}

// Level 1: Stable identity (just an ID, never changes)
#[Identity(
    variantClass: ContentVersion::class,
    variantsProperty: 'versions',
    identityProperty: 'contentIdentity',
    variantProperty: 'version',
)]
class ContentIdentity
{
    public string $id;

    #[OneToMany(targetEntity: ContentVersion::class, mappedBy: 'contentIdentity')]
    public Collection $versions;

    public ContentVersion $version;      // Active version (hydrated)
    public ContentLocalised $localised;  // Shortcut to version->localised
}

// Level 2: Versioned shared data (thumbnail, tags, etc.)
#[Identity(
    variantClass: ContentLocalised::class,
    variantsProperty: 'localisedVariants',
    identityProperty: 'contentVersion',
    variantProperty: 'localised',
)]
class ContentVersion
{
    use VersionDimensionTrait;  // Adds $version

    #[ManyToOne(inversedBy: 'versions')]
    public ContentIdentity $contentIdentity;

    public ?MediaUpload $thumbnail = null;  // Shared, versioned
    public Collection $tags;                 // Shared, versioned

    #[OneToMany(targetEntity: ContentLocalised::class, mappedBy: 'contentVersion')]
    public Collection $localisedVariants;

    public ContentLocalised $localised;  // Active locale (hydrated)
}

// Level 3: Locale-specific, versioned content
class ContentLocalised
{
    use LocaleDimensionTrait;   // Adds $locale
    use VersionDimensionTrait;  // Adds $version (locale-level versioning)

    #[ManyToOne(inversedBy: 'localisedVariants')]
    public ContentVersion $contentVersion;

    public string $title;   // Locale-specific, versioned
    public string $body;    // Locale-specific, versioned
}

// src/Entity/Page.php
#[ApiResource(
    operations: [
        new GetCollection(provider: IdentityWithVariantProvider::class),
        new Get(provider: IdentityWithVariantProvider::class),
        new Get(
            uriTemplate: '/pages/by-slug/{slug}',
            provider: ByPropertyProvider::class,
        ),
    ],
)]
#[Identity(variantClass: PageVariant::class)]
#[ORM\Entity]
#[ORM\UniqueConstraint(columns: ['slug'])]
class Page
{
    use HasSlugTrait;

    #[ORM\Id, ORM\Column] public string $id;
    #[ORM\OneToMany(targetEntity: PageVariant::class, mappedBy: 'identity', cascade: ['persist'])]
    public Collection $variants;
    public ?PageVariant $variant = null;
}

// src/Entity/PageVariant.php
#[ApiResource(operations: [new Patch()])]
#[ORM\Entity, ORM\HasLifecycleCallbacks]
class PageVariant
{
    use LocaleDimensionTrait;
    use HasSeoMetaTrait;
    use HasBlocksTrait;
    use HasUpdatedAtTrait;

    #[ORM\Id, ORM\Column] public string $id;
    #[ORM\ManyToOne(inversedBy: 'variants')]
    public Page $identity;
}

// src/Cms/BlockType/HeroBlockType.php
final class HeroBlockType implements BlockTypeInterface
{
    public function getKey(): string
    {
        return 'hero';
    }

    // Enforced on every write; a violation reports as e.g. blocks[0].data[headline].
    public function getDataConstraints(): Constraint
    {
        return new Assert\Collection([
            'fields' => [
                'headline' => [new Assert\NotBlank(), new Assert\Length(max: 255)],
                'subhead' => [new Assert\NotBlank(), new Assert\Length(max: 500)],
            ],
            'allowExtraFields' => false,
            'allowMissingFields' => false,
        ]);
    }

    public function getDefaultData(): array
    {
        return ['headline' => '', 'subhead' => ''];
    }

    public function getSchema(): array
    {
        return [
            'label' => 'Hero',
            'fields' => [
                'headline' => ['kind' => 'text', 'label' => 'Headline', 'maxLength' => 255],
                'subhead'  => ['kind' => 'textarea', 'label' => 'Subhead', 'maxLength' => 500],
            ],
        ];
    }
}