PHP code example of povly / moonshine-flexible-layouts

1. Go to this page and download the library: Download povly/moonshine-flexible-layouts 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/ */

    

povly / moonshine-flexible-layouts example snippets


protected function casts(): array
{
    return [
        'content' => \Povly\FlexibleLayouts\Casts\FlexibleCast::class,
    ];
}

use Povly\FlexibleLayouts\Fields\FlexibleLayouts;

FlexibleLayouts::make('Content', 'content')
    ->block('hero', 'Hero', [
        Text::make('Title', 'title'),
        Image::make('Background', 'image'),
    ])
    ->block('text', 'Text Block', [
        Textarea::make('Body', 'body'),
    ])
    ->block('gallery', 'Gallery', [
        Json::make('Images', 'images'),
    ]);

->block('cta', 'Call to Action', [
    Text::make('Button Text', 'label'),
    Text::make('Link', 'url'),
], limit: 1)

// MoonShine icon names (301+ Heroicons built-in)
->block('hero', 'Hero', [
    Text::make('Title', 'title'),
    Image::make('Background', 'image'),
], category: 'Header', description: 'Large banner with background', icon: 'photo')

->block('gallery', 'Gallery', [
    Json::make('Images', 'images'),
], category: 'Media', description: 'Image grid gallery', icon: 'rectangle-stack')

->block('wysiwyg', 'Text Editor', [
    Textarea::make('Body', 'body'),
], category: 'Content', description: 'Rich text content', icon: 'document-text')

// Emoji also works
->block('cta', 'Call to Action', [
    Text::make('Text', 'label'),
    Text::make('Link', 'url'),
], limit: 1, icon: '🔗')

->block('section', 'Section', [
    Text::make('Title', 'title'),
    FlexibleLayouts::make('Blocks', 'blocks')
        ->block('button', 'Button', [
            Text::make('Text', 'text'),
            Text::make('Link', 'link'),
        ])
        ->block('form', 'Form', [
            Select::make('Type', 'form_type')->options([
                'hotel' => 'Hotel',
                'tickets' => 'Tickets',
            ]),
            Text::make('Redirect URL', 'url'),
        ]),
])

->disableAdd()     // hide the "Add block" button
->disableRemove()  // hide the remove button on blocks
->disableDuplicate()  // hide the duplicate button on blocks
->disableSort()    // disable drag-to-reorder

->addButton(ActionButton::make('Add')->primary())
->removeButton(ActionButton::make('Delete')->icon('trash')->error())
->duplicateButton(ActionButton::make('Duplicate')->icon('square-2-stack')->secondary())

// Top-level — default labels ("Add block", "Search blocks...")
FlexibleLayouts::make('Blocks', 'blocks')
    ->block('hero', 'Hero', [...])

// Nested — custom labels via separate translation file
->block('section', 'Section', [
    Text::make('Title', 'title'),

    FlexibleLayouts::make('Refs', 'refs')
        ->transKey('refs')   // uses flexible-layouts::refs.* translations
        ->block('reference', 'Reference', [
            Text::make('Title', 'title'),
        ]),
])

return [
    'add_block'       => 'Add reference',
    'search_blocks'   => 'Search references...',
    'no_blocks_found' => 'No references found',
    'all_categories'  => 'All',
];

->block('hero', 'Hero', [
    Flex::make([
        Column::make([
            Text::make('Title', 'title'),
        ])->columnSpan(6),

        Column::make([
            Text::make('Subtitle', 'subtitle'),
        ])->columnSpan(6),
    ]),
])

// config/flexible-layouts.php
return [
    'route_prefix' => 'flexible-layouts',
    'logging' => env('FLEXIBLE_LAYOUTS_LOGGING', config('app.debug')),
];

// lang/vendor/flexible-layouts/de/messages.php
return [
    'add_block' => 'Block hinzufügen',
    'search_blocks' => 'Blöcke suchen...',
    'no_blocks_found' => 'Keine Blöcke gefunden',
    'all_categories' => 'Alle',
];

protected function casts(): array
{
    return [
        'content' => \Povly\FlexibleLayouts\Casts\FlexibleCast::class,
    ];
}

use Povly\FlexibleLayouts\Fields\FlexibleLayouts;

FlexibleLayouts::make('Контент', 'content')
    ->block('hero', 'Hero', [
        Text::make('Заголовок', 'title'),
        Image::make('Фон', 'image'),
    ])
    ->block('text', 'Текстовый блок', [
        Textarea::make('Текст', 'body'),
    ])
    ->block('gallery', 'Галерея', [
        Json::make('Изображения', 'images'),
    ]);

