1. Go to this page and download the library: Download wmd/craft-design-field 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/ */
wmd / craft-design-field example snippets
return [
// Old option field => group, used when reading or moving old fields.
'fieldMap' => ['accentToken' => 'tone'],
'groups' => [
'tone' => [
'label' => 'Section tone',
'instructions' => 'Background and text tone for the whole section.',
'tokens' => 'accent', // config/designtokens/accent.json
'auto' => 'Block default', // adds an `auto` option: the template decides
'input' => 'swatches',
'optionMeta' => ['primary' => ['swatch' => '#4f46e5']],
],
'columns' => [
'options' => ['2' => ['label' => '2'], '3' => ['label' => '3'], '4' => ['label' => '4']],
'default' => '3',
'input' => 'slider',
'aliases' => ['three' => '3'], // renamed keys keep working
],
],
'profiles' => [
'blockTeam' => [
'variant' => [
'label' => 'Layout',
'input' => 'tiles',
'options' => [
'_default' => ['label' => 'Grid', 'image' => '/design-field/blockTeam/_default.jpg'],
'cards' => ['label' => 'Cards', 'image' => '/design-field/blockTeam/cards.jpg'],
],
],
'tone',
'columns' => ['default' => '4', 'variants' => ['_default']], // shown only for the Grid layout
],
'*' => ['tone'],
],
];
'groupOverrides' => [
'columns' => ['input' => 'buttons'], // every block
],
'profileOverrides' => [
'blockTeam' => ['columns' => ['input' => 'slider', 'default' => '4']], // only Team
],
// Settings page: the block next to its panel. {type} is the entry type handle,
// {design} the panel's keys as JSON; the site's route renders a saved block with them.
// examples/block-preview.twig is a ready page for this route.
'blockPreviewUrl' => '/_design-field/block?type={type}&design={design}',
'templateCheck' => [
'blocks' => '_blocks/{type}', // a block's templates; {type} is the entry type handle
'everyBlock' => ['_blocks/_render'], // rendered for every block (the dispatcher)
'code' => ['@root/modules'], // PHP that reads options too
],
use craft\elements\Entry;
use wmd\designfield\events\DefinePanelGroupsEvent;
use wmd\designfield\fields\Design;
use yii\base\Event;
Event::on(Design::class, Design::EVENT_DEFINE_PANEL_GROUPS, function(DefinePanelGroupsEvent $event) {
$group = $event->groups['sourceField'] ?? null;
$owner = $event->element instanceof Entry ? $event->element->getOwner() : null;
if ($group === null || !$owner instanceof Entry) {
return;
}
$onPage = array_map(fn($field) => $field->handle, $owner->getFieldLayout()->getCustomFields());
// Keep the hidden choices already set on the settings page.
$event->groups['sourceField'] = $group->withHidden([
...$group->hidden,
...array_diff(array_keys($group->options), $onPage),
]);
});