1. Go to this page and download the library: Download nystudio107/craft-twigfield 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/ */
nystudio107 / craft-twigfield example snippets
return [
// Whether to allow anonymous access be allowed to the twigfield/autocomplete/index endpoint
'allowFrontendAccess' => true,
// The default autcompletes to use for the default `Twigfield` field type
'defaultTwigfieldAutocompletes' => [
CraftApiAutocomplete::class,
TwigLanguageAutocomplete::class,
]
];
use nystudio107\twigfield\autocompletes\EnvironmentVariableAutocomplete;
use nystudio107\twigfield\events\RegisterTwigfieldAutocompletesEvent;
use nystudio107\twigfield\services\AutocompleteService;
Event::on(
AutocompleteService::class,
AutocompleteService::EVENT_REGISTER_TWIGFIELD_AUTOCOMPLETES,
function (RegisterTwigfieldAutocompletesEvent $event) {
$event->types[] = EnvironmentVariableAutocomplete::class;
}
);
use nystudio107\twigfield\events\RegisterTwigfieldAutocompletesEvent;
use nystudio107\twigfield\services\AutocompleteService;
use putyourlightson\sprig\plugin\autocompletes\SprigApiAutocomplete;
public const SPRIG_TWIG_FIELD_TYPE = 'SprigField';
Event::on(
AutocompleteService::class,
AutocompleteService::EVENT_REGISTER_TWIGFIELD_AUTOCOMPLETES,
function (RegisterTwigfieldAutocompletesEvent $event) {
if ($event->fieldType === self::SPRIG_TWIG_FIELD_TYPE) {
$event->types[] = SprigApiAutocomplete::class;
}
}
);
use nystudio107\twigfield\autocompletes\CraftApiAutocomplete;
use nystudio107\twigfield\events\RegisterTwigfieldAutocompletesEvent;
use nystudio107\twigfield\services\AutocompleteService;
Event::on(
AutocompleteService::class,
AutocompleteService::EVENT_REGISTER_TWIGFIELD_AUTOCOMPLETES,
function (RegisterTwigfieldAutocompletesEvent $event) {
$config = [
'additionalGlobals' => $arrayOfVariables,
];
$event->types[] = [CraftApiAutocomplete::class => $config];
}
);
namespace myvendor\myname\autocompletes;
use nystudio107\twigfield\base\Autocomplete;
use nystudio107\twigfield\models\CompleteItem;
use nystudio107\twigfield\types\AutocompleteTypes;
use nystudio107\twigfield\types\CompleteItemKind;
class MyCustomAutocomplete extends Autocomplete
{
public $name = 'EnvironmentVariableAutocomplete';
public $type = AutocompleteTypes::GeneralAutocomplete;
public $hasSubProperties = false;
public function generateCompleteItems(): void
{
CompleteItem::create()
->label('MyAutocomplete')
->insertText('MyAutocomplete')
->detail('This is my autocomplete')
->documentation('This detailed documentation of my autocomplete')
->kind(CompleteItemKind::ConstantKind)
->add($this);
}
}
use nystudio107\twigfield\validators\TwigTemplateValidator;
public function defineRules()
{
return [
['myTwigCode', TwigTemplateValidator::class],
];
}
use nystudio107\twigfield\validators\TwigTemplateValidator;
public function defineRules()
{
return [
[
'myTwigCode', TwigTemplateValidator::class,
'variables' => [
'foo' => 'bar',
]
],
];
}
use nystudio107\twigfield\validators\TwigObjectTemplateValidator;
public function defineRules()
{
return [
[
'myTwigCode', TwigObjectTemplateValidator::class,
'object' => $object,
'variables' => [
'foo' => 'bar',
]
],
];
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.