->block('cta', 'Призыв к действию', [
    Text::make('Текст кнопки', 'label'),
    Text::make('Ссылка', 'url'),
], limit: 1)

// Имена иконок MoonShine (301+ Heroicons встроено)
->block('hero', 'Hero', [
    Text::make('Заголовок', 'title'),
    Image::make('Фон', 'image'),
], category: 'Шапка', description: 'Большой баннер с фоном', icon: 'photo')

->block('gallery', 'Галерея', [
    Json::make('Изображения', 'images'),
], category: 'Медиа', description: 'Сетка изображений', icon: 'rectangle-stack')

->block('wysiwyg', 'Текстовый редактор', [
    Textarea::make('Текст', 'body'),
], category: 'Контент', description: 'Форматированный текст', icon: 'document-text')

// Emoji тоже работает
->block('cta', 'Призыв к действию', [
    Text::make('Текст', 'label'),
    Text::make('Ссылка', 'url'),
], limit: 1, icon: '🔗')

->block('section', 'Секция', [
    Text::make('Заголовок', 'title'),
    FlexibleLayouts::make('Блоки', 'blocks')
        ->block('button', 'Кнопка', [
            Text::make('Текст', 'text'),
            Text::make('Ссылка', 'link'),
        ])
        ->block('form', 'Форма', [
            Select::make('Тип', 'form_type')->options([
                'hotel' => 'Отель',
                'tickets' => 'Билеты',
            ]),
            Text::make('URL перенаправления', 'url'),
        ]),
])

->disableAdd()     // скрыть кнопку «Добавить блок»
->disableRemove()  // скрыть кнопку удаления у блоков
->disableDuplicate()  // скрыть кнопку дублирования у блоков
->disableSort()    // запретить drag-сортировку

->addButton(ActionButton::make('Добавить')->primary())
->removeButton(ActionButton::make('Удалить')->icon('trash')->error())
->duplicateButton(ActionButton::make('Дублировать')->icon('square-2-stack')->secondary())

// Верхний уровень — подписи по умолчанию («Добавить блок», «Поиск блоков...»)
FlexibleLayouts::make('Блоки', 'blocks')
    ->block('hero', 'Hero', [...])

// Вложенный — свои подписи из отдельного файла переводов
->block('section', 'Секция', [
    Text::make('Заголовок', 'title'),

    FlexibleLayouts::make('Справочники', 'refs')
        ->transKey('refs')   // берёт flexible-layouts::refs.*
        ->block('reference', 'Справочник', [
            Text::make('Название', 'title'),
        ]),
])

return [
    'add_block'       => 'Добавить справочник',
    'search_blocks'   => 'Поиск справочников...',
    'no_blocks_found' => 'Справочники не найдены',
    'all_categories'  => 'Все',
];

->block('hero', 'Hero', [
    Flex::make([
        Column::make([
            Text::make('Заголовок', 'title'),
        ])->columnSpan(6),

        Column::make([
            Text::make('Подзаголовок', 'subtitle'),
        ])->columnSpan(6),
    ]),
])

// config/flexible-layouts.php
return [
    'route_prefix' => 'flexible-layouts',
    'logging' => env('FLEXIBLE_LAYOUTS_LOGGING', config('app.debug')),
];

// lang/vendor/flexible-layouts/de/messages.php
return [
    'add_block' => 'Block hinzufügen',
    'search_blocks' => 'Blöcke suchen...',
    'no_blocks_found' => 'Keine Blöcke gefunden',
    'all_categories' => 'Alle',
];
bash
php artisan vendor:publish --tag=flexible-layouts
bash
php artisan vendor:publish --tag=flexible-layouts-config
php artisan vendor:publish --tag=flexible-layouts-lang
bash
php artisan vendor:publish --tag=flexible-layouts-config
bash
php artisan vendor:publish --tag=flexible-layouts-lang
bash
# Example: add German
cp lang/vendor/flexible-layouts/en/messages.php lang/vendor/flexible-layouts/de/messages.php
bash
php artisan vendor:publish --tag=flexible-layouts
bash
php artisan vendor:publish --tag=flexible-layouts-config
php artisan vendor:publish --tag=flexible-layouts-lang
bash
php artisan vendor:publish --tag=flexible-layouts-config
bash
php artisan vendor:publish --tag=flexible-layouts-lang
bash
# Пример: немецкий
cp lang/vendor/flexible-layouts/en/messages.php lang/vendor/flexible-layouts/de/messages.php