Giter Site home page Giter Site logo

craft-relabel's Introduction

Relabel Plugin for Craft CMS 3.x

drawing

Requirements

This plugin requires Craft CMS 3.0.0 or later. Important: if you are coming from Craft 2 - there is no migration currently so all your Relabels will be deleted. If you really need a migration feel free to contact me or backup the table and insert it on your own

Installation

To install the plugin, follow these instructions.

  1. Open your terminal and go to your Craft project:

     cd /path/to/project
    
  2. Then tell Composer to load the plugin:

     composer require anubarak/craft-relabel
    
  3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Relabel .

Basic Hints

Relabel creates a custom Database table that stores the new labels. Default Craft fields are not touched in any way. The strings are replaced by JavaScript so you can remove/uninstall the plugin whenever you want without breaking changes in your Control Panel. Relabel is compatible with the Craft 3 project config so you only need to relabel your fields in your main environment and can sync all data.

Usage

Go to your field layout, click on the wheel icon and select "Relabel"

Screenshot

Your field layout will have a different label and description.

To receive error messages that contains your new relabel strings do

$errors = Relabel::getErrors($element);

Or in Twig

{% set errors = relabel.getErrors(element) %}

Register Relabel for custom element types

There is an event to register Relabel for a custom form,

use anubarak\relabel\services\RelabelService;

Event::on(
    RelabelService::class,
    RelabelService::EVENT_REGISTER_LABELS,
    function(RegisterLabelEvent $event) use($myCustomElement){
        $event->fieldLayoutId = $myCustomElement->fieldLayoutId;
    }
);

Register additional Relabels

You can as well register additional Labels for every field layout within your Plugins init function as well. Important: you need to have the event there and not in certain Controllers because I need to pass those variables before the Controller action begins otherwise I'm not able to inject the required JavaScript to the request. This is an example how you could register custom nested field layouts

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;
                    }
                }
            }
        }
    }
);

If Relabel does not support your HTML yet, feel free to contact me and we'll find a way to make it work

Currently supported Element Types are

  • craft\elements\Entries
  • craft\elements\Assets
  • craft\elements\GlobalSets
  • craft\elements\Categories
  • craft\elements\Users
  • verbb\voucher\elements\Voucher
  • verbb\voucher\elements\Code
  • craft\commerce\elements\Order
  • craft\commerce\elements\Product
  • craft\commerce\elements\Variant
  • Solspace\Calendar\elements\Calendar

Register custom labels after Ajax requests

Crafts entries are able to change the field layout by changing the entry type, if you want to be able to change the field layout for a custom element type via Javascript as well you need to include these lines

$labelsForLayout = Relabel::getService()->getAllLabelsForLayout($layout->id);
Craft::$app->getView()->registerJs('Craft.relabel.changeEntryType(' . json_encode($labelsForLayout) . ');');

craft-relabel's People

Contributors

anubarak avatar bencroker avatar richardcalahan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

craft-relabel's Issues

Integrity constraint violation: 1048 Column 'fieldLayoutId' cannot be null

Description
Running a new Craft install with a project.yml that includes settings from relabel produces the following error during the install database migration step:

Exception: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'fieldLayoutId' cannot be null
The SQL being executed was: INSERT INTO `relabel` (`uid`, `fieldId`, `fieldLayoutId`, `instructions`, `name`, `dateCreated`, `dateUpdated`) VALUES ('df7a1cf5-4ef3-4c6f-8991-01b188b9bf7f', 62, NULL, '', 'Content', '2019-10-27 14:20:55', '2019-10-27 14:20:55') (/home/vagrant/code/proejct/craft/vendor/yiisoft/yii2/db/Schema.php:664)

Example entry from project.yml looks like:

relabel:
  016043c5-bb96-46d6-b148-811900a56f14:
    field: 686a3021-a8b1-4213-b8b5-4e4886823ed8
    fieldLayout: e9270c63-c469-418f-b352-5bf4d56b9cea
    instructions: ''
    name: 'Show Facebook on page'

