PHP code example of nystudio107 / craft-code-editor
1. Go to this page and download the library: Download nystudio107/craft-code-editor 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-code-editor example snippets
return [
// Whether to allow anonymous access be allowed to the codeeditor/autocomplete/index endpoint
'allowFrontendAccess' => true,
// Whether to allow frontend templates access to the `codeeditor/codeEditor.twig` Twig template
'allowTemplateAccess' => true,
// The default autocompletes to use for the default `CodeEditor` field type
'defaultCodeEditorAutocompletes' => [
CraftApiAutocomplete::class,
TwigLanguageAutocomplete::class,
SectionShorthandFieldsAutocomplete::class,
]
];
use nystudio107\codeeditor\autocompletes\EnvironmentVariableAutocomplete;
use nystudio107\codeeditor\events\RegisterCodeEditorAutocompletesEvent;
use nystudio107\codeeditor\services\AutocompleteService;
Event::on(
AutocompleteService::class,
AutocompleteService::EVENT_REGISTER_CODEEDITOR_AUTOCOMPLETES,
function (RegisterCodeEditorAutocompletesEvent $event) {
$event->types[] = EnvironmentVariableAutocomplete::class;
}
);
use nystudio107\codeeditor\events\RegisterCodeEditorAutocompletesEvent;
use nystudio107\codeeditor\services\AutocompleteService;
use putyourlightson\sprig\plugin\autocompletes\SprigApiAutocomplete;
public const SPRIG_TWIG_FIELD_TYPE = 'SprigField';
Event::on(
AutocompleteService::class,
AutocompleteService::EVENT_REGISTER_CODEEDITOR_AUTOCOMPLETES,
function (RegisterCodeEditorAutocompletesEvent $event) {
if ($event->fieldType === self::SPRIG_TWIG_FIELD_TYPE) {
$event->types[] = SprigApiAutocomplete::class;
}
}
);
use nystudio107\codeeditor\autocompletes\CraftApiAutocomplete;
use nystudio107\codeeditor\events\RegisterCodeEditorAutocompletesEvent;
use nystudio107\codeeditor\services\AutocompleteService;
Event::on(
AutocompleteService::class,
AutocompleteService::EVENT_REGISTER_CODEEDITOR_AUTOCOMPLETES,
function (RegisterCodeEditorAutocompletesEvent $event) {
$config = [
'additionalGlobals' => $arrayOfVariables,
];
$event->types[] = [CraftApiAutocomplete::class => $config];
}
);
namespace myvendor\myname\autocompletes;
use nystudio107\codeeditor\base\Autocomplete;
use nystudio107\codeeditor\models\CompleteItem;
use nystudio107\codeeditor\types\AutocompleteTypes;
use nystudio107\codeeditor\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\codeeditor\validators\TwigTemplateValidator;
public function defineRules()
{
return [
['myTwigCode', TwigTemplateValidator::class],
];
}
use nystudio107\codeeditor\validators\TwigTemplateValidator;
public function defineRules()
{
return [
[
'myTwigCode', TwigTemplateValidator::class,
'variables' => [
'foo' => 'bar',
]
],
];
}
use nystudio107\codeeditor\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.