PHP code example of anubarak / craft-relabel
1. Go to this page and download the library: Download anubarak/craft-relabel 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/ */
anubarak / craft-relabel example snippets
$errors = Relabel::getErrors($element);
use anubarak\relabel\services\RelabelService;
Event::on(
RelabelService::class,
RelabelService::EVENT_REGISTER_LABELS,
function(RegisterLabelEvent $event) use($myCustomElement){
$event->fieldLayoutId = $myCustomElement->fieldLayoutId;
}
);
Event::on(
RelabelService::class,
RelabelService::EVENT_REGISTER_ADDITIONAL_LABELS,
function(Event $event) {
$layoutId = $event->fieldLayoutId;
$fields = Craft::$app->getFields()->getFieldsByLayoutId($layoutId);
$plugin = Craft::$app->getPlugins()->getPlugin('relabel');
/** @var RelabelService $service */
$service = $plugin->get('relabel');
foreach ($fields as $field){
if($field instanceof Field){
$blocks = $field->getBlockTypes();
foreach ($blocks as $block){
$layoutForBlock = (int)$block->fieldLayoutId;
$index = $field->handle . '.' . $block->handle;
$relabels = $service->getAllLabelsForLayout($layoutForBlock, $index);
foreach ($relabels as $relabel){
$event->labels[] = $relabel;
}
}
}
}
}
);
$labelsForLayout = Relabel::getService()->getAllLabelsForLayout($layout->id);
Craft::$app->getView()->registerJs('Craft.relabel.changeEntryType(' . json_encode($labelsForLayout) . ');');