MySQL v5.7
PHP v7.2
Craft v3.2.10

Not working with Solspace Calendar plugin

First of all, thanks for this plugin! It makes managing fields a lot easier. 🎉

I'm using the Solspace Calendar plugin, and while I'm able to change labels when I'm setting up my fields for each calendar, the new labels don't display when adding/editing an entry.

I see in your docs that you don't officially support Solspace events yet. Is this something that Solspace can add support for on their end? If so, what can I tell them? Thanks!

Empty label not falling back to default label

For the Relabel options on a field, if I do not specify a value for 'New Label' in the HUD, when the field is rendered it is rendered without a label. I would have thought that if no label is specified in the HUD, then when the field is rendered it should fall back to using the field's default label.

Relabels no longer working in entries

Hi, When I relabel in the field layout designer, it works just fine. Once it's saved, the change doesn't take place in the entry fields, however, if I view in the field layout designer, the change seems to have saved correctly.

Screen Shot 2020-04-28 at 12 56 24 PM

Screen Shot 2020-04-28 at 12 56 03 PM

Not working with assets

Loving the plugin, so thanks for creating it.

However, I've set up a field called "Description" but wanted to use it and relabel it to "Caption" for use on images.

Please see the screenshots below to illustrate it's not updating the field title in the Assets section.

screenshot 2019-02-13 at 20 50 35
screenshot 2019-02-13 at 20 50 23

Cheers

Doesn't work with Neo

As of right now it does not appear to work with NEO in Craft 3. All appears well except nothing is saved. You can click to save it however on refresh of the field, it is as if you never set any relabel.

Relabels not showing after Relabel (1.3.5.2) update

Just updated to the latest Relabel (1.3.5.2) on my local machine and all relabels are no longer in CP. I have not updated on my live site, and relabels are working just fine. Seems like something broke with the update. Is anyone else seeing this?

Allow Markdown inside field instructions

Craft allows Markdown inside the native field instructions. This comes handy when you want to italicise something, or provide a link. Relabel appears to support raw HTML but does not appear to process Markdown inside the descriptions. It would be great if you can add support for that.

Cheers, and thank you for the plugin!

Does not install

After the plugin installs from the Plugin Store, I try to install from /Settings/Plugins/ and I get: "Could't install plugin". I don't see any errors in the logs.

I've tried multiple times with the same results and other plugins install with no issues.

Matrix field with the same handle as a regular field gets relabelled inadvertently

An entry type which has a field called “Label” with the handle label also has a Matrix field, some of whose block types have fields with the handle label. If I relabel the regular field from "Label" to "Objective", the fields inside the Matrix field’s block types with the same handle also end up getting relabeled.

Correct relabel

Incorrect relabel

Additional info

  • Craft version: 3.4.3
  • Plugin version: 1.3.4

Handle missing fields or layouts in a more graceful manner

There are scenarios where the plugin can leave the project config in a state that prevents importing into another site.

It seems that if, for example, a matrix block type gets deleted, the corresponding labels in the project config do not get removed, even though the field layouts no longer exist.
This leaves the project config in a state, where if you apply it to a different environment, a DB error will be thrown because of non-nullable columns.

The easiest way to hotfix this is to check if any of these calls return null, and, if so, skipping the (re)label.

Security issue: private admin field labels and instructions being leaked in login screen source code

A security issue reported in Relabel for Craft 2 (nearly 21 months ago) still exists in the Craft 3 version. Originally reported at spicywebau/craft-fieldlabels#12 but I'll repost here.


I just noticed that when viewing the source code of my Craft login screen, the relabel plugin seems to be dumping all its JSON data there. Since this could potentially include private data that includes field names and instructions intended for admin users only, this should be excluded from the login screen and any other screens that are potentially accessible by non-admins.

See Craft 3 example here: https://aspect.staging3.yump.com.au/admin/login

I don't have time right now to submit a patch, but thought you should be aware.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.